From 20f5c5e42643e71f8bde0344ae73d7c6b8ec7196 Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Sun, 26 Feb 2023 19:52:45 +0100 Subject: [PATCH] add timer widget, provider and util (format) --- lib/pages/interval_page.dart | 7 +++++++ lib/providers/timer_provider.dart | 23 +++++++++++++++++++++++ lib/utils/timer_util.dart | 4 ++++ lib/widgets/timer_widget.dart | 25 +++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 lib/providers/timer_provider.dart create mode 100644 lib/utils/timer_util.dart diff --git a/lib/pages/interval_page.dart b/lib/pages/interval_page.dart index 98b1c17..f6d2fcd 100644 --- a/lib/pages/interval_page.dart +++ b/lib/pages/interval_page.dart @@ -2,7 +2,9 @@ import 'dart:async'; import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/providers/timer_provider.dart'; import 'package:smoke_cess_app/widgets/popup_for_start_and_stop.dart'; +import 'package:smoke_cess_app/widgets/timer_widget.dart'; import '../providers/input_provider.dart'; @@ -166,6 +168,11 @@ class _IntervalTimerPageState extends State { @override Widget build(BuildContext context) { + return ChangeNotifierProvider( + create: (context) => TimerProvider(), + child: TimerWidget( + duration: Duration(seconds: 5), + )); return Center( child: ChangeNotifierProvider( create: (context) => InputProvider(), diff --git a/lib/providers/timer_provider.dart b/lib/providers/timer_provider.dart new file mode 100644 index 0000000..0601b39 --- /dev/null +++ b/lib/providers/timer_provider.dart @@ -0,0 +1,23 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; + +class TimerProvider extends ChangeNotifier { + Timer? _timer; + bool started = false; + int get elapsedSeconds => _timer != null ? _timer!.tick : 0; + + void startTimer(Duration duration) { + started = true; + print('starting timer'); + _timer = Timer.periodic(const Duration(seconds: 1), ((timer) { + if (timer.tick >= duration.inSeconds) { + timer.cancel(); + started = false; + } + notifyListeners(); + })); + } + + void stopTimer() => _timer?.cancel(); +} diff --git a/lib/utils/timer_util.dart b/lib/utils/timer_util.dart new file mode 100644 index 0000000..58d9690 --- /dev/null +++ b/lib/utils/timer_util.dart @@ -0,0 +1,4 @@ +String formatTime(int seconds) { + Duration duration = Duration(seconds: seconds); + return '${duration.inMinutes.remainder(60).toString().padLeft(2, '0')}:${duration.inSeconds.remainder(60).toString().padLeft(2, '0')}'; +} diff --git a/lib/widgets/timer_widget.dart b/lib/widgets/timer_widget.dart index e69de29..875697f 100644 --- a/lib/widgets/timer_widget.dart +++ b/lib/widgets/timer_widget.dart @@ -0,0 +1,25 @@ +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/providers/timer_provider.dart'; +import 'package:smoke_cess_app/utils/timer_util.dart'; + +class TimerWidget extends StatelessWidget { + final Duration duration; + const TimerWidget({super.key, required this.duration}); + + @override + Widget build(BuildContext context) { + TimerProvider timerProvider = context.watch(); + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text(formatTime(duration.inSeconds - timerProvider.elapsedSeconds)), + ElevatedButton( + onPressed: () => timerProvider.started + ? timerProvider.stopTimer() + : timerProvider.startTimer(duration), + child: Text(timerProvider.started ? 'Stop' : 'Start')) + ], + ); + } +}