【问题标题】:Flutter - Issues with AnimatorFlutter - 动画师的问题
【发布时间】:2020-08-30 15:50:37
【问题描述】:

我在这里有更多的问题。今天,我遇到了 Animator 的问题。我正在尝试为我的帖子点赞代码设置一个小心脏符号动画,但我不断收到以下错误。

Compiler message:
../../../developer/flutter/.pub-cache/hosted/pub.dartlang.org/animator-0.1.4/lib/animator.dart:457:7: Error: No named parameter with the name 'blocs'.
      blocs: [_animatorBloc],
      ^^^^^
../../../developer/flutter/.pub-cache/hosted/pub.dartlang.org/states_rebuilder-1.15.0/lib/src/state_with_mixin_builder.dart:142:3: Context: Found this candidate, but the arguments don't match.
  StateWithMixinBuilder({
  ^^^^^^^^^^^^^^^^^^^^^
../../../developer/flutter/.pub-cache/hosted/pub.dartlang.org/animator-0.1.4/lib/animator.dart:494:27: Error: Method not found: 'StatesRebuilder.addToListeners'.
          StatesRebuilder.addToListeners(
                          ^^^^^^^^^^^^^^
../../../developer/flutter/.pub-cache/hosted/pub.dartlang.org/animator-0.1.4/lib/animator.dart:559:27: Error: Method not found: 'StatesRebuilder.removeFromListeners'.
          StatesRebuilder.removeFromListeners(b, widget.name, "$hashCode");
                          ^^^^^^^^^^^^^^^^^^^

我试图运行 flutter clean 无济于事,我无法在网上找到最佳答案。我试图更新我的 pubspec.yaml,但这弄乱了我在项目中的很多代码。我想看看其他人的想法。

Post.dart - 查看 buildPostImage

class Post extends StatefulWidget {
  final String postId;
  final String ownerId;
  final String userName;
  final String location;
  final String description;
  final String mediaUrl;
  final dynamic likes;

  Post({
    this.postId,
    this.ownerId,
    this.userName,
    this.location,
    this.description,
    this.mediaUrl,
    this.likes,
  });

  factory Post.fromDocument(DocumentSnapshot doc){
    return Post(
      postId: doc['postId'],
      ownerId: doc['ownerId'],
      userName: doc['username'],
      location: doc['location'],
      description: doc['description'],
      mediaUrl: doc['mediaUrl'],
      likes: doc['likes'],
    );
  }

  int getLikeCount(likes) {
    // if no likes, return 0
    if(likes == null){
      return 0;
    }
    int count = 0;
    //if like explicitly set to true, add a like
    likes.values.forEach((val){
      if(val == true){
        count += 1;
      }
    });
    return count;
  }

  @override
  _PostState createState() => _PostState(
    postId: this.postId,
    ownerId: this.ownerId,
    userName: this.userName,
    location: this.location,
    description: this.description,
    mediaUrl: this.mediaUrl,
    likeCount: getLikeCount(this.likes),
    likes: this.likes
  );
}

class _PostState extends State<Post> {
  final String currentUserId = currentUser?.id;
  final String postId;
  final String ownerId;
  final String userName;
  final String location;
  final String description;
  final String mediaUrl;
  int likeCount;
  Map likes;
  bool isLiked;
  bool showHeart = false;

  _PostState({
    this.postId,
    this.ownerId,
    this.userName,
    this.location,
    this.description,
    this.mediaUrl,
    this.likes,
    this.likeCount,
  });

  buildPostHeader(){
    return FutureBuilder(
      future: usersRef.document(ownerId).get(),
      builder: (context, snapshot){
        if(!snapshot.hasData){
          return circularProgress(context);
        }
        User user = User.fromDocument(snapshot.data);
        return ListTile(
          leading: CircleAvatar(
            backgroundImage: CachedNetworkImageProvider(user.photoUrl),
            backgroundColor: Colors.grey,
          ),
          title: GestureDetector(
            child: Text(
              user.username,
              style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.bold
              ),
            ),
          ),
          subtitle: Text(location),
          trailing: IconButton(
            onPressed: () => print("deleting post"),
            icon: Icon(Icons.more_vert),
          ),
        );
      },
    );
  }

  handleLikePosts() {
    bool _isLiked = likes[currentUserId] == true;

    if (_isLiked) {
      postsRef.document(ownerId)
          .collection('userPosts')
          .document(postId)
          .updateData({'likes.$currentUserId': false});
      setState(() {
        likeCount -= 1;
        isLiked = false;
        likes[currentUserId] = false;
      });
    } else if (!_isLiked) {
      postsRef.document(ownerId)
          .collection('userPosts')
          .document(postId)
          .updateData({'likes.$currentUserId': true});
        setState(() {
          likeCount += 1;
          isLiked = true;
          likes[currentUserId] = true;
          showHeart = true;
        });
        Timer(Duration(milliseconds: 500),(){
          setState(() {
            showHeart = false;
          });
      });
    }
  }

  buildPostImage() {
    return GestureDetector(
      onDoubleTap: handleLikePosts,
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          cachedNetworkImage(mediaUrl),
          showHeart
              ? Animator(
            duration: Duration(milliseconds: 300),
            tween: Tween(begin: 0.8, end: 1.4),
            curve: Curves.elasticOut,
            cycles: 0,
            builder: (anim) => Transform.scale(
              scale: anim.value,
              child: Icon(
                Icons.favorite,
                size: 80.0,
                color: Colors.red,
              ),
            ),
          )
              : Text(""),
        ],
      ),
    );
  }

  buildPostFooter() {
    return Column(
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Padding(padding: EdgeInsets.only(top: 40.0, left: 20.0)),
            GestureDetector(
              onTap: handleLikePosts,
              child: Icon(
                isLiked ?  Icons.favorite : Icons.favorite_border,
                size: 28.0,
                color: Colors.pink,
              ),
            ),
            Padding(padding: EdgeInsets.only(right: 20.0)),
            GestureDetector(
              onTap: () => print('Showing comments'),
              child: Icon(
                Icons.chat,
                size: 28.0,
                color: Colors.blue[900],
              ),
            ),
          ],
        ),
        Row(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: 20.0),
              child: Text(
                "$likeCount likes ",
                style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ],
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: 20.0),
              child: Text(
                "$userName ",
                style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Expanded(
                child: Text(description),
            )
          ],
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {

    isLiked = (likes[currentUserId] == true);

    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        buildPostHeader(),
        buildPostImage(),
        buildPostFooter(),
      ],
    );
  }
}

Thank you all for the help.

【问题讨论】:

    标签: android ios flutter dart


    【解决方案1】:

    看起来您使用的是 0.1.4 版本的 Animator 包。将其更新到最新版本 2.0.1 (visit here for Animator Package Updates)。

    Animator(
                duration: Duration(milliseconds: 300),
                tween: Tween(begin: 0.8, end: 1.5),
                curve: Curves.elasticOut,
                cycles: 0,
                builder: (context, anim, child) => Transform.scale(
                    scale: anim.value,
                    child: Icon(Icons.favorite, color: Colors.pink, size: 100.0,),
                ),
              )
    

    检查它是否有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-26
      • 2023-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多