2023-02-14 14:02:26 +01:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
2023-02-16 02:08:15 +01:00
|
|
|
import 'package:smoke_cess_app/service/settings_service.dart';
|
2023-02-14 14:02:26 +01:00
|
|
|
import 'package:smoke_cess_app/widgets/timer_button.dart';
|
|
|
|
|
|
|
|
class StopWatchTimerPage extends StatefulWidget {
|
2023-02-14 14:13:32 +01:00
|
|
|
const StopWatchTimerPage({super.key});
|
|
|
|
|
2023-02-14 14:02:26 +01:00
|
|
|
@override
|
2023-02-14 14:13:32 +01:00
|
|
|
StopWatchTimerPageState createState() => StopWatchTimerPageState();
|
2023-02-14 14:02:26 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 14:13:32 +01:00
|
|
|
class StopWatchTimerPageState extends State<StopWatchTimerPage> {
|
2023-02-16 02:08:15 +01:00
|
|
|
SettingsService settings = SettingsService();
|
|
|
|
Duration duration = const Duration(minutes: 1);
|
2023-02-14 14:02:26 +01:00
|
|
|
Timer? timer;
|
|
|
|
|
|
|
|
bool countDown = true;
|
|
|
|
|
2023-02-16 02:08:15 +01:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
setDurationWithSetting();
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDurationWithSetting() {
|
|
|
|
settings.getIntSetting('workout_duration_minutes').then((workoutMinutes) =>
|
|
|
|
{setState(() => duration = Duration(minutes: workoutMinutes ?? 10))});
|
|
|
|
}
|
|
|
|
|
2023-02-14 14:02:26 +01:00
|
|
|
void reset() {
|
|
|
|
if (countDown) {
|
2023-02-16 02:08:15 +01:00
|
|
|
setDurationWithSetting();
|
2023-02-14 14:02:26 +01:00
|
|
|
} else {
|
2023-02-14 14:13:32 +01:00
|
|
|
setState(() => duration = const Duration());
|
2023-02-14 14:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void startTimer() {
|
2023-02-14 14:13:32 +01:00
|
|
|
timer = Timer.periodic(const Duration(seconds: 1), (_) => addTime());
|
2023-02-14 14:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void addTime() {
|
|
|
|
final addSeconds = countDown ? -1 : 1;
|
|
|
|
setState(() {
|
|
|
|
final seconds = duration.inSeconds + addSeconds;
|
|
|
|
if (seconds < 0) {
|
|
|
|
timer?.cancel();
|
|
|
|
} else {
|
|
|
|
duration = Duration(seconds: seconds);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopTimer({bool resets = true}) {
|
|
|
|
if (resets) {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
setState(() => timer?.cancel());
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
buildTime(),
|
2023-02-14 14:13:32 +01:00
|
|
|
const SizedBox(
|
2023-02-14 14:02:26 +01:00
|
|
|
height: 80,
|
|
|
|
),
|
|
|
|
buildButtons()
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget buildTime() {
|
|
|
|
String twoDigits(int n) => n.toString().padLeft(2, '0');
|
|
|
|
final minutes = twoDigits(duration.inMinutes.remainder(60));
|
|
|
|
final seconds = twoDigits(duration.inSeconds.remainder(60));
|
|
|
|
return Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
2023-02-14 14:13:32 +01:00
|
|
|
const SizedBox(
|
2023-02-14 14:02:26 +01:00
|
|
|
width: 8,
|
|
|
|
),
|
|
|
|
buildTimeCard(time: minutes, header: 'MINUTEN'),
|
2023-02-14 14:13:32 +01:00
|
|
|
const SizedBox(
|
2023-02-14 14:02:26 +01:00
|
|
|
width: 8,
|
|
|
|
),
|
|
|
|
buildTimeCard(time: seconds, header: 'SEKUNDEN'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget buildTimeCard({required String time, required String header}) =>
|
|
|
|
Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Container(
|
2023-02-14 14:13:32 +01:00
|
|
|
padding: const EdgeInsets.all(8),
|
2023-02-14 14:02:26 +01:00
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.white, borderRadius: BorderRadius.circular(20)),
|
|
|
|
child: Text(
|
|
|
|
time,
|
2023-02-14 14:13:32 +01:00
|
|
|
style: const TextStyle(
|
2023-02-14 14:02:26 +01:00
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
color: Colors.black,
|
|
|
|
fontSize: 50),
|
|
|
|
),
|
|
|
|
),
|
2023-02-14 14:13:32 +01:00
|
|
|
const SizedBox(
|
2023-02-14 14:02:26 +01:00
|
|
|
height: 24,
|
|
|
|
),
|
2023-02-14 14:13:32 +01:00
|
|
|
Text(header, style: const TextStyle(color: Colors.black45)),
|
2023-02-14 14:02:26 +01:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget buildButtons() {
|
|
|
|
final isRunning = timer == null ? false : timer!.isActive;
|
|
|
|
final isCompleted = duration.inSeconds == 0;
|
|
|
|
return isRunning || isCompleted
|
|
|
|
? TimerButton(
|
|
|
|
onClicked: stopTimer,
|
2023-02-14 14:13:32 +01:00
|
|
|
icon: const Icon(
|
2023-02-14 14:02:26 +01:00
|
|
|
Icons.stop,
|
|
|
|
size: 50,
|
|
|
|
color: Colors.white,
|
|
|
|
),
|
|
|
|
color: Colors.red)
|
|
|
|
: TimerButton(
|
|
|
|
onClicked: startTimer,
|
2023-02-14 14:13:32 +01:00
|
|
|
icon: const Icon(
|
2023-02-14 14:02:26 +01:00
|
|
|
Icons.play_arrow,
|
|
|
|
size: 50,
|
|
|
|
color: Colors.white,
|
|
|
|
),
|
|
|
|
color: Colors.green);
|
|
|
|
}
|
|
|
|
}
|