cpd_2022_zi/lib/screens/stopwatch_timer

130 lines
3.1 KiB
Plaintext

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:smoke_cess_app/widgets/timer_button';
class StopWatchTimerPage extends StatefulWidget {
@override
_StopWatchTimerPageState createState() => _StopWatchTimerPageState();
}
class _StopWatchTimerPageState extends State<StopWatchTimerPage> {
static const countdownDuration = Duration(minutes: 1);
Duration duration = Duration();
Timer? timer;
bool countDown =true;
@override
void initState() {
// TODO: implement initState
super.initState();
reset();
}
void reset(){
if (countDown){
setState(() =>
duration = countdownDuration);
} else{
setState(() =>
duration = Duration());
}
}
void startTimer(){
timer = Timer.periodic(Duration(seconds: 1),(_) => addTime());
}
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(),
SizedBox(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: [
SizedBox(width: 8,),
buildTimeCard(time: minutes, header:'MINUTES'),
SizedBox(width: 8,),
buildTimeCard(time: seconds, header:'SECONDS'),
]
);
}
Widget buildTimeCard({required String time, required String header}) =>
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20)
),
child: Text(
time, style: TextStyle(fontWeight: FontWeight.bold,
color: Colors.black,fontSize: 50),),
),
SizedBox(height: 24,),
Text(header,style: TextStyle(color: Colors.black45)),
],
);
Widget buildButtons(){
final isRunning = timer == null? false: timer!.isActive;
final isCompleted = duration.inSeconds == 0;
return isRunning || isCompleted
?
TimerButton(
onClicked: stopTimer,
icon: Icon(
Icons.stop,
size: 50,
color: Colors.white,
),
color: Colors.red)
:
TimerButton(
onClicked: startTimer,
icon: Icon(
Icons.play_arrow,
size: 50,
color: Colors.white,
),
color: Colors.green);
}
}