Flutter 颤振(你的生命周期)_ElementLifecycle.defunct';:事实并非如此

Flutter 颤振(你的生命周期)_ElementLifecycle.defunct';:事实并非如此,flutter,Flutter,我正在我的应用程序中显示一些动画自定义画师进度条。它显示了一些错误 Error: The following assertion was thrown while notifying listeners for AnimationController: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4263 pos 12: '_debugLifecycleState != _ElementLifecycl

我正在我的应用程序中显示一些动画自定义画师进度条。它显示了一些错误

Error: The following assertion was thrown while notifying listeners for AnimationController:
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4263 pos 12: '_debugLifecycleState != _ElementLifecycle.defunct': is not true.
我有一个简单的主页,其中有一个导航栏。当导航像这样改变时,我只是简单地显示容器

SingleChildScrollView(
            child: Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage("images/sidebg.png"),
                    fit: BoxFit.cover,
                  ),
                ),
                child: Column(
                  children: [
                    pageIndex == 0 ? DashboardScreen() : Container(),
                    pageIndex == 1 ? MapScreen() : Container(),
                    pageIndex == 3 ? ServiceCenter() : Container(),
                    pageIndex == 4 ? ProfileScreen() : Container(),
                  ],
                )),
          ),
问题是,当索引发生变化时,您可以看到页面发生了变化,但当我改变页面时,它会显示一个错误,正如我在上面的continuesly循环中所提到的,不会停止

如果我去掉这个进度指示器,一切都正常

这是进度指示器屏幕

import 'dart:math' as math;

import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:sleek_circular_slider/sleek_circular_slider.dart';
import 'package:liquid_progress_indicator/liquid_progress_indicator.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:smooth_star_rating/smooth_star_rating.dart';

class DashboardScreen extends StatefulWidget {
  @override
  _DashboardScreenState createState() => _DashboardScreenState();
}

class _DashboardScreenState extends State<DashboardScreen> {
  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    return Container(
      child: Column(
        children: [
          SizedBox(
            height: height * 0.05,
          ),
          Circular_arc(),

        ],
      ),
    );
  }
}

final Gradient gradient = new LinearGradient(
  colors: <Color>[
    Colors.greenAccent.withOpacity(1.0),
    Colors.yellowAccent.withOpacity(1.0),
    Colors.redAccent.withOpacity(1.0),
  ],
  stops: [
    0.0,
    0.5,
    1.0,
  ],
);

