【发布时间】:2021-08-11 22:09:04
【问题描述】:
我有一个页面,我想向用户显示他们在页面上的经过时间,类似于 Forest 应用程序为其番茄方法所做的事情。然而,我的不是倒计时,而是倒计时。
我无法让计时器在页面加载时启动。此外,一旦用户退出页面,我想让计时器停止并打印经过的时间。并将经过的时间数据传递到后端或其他页面。使用的包是https://pub.dev/packages/simple_timer
如何才能最好地实现这一目标?谢谢各位专家!
class TimerPage extends StatefulWidget {
@override
_TimerPageState createState() => _TimerPageState();
}
class _TimerPageState extends State<TimerPage>
with SingleTickerProviderStateMixin {
TimerController _timerController;
TimerProgressTextCountDirection _progressTextCountDirection =
TimerProgressTextCountDirection.count_up;
@override
void initState() {
_timerController = TimerController(this);
_timerController.start();
_timerController.forward(from: 0.0);
_timerController.addListener(_printLatestValue);
super.initState();
}
_printLatestValue() {
print("${_timerController.duration}");
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xff04072E),
appBar: AppBar(
title: Text('Aloha', style: TextStyle(color: Colors.white)),
backgroundColor: Colors.black,
),
body: WillPopScope(child: SafeArea(
child: SingleChildScrollView(
child: Column(children: <Widget>[
Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: SimpleTimer(
controller: _timerController,
duration: const Duration(seconds: 0),
displayProgressIndicator: false,
backgroundColor: Colors.grey,
progressTextStyle: TextStyle(color: Colors.white, fontSize: 60),
),
),
ElevatedButton( onPressed: () {
_printLatestValue();
Navigator.pushReplacement<void, void>(
context,
MaterialPageRoute<void>(
builder: (BuildContext context) => MainPage()));
},child: Text('Next'))
]),
),
),
),
);
}
}
插入后出现的错误
_timerController.start();
_timerController.forward(from: 0.0);
【问题讨论】:
标签: flutter timer navigation state init