From 501f841ddf1bd3718fbea50e2d9da2d939634c8c Mon Sep 17 00:00:00 2001 From: Ovidius Mazuru Date: Mon, 24 Aug 2020 21:24:55 +0100 Subject: [PATCH] 4.7 --- .../lib/screens/stopwatch_screen.dart | 31 +++++++-- .../lib/state/stopwatch_provider.dart | 66 +++++++++++++++++++ flutter_timer/pubspec.yaml | 8 +-- 3 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 flutter_timer/lib/state/stopwatch_provider.dart diff --git a/flutter_timer/lib/screens/stopwatch_screen.dart b/flutter_timer/lib/screens/stopwatch_screen.dart index 198dad2..2729f6b 100644 --- a/flutter_timer/lib/screens/stopwatch_screen.dart +++ b/flutter_timer/lib/screens/stopwatch_screen.dart @@ -1,4 +1,6 @@ import 'package:flutter/material.dart'; +import 'package:flutter_timer/state/stopwatch_provider.dart'; +import 'package:provider/provider.dart'; import '../widgets/bottom_nav_bar.dart'; @@ -8,10 +10,31 @@ class StopwatchScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( - body: Center( - child: Text('Stopwatch Screen', style: Theme.of(context).textTheme.headline1) + body: ChangeNotifierProvider( + create: (_) => StopwatchProvider(), + child: Consumer( + builder: (ctx, state, _) { + if (state.isRunning) { + return Center( + child: Text(state.elapsedTimeString, + style: Theme.of(context).textTheme.headline1), + ); + } else { + return Center( + child: GestureDetector( + onTap: state.start, + child: Icon( + Icons.play_circle_outline, + size: 200, + color: Theme.of(context).primaryColor, + ), + ), + ); + } + }, + ), ), - bottomNavigationBar: BottomNavBar( ), + bottomNavigationBar: BottomNavBar(), ); } -} \ No newline at end of file +} diff --git a/flutter_timer/lib/state/stopwatch_provider.dart b/flutter_timer/lib/state/stopwatch_provider.dart new file mode 100644 index 0000000..15d9c6f --- /dev/null +++ b/flutter_timer/lib/state/stopwatch_provider.dart @@ -0,0 +1,66 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; + +class StopwatchProvider with ChangeNotifier { + Stopwatch _sw = Stopwatch(); + String _returnString = '00:00'; + Timer t; + + String get time => _returnString; + + bool get isRunning => _sw.isRunning; + + String get elapsedTimeString => _returnString; + + void start() { + _sw.start(); + updateSWString(); + notifyListeners(); + } + + void pause() { + _sw.stop(); + } + + void unpause() { + if (_sw.isRunning) { + _sw.start(); + } + } + + void reset() { + _sw.reset(); + } + + void updateSWString() { + Timer.periodic(Duration(seconds: 1), (t) { + _returnString = _buildReturnString(); + print(_returnString); + notifyListeners(); + }); + } + + String _buildReturnString() { + /// Builds an appropriate return string for a stopwatch, in the format + /// HH:MM:SS + /// ie. 01:23:45 + String str = ''; + + String _buildSeconds() { + return (_sw.elapsed.inSeconds % 60) > 9 ? '${_sw.elapsed.inSeconds % 60}' : '0${_sw.elapsed.inSeconds % 60}'; + } + + String _buildMinutes() { + return (_sw.elapsed.inMinutes % 60) > 9 ? '${_sw.elapsed.inMinutes % 60}:${_buildSeconds()}' : '0${_sw.elapsed.inMinutes % 60}:${_buildSeconds()}'; + } + + if (_sw.elapsed.inHours >= 1) { + str += "${_sw.elapsed.inHours < 10 ? 0 : ''}${_sw.elapsed.inHours}"; + } + + str = str + _buildMinutes(); + return str; + } +} \ No newline at end of file diff --git a/flutter_timer/pubspec.yaml b/flutter_timer/pubspec.yaml index 41b27db..df1017d 100644 --- a/flutter_timer/pubspec.yaml +++ b/flutter_timer/pubspec.yaml @@ -24,10 +24,10 @@ dependencies: flutter: sdk: flutter - # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.3 + provider: ^4.3.2+1 dev_dependencies: flutter_test: @@ -35,7 +35,6 @@ dev_dependencies: # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec - # The following section is specific to Flutter. flutter: @@ -43,18 +42,14 @@ flutter: # included with your application, so that you can use the icons in # the material Icons class. uses-material-design: true - # To add assets to your application, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg - # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware. - # For details regarding adding assets from package dependencies, see # https://flutter.dev/assets-and-images/#from-packages - # To add custom fonts to your application, add a fonts section here, # in this "flutter" section. Each entry in this list should have a # "family" key with the font family name, and a "fonts" key with a @@ -74,3 +69,4 @@ flutter: # # For details regarding fonts from package dependencies, # see https://flutter.dev/custom-fonts/#from-packages +