栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Flutter中实现永久秒表?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Flutter中实现永久秒表?

这是一个最小的工作解决方案。关键点:

  • 介绍
    TimerService
    隔离计时器功能的类
  • TimerService
    实现
    ChangeNotifier
    ,您可以订阅以接收更改。
  • 一个
    InheritedWidget
    用于提供服务给您的应用程序的所有部件。这个继承的小部件包装了您的应用小部件。
  • AnimatedBuilder
    用于接收来自的更改
    ChangeNotifier
    。订阅是自动处理的(无手动
    addListener
    /
    removeListener
    )。

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'),     )   ], );          },        ),      ),    );  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418556.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号