Flutter小部件生命周期警告/动画小部件异常

wbgh16ku  于 2023-02-13  发布在  Flutter
关注(0)|答案(1)|浏览(141)

我试图建立一个简单的加载器动画内Flutter使用自定义动画,利用动画控制器和吐温方法。动画工作正常的手机,但我得到了一个无休止的警告消息列表,国家:

Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4586 pos 12: '_lifecycleState != _ElementLifecycle.defunct': is not true.

我不知道这是什么。如果我能得到关于这个警告的解释就太好了。
下面是我的加载器动画类代码供参考:

class Loader extends StatefulWidget {
  const Loader({super.key});

  @override
  State<Loader> createState() => _LoaderState();
}

class _LoaderState extends State<Loader> with TickerProviderStateMixin {
  late AnimationController controller;

  late Animation<double> animation_rotation;
  late Animation<double> animation_Radius_in;
  late Animation<double> animation_Radius_out;

  final double idistance = 60;

  double distance = 0;

  @override
  void initState() {
    super.initState();

    controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 2),
    );

    animation_rotation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(
        parent: controller,
        curve: const Interval(0.0, 1.0, curve: Curves.linear),
      ),
    );

    animation_Radius_in = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(
        parent: controller,
        curve: const Interval(0.75, 1.0, curve: Curves.easeInOut),
      ),
    );
    animation_Radius_out = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: controller,
        curve: const Interval(0.0, 0.25, curve: Curves.easeInOut),
      ),
    );

    controller.addListener(() {
      if (mounted) {
        setState(() {
          if (controller.value >= 0.75 && controller.value <= 1.0) {
            distance = animation_Radius_in.value * idistance;
          } else if (controller.value >= 0.0 && controller.value <= 0.25) {
            distance = animation_Radius_out.value * idistance;
          }
        });
      }
    });

    controller.repeat();
  }

谢谢你。

4xrmg8kj

4xrmg8kj1#

我不完全确定这是不是原因,请尝试一下。即使它不能解决您的问题,强烈建议根据小部件的生命周期删除侦听器和处理控制器。
为此,为侦听器创建一个单独的函数(以便能够删除它),并向dispose方法添加一个覆盖:

class _LoaderState extends State<Loader> with TickerProviderStateMixin {
  late AnimationController controller;

  late Animation<double> animation_rotation;
  late Animation<double> animation_Radius_in;
  late Animation<double> animation_Radius_out;

  final double idistance = 60;

  double distance = 0;
  
  // move the listener logic here
  void _listener() {
    if (mounted) {
      setState(() {
        if (controller.value >= 0.75 && controller.value <= 1.0) {
          distance = animation_Radius_in.value * idistance;
        } else if (controller.value >= 0.0 && controller.value <= 0.25)             distance = animation_Radius_out.value * idistance;
        }
      });
    }
  }

  // get rid of the listener and controller
  @override
  void dispose() {
    controller.removeListener(_listener);
    controller.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 2),
    );

    animation_rotation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(
        parent: controller,
        curve: const Interval(0.0, 1.0, curve: Curves.linear),
      ),
    );

    animation_Radius_in = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(
        parent: controller,
        curve: const Interval(0.75, 1.0, curve: Curves.easeInOut),
      ),
    );
    animation_Radius_out = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: controller,
        curve: const Interval(0.0, 0.25, curve: Curves.easeInOut),
      ),
    );

    controller.addListener(_listener);

    controller.repeat();
  }

相关问题