【问题标题】:Flutter animation doesn't render颤振动画不渲染
【发布时间】:2021-05-25 17:09:51
【问题描述】:

这是创建一个脉动圈的尝试。它不起作用。此外,当我尝试退出它所在的页面时,它会给出错误:“ 'package:flutter/src/widgets/framework.dart': 断言失败: line 5098 pos 14: '_dependents.isEmpty': is not true."。这个小部件可能有什么问题?

    import 'package:flutter/material.dart';
    import 'package:flutter/animation.dart';
    
    class BlinkingCircle extends StatefulWidget {
      BlinkingCircle({Key key, @required this.size}) : super(key: key);
      final size;
      @override
      _BlinkingCircleState createState() => _BlinkingCircleState();
    }
    
    class _BlinkingCircleState extends State<BlinkingCircle>
        with SingleTickerProviderStateMixin {
      Animation<double> animation;
      AnimationController controller;
    
      @override
      void initState() {
        super.initState();
        controller = AnimationController(
            vsync: this, duration: const Duration(microseconds: 5000));
        animation = Tween<double>(begin: 0, end: 20).animate(controller)
          ..addListener(() {
            setState(() {});
          });
        controller.forward();
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          color: Colors.blue,
          width: animation.value,
          height: animation.value,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
          ),
        );
      }
    
      @override
      void dispose() {
        controller.dispose();
        super.dispose();
      }
    }

【问题讨论】:

    标签: flutter flutter-animation


    【解决方案1】:

    您需要将color: 属性移动到Decoration

          width: animation.value,
          height: animation.value,
          decoration: BoxDecoration(
            color: Colors.blue,
            shape: BoxShape.circle,
          ),
        );
    

    您还需要将microseconds 更改为milliseconds

    【讨论】:

    • 这是什么魔法?!
    • 颜色属性描述说If the [decoration] is used, this property must be null. A background color may still be painted by the [decoration] even if this property is null.
    猜你喜欢
    • 2020-07-17
    • 2021-04-28
    • 2021-06-26
    • 2018-11-17
    • 1970-01-01
    • 2012-08-15
    • 2021-02-21
    • 2018-09-15
    相关资源
    最近更新 更多