106 lines
2.7 KiB
Dart
106 lines
2.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
void main() => runApp(const HockeyTimerApp());
|
|
|
|
class HockeyTimerApp extends StatelessWidget {
|
|
const HockeyTimerApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Hockey Timer',
|
|
theme: ThemeData(colorSchemeSeed: const Color(0xff087f8c), useMaterial3: true),
|
|
home: const TimerScreen(),
|
|
);
|
|
}
|
|
}
|
|
|
|
class TimerScreen extends StatefulWidget {
|
|
const TimerScreen({super.key});
|
|
|
|
@override
|
|
State<TimerScreen> createState() => _TimerScreenState();
|
|
}
|
|
|
|
class _TimerScreenState extends State<TimerScreen> {
|
|
static const _periodLength = 20 * 60;
|
|
int _secondsRemaining = _periodLength;
|
|
Timer? _ticker;
|
|
|
|
void _toggleTimer() {
|
|
if (_ticker != null) {
|
|
_ticker!.cancel();
|
|
setState(() => _ticker = null);
|
|
return;
|
|
}
|
|
|
|
if (_secondsRemaining == 0) return;
|
|
setState(() {
|
|
_ticker = Timer.periodic(const Duration(seconds: 1), (_) {
|
|
if (_secondsRemaining <= 1) {
|
|
_ticker?.cancel();
|
|
setState(() {
|
|
_secondsRemaining = 0;
|
|
_ticker = null;
|
|
});
|
|
} else {
|
|
setState(() => _secondsRemaining--);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
void _resetTimer() {
|
|
_ticker?.cancel();
|
|
setState(() {
|
|
_ticker = null;
|
|
_secondsRemaining = _periodLength;
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_ticker?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final minutes = (_secondsRemaining ~/ 60).toString().padLeft(2, '0');
|
|
final seconds = (_secondsRemaining % 60).toString().padLeft(2, '0');
|
|
final running = _ticker != null;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Hockey Timer')),
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text('PERIOD 1', style: Theme.of(context).textTheme.titleMedium),
|
|
const SizedBox(height: 12),
|
|
Text('$minutes:$seconds', style: Theme.of(context).textTheme.displayLarge),
|
|
const SizedBox(height: 24),
|
|
Wrap(
|
|
spacing: 12,
|
|
children: [
|
|
FilledButton.icon(
|
|
onPressed: _toggleTimer,
|
|
icon: Icon(running ? Icons.pause : Icons.play_arrow),
|
|
label: Text(running ? 'Pause' : 'Start'),
|
|
),
|
|
OutlinedButton.icon(
|
|
onPressed: _resetTimer,
|
|
icon: const Icon(Icons.replay),
|
|
label: const Text('Reset'),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|