-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #438 from JisanAR03/sizzle_2
the start and stop api implemented in sizzle
- Loading branch information
Showing
2 changed files
with
314 additions
and
52 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import 'dart:async'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class TimerWidget extends StatefulWidget { | ||
const TimerWidget({Key? key}) : super(key: key); | ||
|
||
@override | ||
_TimerWidgetState createState() => _TimerWidgetState(); | ||
} | ||
|
||
class _TimerWidgetState extends State<TimerWidget> { | ||
late Timer _timer; | ||
Duration _elapsedTime = Duration.zero; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_startTimer(); | ||
} | ||
|
||
void _startTimer() { | ||
_timer = Timer.periodic(Duration(seconds: 1), (timer) { | ||
setState(() { | ||
_elapsedTime += Duration(seconds: 1); | ||
}); | ||
}); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_timer.cancel(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final ThemeData theme = Theme.of(context); | ||
bool isDarkMode = theme.brightness == Brightness.dark; | ||
return Text( | ||
_formatDuration(_elapsedTime), | ||
style: TextStyle( | ||
color: isDarkMode ? Colors.white : Colors.black, fontSize: 16), | ||
); | ||
} | ||
|
||
String _formatDuration(Duration duration) { | ||
String twoDigits(int n) => n.toString().padLeft(2, '0'); | ||
String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60)); | ||
String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60)); | ||
return '${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds'; | ||
} | ||
} |