添加 statuslistener 时线性进度指示器上没有动画

No animation on linear progress indicator when adding statuslistener

当我添加 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')
              ],
            )
          ),
        );
      }

没有任何变化,因为你没有听每一个滴答声,你只是在它完成时听,正如 pskink 试图在评论中解释的那样。

 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();
      }