class Circular_arc extends StatefulWidget {
  const Circular_arc({
    Key key,
  }) : super(key: key);

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

class _Circular_arcState extends State<Circular_arc>
    with SingleTickerProviderStateMixin {
  Animation<double> animation;
  AnimationController animController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    animController =
        AnimationController(duration: Duration(seconds: 3), vsync: this);

    final curvedAnimation =
        CurvedAnimation(parent: animController, curve: Curves.easeInOutCubic);

    animation = Tween<double>(begin: 0.0, end: 2).animate(curvedAnimation)
      ..addListener(() {
        setState(() {});
      });
    animController.repeat(max: 1);
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return Container(
      child: Stack(
        children: [
          CustomPaint(
            size: Size(300, 300),
            painter: ProgressArc(null, Colors.black54, true),
          ),

          CustomPaint(
            size: Size(300, 300),
            painter: ProgressArc(animation.value, Colors.redAccent, false),
          ),
          Positioned(
            top: height * 0.07,
            left: width * 0.2,
            child: Column(
              children: [
                Image.asset(
                  'images/star-icon-fill@3x.png',
                  height: height * 0.045,
                ),
                RichText(
                  text: new TextSpan(
                    // Note: Styles for TextSpans must be explicitly defined.
                    // Child text spans will inherit styles from parent
                    style: new TextStyle(
                      fontSize: 14.0,
                      color: Colors.black,
                    ),
                    children: <TextSpan>[
                      new TextSpan(
                          text: '4.6',
                          style: new TextStyle(
                              fontSize: 40, fontFamily: 'UbuntuRegular')),
                      new TextSpan(
                          text: ' /5',
                          style: TextStyle(
                              fontSize: 25,
                              color: Colors.grey[400],
                              fontFamily: 'UbuntuRegular')),
                    ],
                  ),
                ),
                Text(
                  'FIFTEEN DAYS SCORE',
                  style: TextStyle(
                      color: Colors.grey[400], fontFamily: 'UbuntuMedium'),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}

class ProgressArc extends CustomPainter {
  bool isBackground;
  double arc;
  Color progressColor;

  ProgressArc(this.arc, this.progressColor, this.isBackground);

  @override
  void paint(Canvas canvas, Size size) {
    final rect = Rect.fromLTRB(0, 0, 300, 300);
    final startAngle = -math.pi;
    final sweepAngle = arc != null ? arc : math.pi;
    final userCenter = false;
    final paint = Paint()
      ..strokeCap = StrokeCap.round
      ..color = progressColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = 10;

    if (!isBackground) {
      paint.shader = gradient.createShader(rect);
    }
    canvas.drawArc(rect, startAngle, sweepAngle, userCenter, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}
导入'dart:math'作为数学;
导入“包:弧形导航栏/弧形导航栏.省道”;
导入“包装:圆滑滑滑条/圆滑滑滑条.省道”;
导入“包装:液体进度指示器/液体进度指示器.dart”;
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
进口“包装:颤振等级/颤振等级.省道”;
进口“包装:光滑星型/光滑星型.省道”;
类DashboardScreen扩展StatefulWidget{
@凌驾
_仪表板屏幕状态createState()=>U仪表板屏幕状态();
}
类_仪表板屏幕状态扩展状态{
@凌驾
小部件构建(构建上下文){
double width=MediaQuery.of(context).size.width;
double height=MediaQuery.of(context).size.height;
返回容器(
子:列(
儿童:[
大小盒子(
高度:高度*0.05,
),
圆弧(),
],
),
);
}
}
最终梯度=新的线性梯度(
颜色:[
颜色。绿色调。不透明度(1.0),
颜色。黄色调。不透明度(1.0),
颜色。红色调。不透明度(1.0),
],
停止:[
0.0,
0.5,
1.0,
],
);
类圆弧扩展了StatefulWidget{
恒圆弧({
关键点,
}):super(key:key);
@凌驾
_循环_圆弧状态createState()=>_循环_圆弧状态();
}
类_循环_弧状态扩展状态
使用SingleTickerProviderStateMixin{
动画;
动画控制器;
@凌驾
void initState(){
//TODO:实现initState
super.initState();
动物控制器=
AnimationController(持续时间:持续时间(秒数:3),vsync:this);
最终弯曲度=
曲线动画(父对象:animController,曲线:Curves.easeInOutCubic);
动画=Tween(开始:0.0,结束:2)。动画(曲线动画)
…addListener(){
setState((){});
});
动画控制器。重复(最大值:1);
}
@凌驾
小部件构建(构建上下文){
double width=MediaQuery.of(context).size.width;
double height=MediaQuery.of(context).size.height;
返回容器(
子:堆栈(
儿童:[
定制油漆(
尺寸:尺寸(300300),
画家:ProgressArc(空,颜色。黑色54,真),
),
定制油漆(
尺寸:尺寸(300300),
画师:ProgressArc(animation.value、Colors.redAccent、false),
),
定位(
顶部:高度*0.07,
左:宽度*0.2,
子:列(
儿童:[
影像资产(
'图像/星星图标-fill@3x.png',
高度:高度*0.045,
),
RichText(
text:newtextspan(
//注意:必须明确定义TextSpans的样式。
//子文本跨距将从父文本跨距继承样式
样式:新文本样式(
字体大小:14.0,
颜色:颜色,黑色,
),
儿童:[
新文本跨度(
正文:“4.6”,
样式:新文本样式(
fontSize:40,fontFamily:“UbuntuRegular”),
新文本跨度(
文本:'/5',
样式:TextStyle(
尺寸:25,
颜色:颜色。灰色[400],
fontFamily:“UbuntuRegular”),
],
),
),
正文(
“十五天得分”,
样式:TextStyle(
颜色:Colors.gray[400],fontFamily:“UbuntuMedium”),
)
],
),
)
],
),
);
}
}
类ProgressArc扩展了CustomPainter{
布尔背景;
双圆弧;
颜色;
ProgressArc(this.arc,this.progressColor,this.isBackground);
@凌驾
空心油漆(帆布,尺寸){
final rect=rect.fromLTRB(0,0,300,300);
最终startAngle=-math.pi;
最终扫掠角度=弧!=零弧:math.pi;
最终用户中心=false;
最终油漆=油漆()
..strokeCap=strokeCap.round
…颜色=颜色
…风格=绘画风格笔划
..冲程宽度=10;
如果(!isBackground){
paint.shader=gradient.createShader(矩形);
}
画布.drawArc(rect、startAngle、sweepAngle、userCenter、paint);
}
@凌驾
布尔应重新绘制(协变自定义代理){
//TODO:实现应重新绘制
返回true;
}
}

错误在连续循环中,它不会停止。当我更改页面索引时,它也会开始(意思是当我从主页更改导航时)。

在您的
\u circularcstate
中,请

  • 调用
    animController.forward()
    动画控制器之后。重复(最大值:1)
  • 要保存循环状态,请在循环状态中添加mixin
    automatickepaliveclientmixin
    。然后重写
    wantKeepAlive
    并返回
    true
    。另外,调用
    super.build(上下文)内部
    构建(…)
  • import'dart:math'
    
    import 'dart:math' as math;
    import 'dart:ui';
    
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: PageView(
              scrollDirection: Axis.vertical,
              children: [
                DashboardScreen(),
                Container(color: Colors.orange),
                Container(color: Colors.blue),
                Container(color: Colors.green),
              ],
            ),
          ),
        );
      }
    }
    
    class DashboardScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        double width = MediaQuery.of(context).size.width;
        double height = MediaQuery.of(context).size.height;
    
        return Container(
          child: Column(
            children: [
              SizedBox(
                height: height * 0.05,
              ),
              CircularArc(),
            ],
          ),
        );
      }
    }
    
    final Gradient gradient = new LinearGradient(
      colors: <Color>[
        Colors.greenAccent.withOpacity(1.0),
        Colors.yellowAccent.withOpacity(1.0),
        Colors.redAccent.withOpacity(1.0),
      ],
      stops: [0.0, 0.5, 1.0],
    );
    
    class CircularArc extends StatefulWidget {
      const CircularArc({
        Key key,
      }) : super(key: key);
    
      @override
      _CircularArcState createState() => _CircularArcState();
    }
    
    class _CircularArcState extends State<CircularArc>
        with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
      double width;
      double height;
      Animation<double> animation;
    
      @override
      void initState() {
        super.initState();
    
        final AnimationController animController =
            AnimationController(duration: Duration(seconds: 3), vsync: this);
        final curvedAnimation =
            CurvedAnimation(parent: animController, curve: Curves.easeInOutCubic);
        animation = Tween<double>(begin: 0.0, end: 2).animate(curvedAnimation)
          ..addListener(() {
            setState(() {});
          });
        animController.repeat(max: 1);
        animController.forward();
      }
    
      @override
      bool get wantKeepAlive => true;
    
      @override
      Widget build(BuildContext context) {
        super.build(context);
    
        if (width == null && height == null) {
          width = MediaQuery.of(context).size.width;
          height = MediaQuery.of(context).size.height;
        }
    
        return Container(
          child: Stack(
            children: [
              CustomPaint(
                size: Size(300, 300),
                painter: ProgressArc(null, Colors.black54, true),
              ),
              CustomPaint(
                size: Size(300, 300),
                painter: ProgressArc(animation.value, Colors.redAccent, false),
              ),
              Positioned(
                top: height * 0.07,
                left: width * 0.2,
                child: Column(
                  children: [
                    // Image.asset(
                    //   'images/star-icon-fill@3x.png',
                    //   height: height * 0.045,
                    // ),
                    RichText(
                      text: new TextSpan(
                        // Note: Styles for TextSpans must be explicitly defined.
                        // Child text spans will inherit styles from parent
                        style: new TextStyle(
                          fontSize: 14.0,
                          color: Colors.black,
                        ),
                        children: <TextSpan>[
                          new TextSpan(
                              text: '4.6',
                              style: new TextStyle(
                                  fontSize: 40, fontFamily: 'UbuntuRegular')),
                          new TextSpan(
                              text: ' /5',
                              style: TextStyle(
                                  fontSize: 25,
                                  color: Colors.grey[400],
                                  fontFamily: 'UbuntuRegular')),
                        ],
                      ),
                    ),
                    Text(
                      'FIFTEEN DAYS SCORE',
                      style: TextStyle(
                          color: Colors.grey[400], fontFamily: 'UbuntuMedium'),
                    )
                  ],
                ),
              )
            ],
          ),
        );
      }
    }
    
    class ProgressArc extends CustomPainter {
      bool isBackground;
      double arc;
      Color progressColor;
    
      ProgressArc(this.arc, this.progressColor, this.isBackground);
    
      @override
      void paint(Canvas canvas, Size size) {
        final rect = Rect.fromLTRB(0, 0, 300, 300);
        final startAngle = -math.pi;
        final sweepAngle = arc != null ? arc : math.pi;
        final userCenter = false;
        final paint = Paint()
          ..strokeCap = StrokeCap.round
          ..color = progressColor
          ..style = PaintingStyle.stroke
          ..strokeWidth = 10;
    
        if (!isBackground) {
          paint.shader = gradient.createShader(rect);
        }
        canvas.drawArc(rect, startAngle, sweepAngle, userCenter, paint);
      }
    
      @override
      bool shouldRepaint(covariant CustomPainter oldDelegate) {
        // TODO: implement shouldRepaint
        return true;
      }
    }