Created
April 17, 2020 05:23
-
-
Save leecommamichael/0181a40538adcea489b8ea5e0ef55a49 to your computer and use it in GitHub Desktop.
Stopwatch Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:core'; | |
import 'dart:async'; | |
//I don't really need this, but from an analytics perspective it'd be good to know how long of breaks they're taking | |
// TODO(mike): I probably need to ignore time before start | |
class PausableTimer { | |
final Stopwatch _stopwatch; | |
final Duration maxRunningTime; | |
final void Function(Duration) onTick; | |
final void Function() onTimeElapsed; | |
int get secondsLeft => | |
maxRunningTime.inSeconds - _stopwatch.elapsed.inSeconds; | |
bool get _finalTickOfTimer => | |
_stopwatch.elapsed.inSeconds == maxRunningTime.inSeconds + 1; | |
PausableTimer( | |
{@required this.maxRunningTime, | |
@required this.onTick, | |
this.onTimeElapsed}) | |
: _stopwatch = Stopwatch() { | |
Timer.periodic(Duration(seconds: 1), (t) { | |
if (_finalTickOfTimer) { | |
onTick(Duration(seconds: 0)); | |
onTimeElapsed?.call(); | |
_stopwatch.stop(); | |
t.cancel(); | |
} else if (_stopwatch.isRunning) { | |
onTick(Duration(seconds: secondsLeft)); | |
} | |
}); | |
} | |
void toggle() { | |
if (_stopwatch.isRunning) | |
_stopwatch.stop(); | |
else if (secondsLeft != 0) _stopwatch.start(); | |
} | |
void start() { | |
_stopwatch.start(); | |
} | |
void stop() { | |
_stopwatch.stop(); | |
} | |
} | |
class ClockDisplay extends StatefulWidget { | |
final Duration timeToFocus; | |
ClockDisplay({Key key, @required this.timeToFocus}) : super(key: key); | |
@override | |
_ClockDisplayState createState() => _ClockDisplayState(timeLeft: timeToFocus); | |
} | |
class _ClockDisplayState extends State<ClockDisplay> { | |
Duration timeLeft; | |
PausableTimer clockTimer; | |
_ClockDisplayState({@required this.timeLeft}) : super(); | |
double get progressBarPortion => | |
timeLeft.inMilliseconds / widget.timeToFocus.inMilliseconds; | |
int get secondsOnClock => timeLeft.inSeconds - (timeLeft.inMinutes * 60); | |
String get secondsDisplay => '${secondsOnClock < 10 ? 0 : ''}$secondsOnClock'; | |
String get timerDisplay => '${timeLeft.inMinutes}:$secondsDisplay'; | |
void f(Duration d) { | |
setState(() { | |
timeLeft = d; | |
}); | |
} | |
@override | |
void initState() { | |
clockTimer = PausableTimer( | |
maxRunningTime: timeLeft, | |
onTick: f, | |
); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Card( | |
color: Theme.of(context).buttonColor, | |
child: InkWell( | |
child: Column(children: <Widget>[ | |
Text( | |
timerDisplay, | |
style: Theme.of(context).textTheme.display4, | |
), | |
SizedBox( | |
height: 16, | |
child: LinearProgressIndicator( | |
value: progressBarPortion, | |
backgroundColor: Colors.transparent, | |
)) | |
]), | |
onTap: () { | |
clockTimer.toggle(); | |
})); | |
} | |
@override | |
void dispose() { | |
// clockTimer.stop(); | |
// clockTimer = null; | |
super.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment