【问题标题】:No animation on linear progress indicator when adding statuslistener添加状态监听器时线性进度指示器上没有动画
【发布时间】:2021-10-11 16:40:35
【问题描述】:

当我添加 AddStatusListener 时,我的 LinearProgressIndicator 没有向前动画。动画完成后,我得到了重新渲染的构建方法,但没有发生线性动画。我希望动画运行,并且在它完成后我需要一个小部件出现,在这种情况下是文本小部件。

这是我的代码 -

class _AuthenticationPageState extends State<AuthenticationPage> with TickerProviderStateMixin {

  late AnimationController controller;
  bool test = false;
    @override
      void initState() {
        controller = AnimationController(
          vsync: this,
          duration: const Duration(seconds: 5),
        )..addStatusListener((AnimationStatus status) {
          setState(() {
            if(status == AnimationStatus.completed) test = true;
          });
        });
        controller.forward();
        super.initState();
      }
    
      @override
      void dispose() {
        controller.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Padding(
            padding: const EdgeInsets.fromLTRB(30,50,10,30),
            child: Column(
              children: [
                const Text('Hello, Welcome', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 60),),
                LinearProgressIndicator(
                  value: controller.value,
                  semanticsLabel: 'Linear progress indicator',
                ),
                if(test == true) const Text('Test is true')
              ],
            )
          ),
        );
      }

【问题讨论】:

  • "[...] 但没有发生线性动画" - 这是因为您的 build 方法不会在每个控制器滴答声后被调用 - 这样做你例如,可以在控制器上添加监听器(而不仅仅是状态监听器)
  • @pskink 你能把代码作为答案吗?只是想看看我如何添加监听器和状态监听器。
  • ..addListener(() =&gt; setState(() {}))
  • 它不起作用,您在之前的评论中对 addListener 和 addStatusListener 说,我们如何添加两者。请清楚,如果您将代码作为答案发布,非常感谢。
  • 只需在..addStatusListener(...) 之后添加上面的代码 - 看起来:controller = AnimationController(...)..addStatusListener(...)..addListener(...)

标签: flutter flutter-animation


【解决方案1】:

没有什么变化,因为您不是在听每一个滴答声,您只是在完成时才听,正如 pskink 试图在 cmets 中解释的那样。

 void initState() {
        controller = AnimationController(
          vsync: this,
          duration: const Duration(seconds: 5),
        )
          ..addStatusListener((AnimationStatus status) {
            setState(() {
              if (status == AnimationStatus.completed) test = true;
            });
          })
          ..addListener(() {
            setState(() {});
          });
        controller.forward();
        super.initState();
      }

【讨论】:

  • 感谢 Yasine,我非常感谢代码。作为一个新手,只有实际的代码对我来说才有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
相关资源
最近更新 更多