From a351a88d4d1e178409d05bd4bc678d9c891e6052 Mon Sep 17 00:00:00 2001 From: Louay Farah Date: Thu, 26 Sep 2024 12:47:57 +0800 Subject: [PATCH] Add timer for the steps of each first aid situation --- lib/screens/first_aid_steps_screen.dart | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/screens/first_aid_steps_screen.dart b/lib/screens/first_aid_steps_screen.dart index 270b178..7bf301f 100644 --- a/lib/screens/first_aid_steps_screen.dart +++ b/lib/screens/first_aid_steps_screen.dart @@ -1,9 +1,36 @@ +import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:firstaid/notifiers/first_aid_steps_notifier.dart'; import 'package:firstaid/providers/situation_provider.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +final firstAidStepsStateProvider = + ChangeNotifierProvider.autoDispose((ref) => FirstAidStepsState()); + +class FirstAidStepsState extends ChangeNotifier { + Stopwatch stopwatch = Stopwatch(); + Timer? timer; + + FirstAidStepsState() { + stopwatch.start(); + timer = + Timer.periodic(Duration(seconds: 1), (Timer t) => notifyListeners()); + } + + void stop() { + stopwatch.stop(); + timer?.cancel(); + notifyListeners(); + } + + @override + void dispose() { + timer?.cancel(); + super.dispose(); + } +} + class FirstAidStepsScreen extends ConsumerWidget { final String id; @@ -14,6 +41,7 @@ class FirstAidStepsScreen extends ConsumerWidget { final situation = ref.watch(situationProvider(id)); final _checked = ref.watch(firstAidStepsProvider(id)); final _stepsNotifier = ref.read(firstAidStepsProvider(id).notifier); + final _firstAidStepsState = ref.watch(firstAidStepsStateProvider); return Scaffold( appBar: AppBar( @@ -30,6 +58,9 @@ class FirstAidStepsScreen extends ConsumerWidget { value: _checked[index], onChanged: (bool? value) { _stepsNotifier.toggleStep(id, index); + if (index == situation.steps.length - 1 && value == true) { + _firstAidStepsState.stop(); + } }, ), title: Text(situation.steps[index]), @@ -37,6 +68,10 @@ class FirstAidStepsScreen extends ConsumerWidget { ); }, ), + bottomNavigationBar: Text( + 'Elapsed time: ${_firstAidStepsState.stopwatch.elapsed.inSeconds} seconds', + style: TextStyle(fontSize: 20), + ), ); } }