1

在动画运行时,我想返回下一页或上一页......但此错误显示为“AnimationController.stop() 在 AnimationController.dispose() 之后调用 AnimationController 方法在调用 dispose 后不应使用。”

请帮我...

          import 'package:flutter/material.dart';
          import 'package:flutter_screenutil/flutter_screenutil.dart';
          import 'package:get/get.dart';
          import 'package:sanduk/utils/app_colors.dart';
          import 'package:sanduk/utils/text_widget.dart';
          import 'dart:math' as math;

          class QuizSearchingPlayers extends StatefulWidget {
            const QuizSearchingPlayers({Key? key}) : super(key: key);

            @override
            _QuizSearchingPlayersState createState() => _QuizSearchingPlayersState();
          }

          class _QuizSearchingPlayersState extends State<QuizSearchingPlayers>
              with SingleTickerProviderStateMixin {
            late AnimationController _controller;

            @override
            void initState() {
              super.initState();
              _controller = AnimationController(
                duration: const Duration(seconds: 2),
                vsync: this,
              )..repeat();

              _controller.addListener(() async {
                await stoppingAnimation();
              });
            }

            Future stoppingAnimation() async {
              await Future.delayed(const Duration(seconds: 10));
              _controller.reset();
              _controller.stop();
              return true;
            }

            @override
            void dispose() {
                _controller.dispose();
              super.dispose();
              
            
            }

            @override
            Widget build(BuildContext context) {
              return SafeArea(
                child: Scaffold(
                  backgroundColor: AppColors.darkThemeBackground,
                  body: Container(
                    height: Get.height,
                    width: Get.width,
                    padding: EdgeInsets.all(20.h),
                    child: Column(
                      children: [
                        TextWidget(
                          "BIOLOGY QUIZ",
                          styles: TextStyles.size18_500,
                          color: AppColors.white,
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10.w),
                          child: CircleAvatar(
                            radius: 150.r,
                            backgroundColor: AppColors.transparent,
                            child: AnimatedBuilder(
                              animation: _controller,
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Stack(
                                  children: [
                                    Align(
                                      alignment: Alignment.center,
                                      child: CircleAvatar(
                                        radius: 120.r,
                                        backgroundColor: AppColors.blue,
                                        child: CircleAvatar(
                                          radius: 115.r,
                                          backgroundColor: AppColors.darkThemeBackground,
                                        ),
                                      ),
                                    ),
                                    Align(
                                      alignment: Alignment.centerLeft,
                                      child: CircleAvatar(
                                          radius: 40.r,
                                          backgroundColor: AppColors.red,
                                          backgroundImage: const AssetImage(
                                              "assets/images/sardar.png")),
                                    ),
                                    Align(
                                        alignment: Alignment.centerRight,
                                        child: CircleAvatar(
                                          radius: 40.r,
                                          backgroundColor: AppColors.darkBlue,
                                          backgroundImage: const AssetImage(
                                              "assets/images/sardar.png"),
                                        )),
                                  ],
                                ),
                              ),
                              builder: (context, child) {
                                return Transform.rotate(
                                  angle: _controller.value * 2 * math.pi,
                                  child: child,
                                );
                              },
                            ),
                          ),
                        ),
                        TextWidget(
                          "Searching For Opponent..",
                          styles: TextStyles.size32_400,
                          color: AppColors.blue,
                          maxLines: 2,
                        ),
                      ],
                    ),
                  ),
                ),
              );
            }
          }

一切正常,直到我导航回其他屏幕......当我在我的停止动画()方法中打印一些行时,它会一直打印我如何停止它?

4

1 回答 1

2

通过将我的方法保持在方法stoppingAnimation之外来解决addlistener

           @override
          void initState() {
            super.initState();
            _controller = AnimationController(
              duration: const Duration(seconds: 2),
              vsync: this,
            )..repeat();

            _controller.addListener(() {
              if (_controller.status == AnimationStatus.dismissed) {
                setState(() {
                  opponentFound = true;
                  _quizAnimationController.countDownTogetReady();
                });
              }
            });

            stoppingAnimation();
          }

          Future stoppingAnimation() async {
            await Future.delayed(const Duration(seconds: 5));
            _controller.reset();
            _controller.stop();
          }

          @override
          void dispose() {
            _controller.dispose();
            super.dispose();
          }

不确定我是否认为 _controller.addListener() 方法正在静默运行,以监听发生的事件,例如在我的停止Animation() 方法中的“打印语句”。这就是打印多次发生的原因。所以我只是在 _controller.addListener() 方法之外使用了 stopingAnimation() 方法并且工作正常。

我是这个颤振社区的新手,所以不确定事情是如何运作的。

于 2021-11-29T07:00:37.387 回答