Created
February 11, 2023 09:17
-
-
Save vijaybheda/1edd256b0bfc9b77025029a65e2e9534 to your computer and use it in GitHub Desktop.
Dart stopwatch
This file contains hidden or 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 'dart:async'; | |
| import 'package:flutter/material.dart'; | |
| void main() { | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Stopwatch app', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: const MyHomePage(title: 'Flutter Stopwatch app'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| const MyHomePage({super.key, required this.title}); | |
| final String title; | |
| @override | |
| State<MyHomePage> createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| Duration _duration = const Duration(); | |
| Timer? _timer; | |
| void addTimeToDuration() { | |
| int totalSec = _duration.inSeconds + 1; | |
| if (totalSec < 0) { | |
| _timer?.cancel(); | |
| } else { | |
| _duration = Duration(seconds: totalSec); | |
| } | |
| setState(() {}); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| body: Center( | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| Text( | |
| getRunningTime(), | |
| style: Theme.of(context) | |
| .textTheme | |
| .headlineMedium | |
| ?.copyWith(color: Colors.black, fontSize: 20), | |
| ), | |
| TextButton( | |
| onPressed: () { | |
| if (_timer == null) { | |
| _timer = | |
| Timer.periodic(const Duration(seconds: 1), (timer) { | |
| return addTimeToDuration(); | |
| }); | |
| } else { | |
| _timer?.cancel(); | |
| _timer = null; | |
| } | |
| setState(() {}); | |
| }, | |
| child: Text(_timer != null | |
| ? 'Pause timer' | |
| : (getRunningTime() == '0' | |
| ? 'Start timer' | |
| : 'Resume timer')), | |
| ), | |
| TextButton( | |
| onPressed: () { | |
| _timer?.cancel(); | |
| _timer = null; | |
| _duration = const Duration(); | |
| setState(() {}); | |
| }, | |
| child: const Text('Reset timer'), | |
| ), | |
| ], | |
| ), | |
| )); | |
| } | |
| String getRunningTime() { | |
| List<String> time = []; | |
| print('duration $_duration'); | |
| if (_duration.inHours > 0) { | |
| time.add(_duration.inHours.toString()); | |
| } | |
| if (_duration.inHours > 0 || _duration.inMinutes > 0) { | |
| time.add(_duration.inMinutes.toString()); | |
| } | |
| if (_duration.inSeconds > 0) { | |
| time.add(_duration.inSeconds.toString()); | |
| } | |
| if (time.isEmpty) { | |
| return '0'; | |
| } | |
| return time.join(':'); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment