首页 > 解决方案 > 从另一个回来后重新绘制小部件

问题描述

我有一个 MainExerseClass 飞镖课程。其中我有 ImageSequenceAnimator 和 LinearPercentIndicator,当控件通过弹出另一个类到达 MainExerseClass dart 时应该重新启动它们。count1 正在更新,但 ImageSequenceAnimator 未更新。下面是代码。

 class MainExerseClass extends StatefulWidget {
    
    
    
    
      @override
      State<StatefulWidget> createState() {
        // TODO: implement createState
        return Exersise();
    
      }
    }
    
    class Exersise extends State<MainExerseClass> with WidgetsBindingObserver{
      var count1;
      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        if (state == AppLifecycleState.resumed) {
          //do your stuff
          _requestSqlData();
        }
      }
      @override
      void dispose() {
        WidgetsBinding.instance.removeObserver(this);
        super.dispose();
      }
    
      @override
      void initState() {
        // TODO: implement initState
        //count1 = widget.progress;
        _requestSqlData();
    
        super.initState();
      }
    
    
    
      void _requestSqlData() {
        _requestSqlDataAsync();
      }
    
      void _requestSqlDataAsync() async {
        int i = await DatabaseHelper.instance.getDayExcCounter("Day 1");
        setState(() {
          count1 = i;
        });
    
        print(count1);
      }
      void _gotoB() async {
    
        String parameter = await Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => Resttimer(ExcerciselistPojo.randomList[count1].name,count1.toString())),
        );
    
        setState(() {
          count1 = int.tryParse(parameter);
        });
      }
    
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        SizeConfig().init(context);
        return Scaffold(
            body: new Column(
          children: <Widget>[
            new Row(
              children: <Widget>[
                Builder(
                  builder: (context) => IconButton(
                    icon: Icon(Icons.arrow_back),
                    iconSize: 30,
                    onPressed: () {
                      // Here i want context
                      if (Navigator.canPop(context)) {
                        Navigator.pop(context);
                      } else {
                        SystemNavigator.pop();
                      }
                    },
                  ),
                ),
                new Container(
                    margin: const EdgeInsets.fromLTRB(20, 0, 0, 0),
                    child: new Text("Exercise",
                        style: new TextStyle(
                            color: Colors.black,
                            fontSize: 25,
                            fontWeight: FontWeight.bold)))
              ],
            ),
            new Container(
                margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
                child: IntervalProgressBar(
                    direction: IntervalProgressDirection.horizontal,
                    max: ExcerciselistPojo.randomList.length,
                    progress: count1,
                    intervalSize: 2,
                    size: Size(600, 10),
                    highlightColor: Colors.pink,
                    defaultColor: Colors.grey,
                    intervalColor: Colors.transparent,
                    intervalHighlightColor: Colors.transparent,
                    reverse: false,
                    radius: 0)),
            new Container(
              margin: EdgeInsets.fromLTRB(0, 5, 0, 0),
              child: new Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  new Container(
                    height: 30,
                    alignment: Alignment.centerLeft,
                    child: FlatButton(
    
                      child: Image.asset("assets/images/play.webp"),
                      onPressed: () async {
                        //_updatecountertodb();
                        count1--;
                        if(count1<0)
                          count1=0;
                        await DatabaseHelper.instance.insertExcCounter("Day 1", count1);
                        _gotoB();
                      },
                    ),
                  ),
                  new Container(
                    height: 30,
                    alignment: Alignment.centerRight,
                    child: FlatButton(
    
                      child: Image.asset("assets/images/play.webp"),
                      onPressed: () async {
                        //_updatecountertodb();
                        count1++;
                        if(count1>5)
                          count1=0;
                        await DatabaseHelper.instance.insertExcCounter("Day 1", count1);
                        _gotoB();
                      },
                    ),
                  )
                ],
              ),
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
              child: new ImageSequenceAnimator(
                "assets/images/" + ExcerciselistPojo.randomList[count1].imageUrl,
                "Pic_",
                0,
                5,
                "webp",
                3,
                isAutoPlay: true,
                color: null,
                fps: 2,
                isLooping: true,
    
              ),
              height: 300,
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
              alignment: Alignment.centerRight,
              child: IconButton(
                icon: Image.asset("assets/images/rest_time_exc.png"),
                onPressed: () {},
              ),
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(10, SizeConfig.screenHeight /16, 0, 0),
              alignment: Alignment.centerLeft,
              child: new Text(ExcerciselistPojo.randomList[count1].name,
                  style: new TextStyle(
                      fontSize: 25,
                      fontWeight: FontWeight.bold,
                      color: Colors.blueGrey)),
            ),
            new Container(
              margin: EdgeInsets.fromLTRB(0, SizeConfig.screenHeight /44, 0, 0),
              child: new LinearPercentIndicator(
                animation: true,
                animationDuration: 6000,
                lineHeight: SizeConfig.screenHeight / 10,
                percent: 1,
                center: Text("100/68%"),
                linearStrokeCap: LinearStrokeCap.butt,
                progressColor: Colors.pink,
              ),
            )
          ],
        ));
    
    
      }
    
    
    }

我正在使用从第二类导航到 MainExerseClass dart

 onTap: () {
                            Navigator.pop(context, count);
                          },

在 MainExerseClass 计数正在更新但 ImageSequenceAnimator 未刷新它显示旧动画。我想重新启动 LinearPercentIndicator。

标签: flutterwidgetredrawflutter-futurebuilder

解决方案


我相信您的问题将通过将 UniqueKey 传递给您想要再次呈现的小部件来解决,如下所示:

Widget(key: uniqueKey(), ...)

推荐阅读