import 'dart:async';
import 'package:Flutter/material.dart';
void main() {
final timerService = TimerService();
runApp(
TimerServiceProvider( // provide timer service to all widgets of your app
service: timerService,
child: MyApp(),
),
);
}
class TimerService extends ChangeNotifier {
Stopwatch _watch;
Timer _timer;
Duration get currentDuration => _currentDuration;
Duration _currentDuration = Duration.zero;
bool get isRunning => _timer != null;
TimerService() {
_watch = Stopwatch();
}
void _onTick(Timer timer) {
_currentDuration = _watch.elapsed;
// notify all listening widgets
notifyListeners();
}
void start() {
if (_timer != null) return;
_timer = Timer.periodic(Duration(seconds: 1), _onTick);
_watch.start();
notifyListeners();
}
void stop() {
_timer?.cancel();
_timer = null;
_watch.stop();
_currentDuration = _watch.elapsed;
notifyListeners();
}
void reset() {
stop();
_watch.reset();
_currentDuration = Duration.zero;
notifyListeners();
}
static TimerService of(BuildContext context) {
var provider = context.inheritFromWidgetOfExactType(TimerServiceProvider) as TimerServiceProvider;
return provider.service;
}
}
class TimerServiceProvider extends InheritedWidget {
const TimerServiceProvider({Key key, this.service, Widget child}) : super(key: key, child: child);
final TimerService service;
@override
bool updateShouldNotify(TimerServiceProvider old) => service != old.service;
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Service Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
var timerService = TimerService.of(context);
return Scaffold(
appBar: AppBar(),
body: Center(
child: AnimatedBuilder(
animation: timerService, // listen to ChangeNotifier
builder: (context, child) {
// this part is rebuilt whenever notifyListeners() is called
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Elapsed: ${timerService.currentDuration}'),
RaisedButton(
onPressed: !timerService.isRunning ? timerService.start : timerService.stop,
child: Text(!timerService.isRunning ? 'Start' : 'Stop'),
),
RaisedButton(
onPressed: timerService.reset,
child: Text('Reset'),
)
],
);
},
),
),
);
}
}
如何在Flutter中实现永久秒表?
如何在Flutter中实现永久秒表?
推荐问题
分类汇总
- (2)
- .net(5)
- Access(210)
- android(1)
- android-studio(1)
- angular(1)
- bash(1)
- c(1)
- c#(625)
- chrome-devtools(1)
- CSS(782)
- css3动画(1)
- docker(1)
- docker-compose(2)
- dotnet(477)
- echarts5.0(1)
- elasticsearch(2)
- element-ui(1)
- eslint(1)
- eventbus(1)
- ffmpeg(2)
- fiddler(1)
- flask(1)
- flutter(1)
- git(2)
- Go(2093)
- golang(9)
- gradle(1)
- harmonyos(4)
- ios(1)
- java(7682)
- javascript(1221)
- Jave(256)
- JS(330)
- jwt(1)
- kafka(1)
- linux(1)
- lua(1)
- matlab(1)
- mongodb(192)
- MySQL(2516)
- nestjs(1)
- nginx(1)
- Node(262)
- node.js(3)
- Oracle(458)
- php(1213)
- player(1)
- Postgres(167)
- ppt(1)
- python(11274)
- react.js(6)
- redis(2)
- rollup(1)
- seata(1)
- sequelize(1)
- sniffer(1)
- Solr(23)
- springboot(1)
- SQL(118)
- SQLServer(5624)
- Swift(224)
- sybase(21)
- typescript(5)
- uniapp(1)
- uni-app(1)
- vant-weapp(1)
- visual-studio-code(1)
- vue.js(12)
- vue3(3)
- vuex(1)
- wasm(1)
- webpack(1)
- 笔记本电脑(1)
- 调试技巧(1)
- 公众号(1)
- 机器学习(1)
- 计算机(1)
- 爬虫(1)
- 其他(33505)
- 前端(16)
- 算法(2)
- 小程序(3)
- 虚拟机(1)
- 运维(1)