Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 更改选项卡时继续计时_Dart_Flutter - Fatal编程技术网

Dart 更改选项卡时继续计时

Dart 更改选项卡时继续计时,dart,flutter,Dart,Flutter,我有一个基于时间的应用程序,其中我在一个选项卡上显示计时器(倒计时),在下一个选项卡上显示一些文章 现在,用户可以自由阅读文章,直到计时器滴答作响。 但当用户更改选项卡时,计时器将重置 当状态改变时,我不会重建计时器。它作为静态变量放置,并且只初始化一次 任何帮助都将不胜感激 总结我迄今为止所做的工作: 1) 创建了一个静态变量 静态变量计时器=倒计时() 2) 用于创建listtile。 ListTile(…,尾随:Classname.timer) 3) 启动计时器。 定时器工作正常 4) 换

我有一个基于时间的应用程序,其中我在一个选项卡上显示计时器(倒计时),在下一个选项卡上显示一些文章

现在,用户可以自由阅读文章,直到计时器滴答作响。 但当用户更改选项卡时,计时器将重置

当状态改变时,我不会重建计时器。它作为静态变量放置,并且只初始化一次

任何帮助都将不胜感激

总结我迄今为止所做的工作:

1) 创建了一个静态变量 静态变量计时器=倒计时()

2) 用于创建listtile。 ListTile(…,尾随:Classname.timer)

3) 启动计时器。 定时器工作正常

4) 换了一页。 可以看到计时器仍在运行(通过放置在ProgressPainter中的print语句)

5) 计时器没有变化

class CountdownTimer extends StatefulWidget{
  var timer = CountdownTimerState();

  @override
  State<StatefulWidget> createState() {
    timer = CountdownTimerState();
//  timer._controller.duration =
    return timer;
  }
}

class CountdownTimerState extends State<CountdownTimer> with TickerProviderStateMixin{
  AnimationController _controller;

  String get timeRemaining{
    Duration duration = _controller.duration * _controller.value;
    return '${duration.inMinutes} : ${(duration.inSeconds % 60).toString().padLeft(2,'0')}';
  }


  startTimer(){
    print('called');
    _controller.reverse(from: 1);
  }

  stopTimer(){
    _controller.reverse(from: 0);
  }
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this,duration: const Duration(seconds: 180));
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 60,
      height: 60,
      child: Align(alignment: FractionalOffset.center,child: AspectRatio(aspectRatio: 1.0,child: Stack(
        children: <Widget>[
          Positioned.fill(child: AnimatedBuilder(animation: _controller, builder: (BuildContext context,Widget child){
            return CustomPaint(
              painter: ProgressPainter(animation: _controller, backgroundColor: Colors.purple, color: Colors.white54),
            );
          })),
          Align(
            alignment: FractionalOffset.center,
            child: AnimatedBuilder(animation: _controller, builder: (BuildContext context,Widget child){
              return Text(timeRemaining,style: TextStyle(fontSize: 20,fontWeight: FontWeight.w400,color: Colors.white),);
            })
          )
        ],
      ),),),
    );
  }
}


class ProgressPainter extends CustomPainter {
  ProgressPainter({
    @required this.animation,
    @required this.backgroundColor,
    @required this.color,
  }) : super(repaint: animation);

  /// Animation representing what we are painting
  final Animation<double> animation;

  /// The color in the background of the circle
  final Color backgroundColor;

  /// The foreground color used to indicate progress
  final Color color;

  @override
  void paint(Canvas canvas, Size size) {
    print('Timer is running');
    Paint paint = new Paint()
      ..color = backgroundColor
      ..strokeWidth = 1.0
      ..strokeCap = StrokeCap.butt
      ..style = PaintingStyle.fill;
    canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
    paint.color = color;
    double progressRadians = (1.0 - animation.value) * 2 * math.pi;
    canvas.drawArc(
        Offset.zero & size, math.pi * 1.5, -progressRadians, false, paint);
  }

  @override
  bool shouldRepaint(ProgressPainter other) {
    return animation.value != other.animation.value ||
        color != other.color ||
        backgroundColor != other.backgroundColor;
  }
}
类倒计时扩展StatefulWidget{
var timer=CountdownTimerState();
@凌驾
状态createState(){
计时器=倒计时状态();
//计时器。_控制器。持续时间=
返回计时器;
}
}
类CountdownTimerState使用TickerProviderStateMixin扩展状态{
动画控制器_控制器;
字符串获取剩余时间{
持续时间=_controller.Duration*_controller.value;
返回${duration.inMinutes}:${(duration.inSeconds%60).toString().padLeft(2,'0')};
}
startTimer(){
打印(‘被调用’);
_控制器。反转(从:1开始);
}
停止计时器(){
_控制器。反转(从:0开始);
}
@凌驾
void initState(){
super.initState();
_controller=AnimationController(vsync:this,duration:const duration(秒:180));
}
@凌驾
小部件构建(构建上下文){
返回容器(
宽度:60,
身高:60,
子级:对齐(对齐:分数偏移。中心,子级:AspectRatio(AspectRatio:1.0,子级:堆栈(
儿童:[
fill(子项:AnimatedBuilder(动画:\控制器,生成器:(BuildContext上下文,小部件子项){
返回自定义油漆(
画家:ProgressPainter(动画:控制器,背景色:Colors.purple,color:Colors.white54),
);
})),
对齐(
对齐:分馏loffset.center,
子:AnimatedBuilder(动画:\控制器,生成器:(BuildContext上下文,小部件子对象){
返回文本(剩余时间,样式:TextStyle(fontSize:20,fontWeight:fontWeight.w400,颜色:Colors.white));
})
)
],
),),),
);
}
}
类ProgressPainter扩展了CustomPainter{
进步画家({
@需要这个动画,
@需要这个。背景色,
@需要这个颜色,
}):super(重新绘制:动画);
///表示我们正在绘制的内容的动画
最终动画;
///圆圈背景中的颜色
最终颜色背景色;
///用于指示进度的前景色
最终颜色;
@凌驾
空心油漆(帆布,尺寸){
打印(“计时器正在运行”);
油漆=新油漆()
…颜色=背景颜色
..冲程宽度=1.0
..strokeCap=strokeCap.butt
..风格=绘画风格。填充;
画布.画圈(大小.中心(偏移.零),大小.宽度/2.0,绘制);
paint.color=颜色;
double progressRadians=(1.0-animation.value)*2*math.pi;
帆布拉弧(
偏移量0和尺寸,数学pi*1.5,-弧度,假,油漆);
}
@凌驾
bool应重新喷漆(其他){
返回animation.value!=其他.animation.value||
颜色||
backgroundColor!=其他.backgroundColor;
}
}

创建一个timer\u bloc.dart文件,并在其中包含以下bloc:

final timerBloc = TimerBloc();

class TimerBloc {
  Timer _timer;

  int _start = 10;

  StreamController<int> timerStreamController = StreamController<int>.broadcast();

  Stream get timerStream {
    if(!timerStreamController.hasListener){
      startTimer();
    }
    return timerStreamController.stream;
  }


  void startTimer() {
    const oneSec = const Duration(seconds: 1);
    _timer = new Timer.periodic(oneSec, (Timer timer) {
      if (_start < 1) {
        timer.cancel();
      } else {
        _start = _start - 1;
      }
      timerStreamController.sink.add(_start);
    });
  }

  stopTimer() {
    _timer?.cancel();
  }

  dispose() {
    timerStreamController.close();
  }
}
return StreamBuilder(
  stream: timerBloc.timerStream,
  builder: (context, snapshot) {
  return Text(snapshot.data.toString());
},);