add timer widget, provider and util (format)
parent
33e8f17db7
commit
20f5c5e426
|
@ -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<IntervalTimerPage> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ChangeNotifierProvider(
|
||||
create: (context) => TimerProvider(),
|
||||
child: TimerWidget(
|
||||
duration: Duration(seconds: 5),
|
||||
));
|
||||
return Center(
|
||||
child: ChangeNotifierProvider(
|
||||
create: (context) => InputProvider(),
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -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')}';
|
||||
}
|
|
@ -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<TimerProvider>();
|
||||
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'))
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue