【问题标题】:Flutter Animating listview elements is not working颤振动画列表视图元素不起作用
【发布时间】:2021-02-15 17:49:55
【问题描述】:

我正在尝试对容器进行发光效果。我正在使用 streambuilder 来构建列表项目,当项目价格发生变化时,我想显示容器发光效果。到目前为止,我已经这样做了,但小部件根本没有显示效果。 didUpdateWidget 不显示任何更新,并且当它显示时,所有列表视图项都会以无限时间为自己设置动画。有什么我遗漏的吗?

 AnimationController _animationController;
  @override
  void initState() {
    super.initState();
    _animationController = new AnimationController(
        vsync: this, duration: new Duration(milliseconds: 500));
  }

  @override
  void didUpdateWidget(CustomContainer oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.price != widget.price) {
      print("changed");
      _animationController.repeat(
          reverse: true, period: Duration(milliseconds: 1000));
    }
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }
@override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
        animation: _animationController,
        builder: (context, _) {
          return Container(
            height:100,
            width:double.infinity,
            decoration: BoxDecoration(
              color: Theme.of(context).canvasColor,
              borderRadius: BorderRadius.circular(10),
              boxShadow: [
                BoxShadow(
                  blurRadius: 4 * _animationController.value,
                  color: primaryColor.withOpacity(0.7),
                ),
              ],
            ),
          );
        });

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    AnimationController.repeat() 将开始运行动画并无限次重复。这就是为什么这些项目会无限地为自己设置动画。您必须改用.forward 方法:

    _animationController = AnimationController(
      duration: Duration(milliseconds: 500),
      vsync: this);
    
    _animationController.forward(); //tell the animation to start without repeating
    

    来自Flutter Docs

    AnimationController 是一个特殊的 Animation 对象,它生成一个新的 每当硬件准备好接收新帧时的值。默认情况下,一个 AnimationController 线性生成从 0.0 到 1.0 的数字 在给定的持续时间内。

    AnimationController 派生自 Animation,所以可以使用 任何需要动画对象的地方。然而 AnimationController 有额外的方法来控制动画。 例如,您使用 .forward() 方法启动动画。这 数字的生成与屏幕刷新有关,因此通常为 60 每秒生成数字。每个数字生成后,每个 动画对象调用附加的侦听器对象。创建一个 每个孩子的自定义显示列表,请参阅RepaintBoundary

    这是AnimationController.forward() 的文档:https://api.flutter.dev/flutter/animation/AnimationController/forward.html

    【讨论】:

    • 实际上我相信动画控制器会在 _repeat 上重复提供的时间段。但这不是唯一的问题。即使数据更改,didUpdateWidget 也不会触发。并且每个单独的小部件都没有动画,而是每个小部件都在动画时自己制作动画。
    • 数据变化时最好使用Bloc状态管理触发。您可以将动画控制器传递给函数并使其前进。每次小部件更改时调用 didUpdateWidget 是非常低效的。最好使用 StreamBuilders 来更新屏幕中的特定小部件,而不是一次全部更新。我个人使用 rxdart 进行 bloc 状态管理:medium.com/flutter-community/…
    • 我正在使用基于 rxdart 的 bloc 进行状态管理,但仍然遇到这个问题。
    • 你能更新你的代码吗?此外,新关键字现在是可选的。你可以删除它们。
    • 删除 didUpdateWidget 并添加 AnimationController.forward() 当你每次想要更新你的小部件时。将 AnimationController.forward() 放在 _subject.add() 下方。
    猜你喜欢
    • 2018-11-17
    • 2022-01-23
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2021-02-01
    • 2020-11-21
    相关资源
    最近更新 更多