使用动画和CustomPainter小部件时发生错误

问题描述

你好,我很陌生,正在尝试找出一个错误,但在此问题上我似乎找不到任何东西。下面是我当前的代码:

class Test extends StatefulWidget {
  Test({Key key}) : super(key: key);

  TestState createState() => TestState();
}

class TestState extends State<Test> with SingleTickerProviderStateMixin {
  double circleOneFraction = 0.0;

  Animation<double> circleOneAnimation;
  AnimationController controller;

  @override
  void initState() {
    //width = MediaQuery.of(context).size.width;
    super.initState();
    controller = AnimationController(
        duration: const Duration(milliseconds: 500),vsync: this);
    circleOneAnimation = Tween(begin: 0.0,end: 0.5).animate(controller)
      ..addListener(() {
        setState(() {
          circleOneFraction = circleOneAnimation.value;
        });
      });
    controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: MyPainter(circleOneFraction),child: Container(),willChange: true,isComplex: true,);
  }
}

class MyPainter extends CustomPainter {
  double circleOneFraction;

  MyPainter(this.circleOneFraction);

  @override
  void paint(Canvas canvas,Size size) {
    final heightOfPaint = size.height;
    final widthOfPaint = size.width;

    Paint paint = Paint();

    Path mainBackground = Path();
    mainBackground.addRect(Rect.fromLTRB(0,widthOfPaint,heightOfPaint));
    paint.color = colors.blueColor;
    canvas.drawPath(mainBackground,paint);

    //TEST
    paint.color = Colors.white;
    var circleOneCenter = Offset(0.25 * widthOfPaint,heightOfPaint * 1.1);
    paint.style = PaintingStyle.fill;
    canvas.drawCircle(circleOneCenter,circleOneFraction * widthOfPaint,paint);

    var circleTwoCenter = Offset(0.8 * widthOfPaint,heightOfPaint * 1.1);
    canvas.drawCircle(circleTwoCenter,widthOfPaint * 0.38,paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return null;
  }
}

我想使用CustomPainter小部件在屏幕底部绘制一个扩大的圆圈来制作动画。启动该应用程序时,我每毫秒就会收到一个错误(我认为是刷新背景。我收到的唯一错误消息类型是“断言失败:布尔表达式不能为空”。 我似乎无法用非常无法描述的错误消息找出问题出在哪里。 我希望有人能指出我的错误。

编辑:我确实看到了动画的展开,但是每毫秒都会弹出一个红色错误屏幕。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)