Skip to content

Commit

Permalink
4.7
Browse files Browse the repository at this point in the history
  • Loading branch information
trance128 committed Aug 24, 2020
1 parent 9357e09 commit 501f841
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 10 deletions.
31 changes: 27 additions & 4 deletions flutter_timer/lib/screens/stopwatch_screen.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<StopwatchProvider>(
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(),
);
}
}
}
66 changes: 66 additions & 0 deletions flutter_timer/lib/state/stopwatch_provider.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 2 additions & 6 deletions flutter_timer/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,32 @@ 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:
sdk: flutter

# 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:

# The following line ensures that the Material Icons font is
# 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
Expand All @@ -74,3 +69,4 @@ flutter:
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages

0 comments on commit 501f841

Please sign in to comment.