0

我有一个 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。

4

2 回答 2

0

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

Widget(key: uniqueKey(), ...)
于 2020-09-15T07:36:53.120 回答
0

您可以在下面复制粘贴运行完整代码
为了允许下面的工作演示工作,您需要从https://github.com/aliyigitbireroglu/flutter-image-sequence-animator/tree/master/image_sequence_animator/example/assets/ImageSequencepng放置文件 至

assets:
  - assets/ImageSequence/

你不需要使用"assets/images/" + ExcerciselistPojo.randomList[count1].imageUrl
你可以直接使用imageSequenceAnimator.skip(count1.toDouble())

代码片段

  ImageSequenceAnimatorState imageSequenceAnimator;

  void onReadyToPlay(ImageSequenceAnimatorState _imageSequenceAnimator) {
    imageSequenceAnimator = _imageSequenceAnimator;
  }

  void onPlaying(ImageSequenceAnimatorState _imageSequenceAnimator) {
    setState(() {});
  }

  void _gotoB() async {
    String parameter = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => RouteB()),
    );

    setState(() {
      count1 = int.tryParse(parameter);
      imageSequenceAnimator.skip(count1.toDouble());
    });
  }

工作演示

在此处输入图像描述

完整代码

import 'package:flutter/material.dart';
import 'package:intervalprogressbar/intervalprogressbar.dart';
import 'package:image_sequence_animator/image_sequence_animator.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int count = 30;
  int count1 = 3;

  ImageSequenceAnimatorState imageSequenceAnimator;

  void onReadyToPlay(ImageSequenceAnimatorState _imageSequenceAnimator) {
    imageSequenceAnimator = _imageSequenceAnimator;
  }

  void onPlaying(ImageSequenceAnimatorState _imageSequenceAnimator) {
    setState(() {});
  }

  void _gotoB() async {
    String parameter = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => RouteB()),
    );

    setState(() {
      count1 = int.tryParse(parameter);
      imageSequenceAnimator.skip(count1.toDouble());
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              flex: 4,
              child: Padding(
                padding: EdgeInsets.all(25),
                child: ImageSequenceAnimator(
                  "assets/ImageSequence",
                  "Frame_",
                  0,
                  5,
                  "png",
                  60,
                  isAutoPlay: false,
                  color: Colors.blue,
                  onReadyToPlay: onReadyToPlay,
                  onPlaying: onPlaying,
                ),
              ),
            ),
            Expanded(
              flex: 2,
              child: Container(
                  margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
                  child: IntervalProgressBar(
                      direction: IntervalProgressDirection.horizontal,
                      max: count,
                      progress: count1,
                      intervalSize: 2,
                      size: Size(600, 10),
                      highlightColor: Colors.pink,
                      defaultColor: Colors.grey,
                      intervalColor: Colors.transparent,
                      intervalHighlightColor: Colors.transparent,
                      reverse: false,
                      radius: 0)),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _gotoB,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class RouteB extends StatefulWidget {
  @override
  _RouteBState createState() => _RouteBState();
}

class _RouteBState extends State<RouteB> {
  TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          body: Column(
        children: [
          TextField(
            controller: _textEditingController,
          ),
          RaisedButton(
            child: Text('Go back'),
            onPressed: () {
              Navigator.pop(context, _textEditingController.text);
            },
          ),
        ],
      )),
    );
  }
}
于 2020-09-15T08:30:36.700 回答