m3_lightmeter/lib/screens/timer/screen_timer.dart

160 lines
5.7 KiB
Dart
Raw Normal View History

2024-04-30 14:32:01 +00:00
import 'package:flutter/material.dart';
2024-04-30 19:50:09 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2024-05-02 15:11:53 +00:00
import 'package:lightmeter/data/models/exposure_pair.dart';
2024-04-30 14:32:01 +00:00
import 'package:lightmeter/res/dimens.dart';
2024-04-30 19:50:09 +00:00
import 'package:lightmeter/screens/timer/bloc_timer.dart';
2024-05-03 08:50:35 +00:00
import 'package:lightmeter/screens/timer/components/text/widget_text_timer.dart';
2024-05-02 17:24:32 +00:00
import 'package:lightmeter/screens/timer/components/timeline/widget_timeline_timer.dart';
2024-04-30 19:50:09 +00:00
import 'package:lightmeter/screens/timer/event_timer.dart';
import 'package:lightmeter/screens/timer/state_timer.dart';
2024-04-30 14:32:01 +00:00
2024-04-30 19:50:09 +00:00
class TimerScreen extends StatefulWidget {
2024-05-02 15:11:53 +00:00
final ExposurePair exposurePair;
final Duration duration;
2024-04-30 19:57:02 +00:00
const TimerScreen({
required this.exposurePair,
required this.duration,
super.key,
});
2024-04-30 14:32:01 +00:00
2024-04-30 19:50:09 +00:00
@override
State<TimerScreen> createState() => _TimerScreenState();
}
class _TimerScreenState extends State<TimerScreen> with TickerProviderStateMixin {
late AnimationController timelineController;
late Animation<double> timelineAnimation;
late AnimationController startStopIconController;
late Animation<double> startStopIconAnimation;
@override
void initState() {
super.initState();
timelineController = AnimationController(vsync: this, duration: widget.duration);
2024-04-30 19:57:02 +00:00
timelineAnimation = Tween<double>(begin: 1, end: 0).animate(timelineController);
timelineController.addStatusListener((status) {
if (status == AnimationStatus.completed) {
context.read<TimerBloc>().add(const StopTimerEvent());
}
});
2024-04-30 19:50:09 +00:00
startStopIconController = AnimationController(vsync: this, duration: Dimens.durationS);
2024-04-30 19:57:02 +00:00
startStopIconAnimation = Tween<double>(begin: 0, end: 1).animate(startStopIconController);
2024-04-30 19:50:09 +00:00
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
context.read<TimerBloc>().add(const StartTimerEvent());
}
@override
void dispose() {
timelineController.dispose();
startStopIconController.dispose();
super.dispose();
}
2024-04-30 14:32:01 +00:00
@override
Widget build(BuildContext context) {
2024-04-30 19:50:09 +00:00
return BlocListener<TimerBloc, TimerState>(
2024-04-30 20:45:46 +00:00
listenWhen: (previous, current) => previous.runtimeType != current.runtimeType,
2024-04-30 19:50:09 +00:00
listener: (context, state) => _updateAnimations(state),
child: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
centerTitle: true,
elevation: 0,
title: Text(
2024-05-02 15:11:53 +00:00
widget.exposurePair.toString(),
2024-04-30 19:50:09 +00:00
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface,
fontSize: Dimens.grid24,
),
2024-04-30 14:32:01 +00:00
),
2024-04-30 19:50:09 +00:00
actions: [if (Navigator.of(context).canPop()) const CloseButton()],
2024-04-30 14:32:01 +00:00
),
2024-04-30 19:50:09 +00:00
body: SafeArea(
child: Center(
child: Padding(
padding: const EdgeInsets.all(Dimens.paddingL),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Spacer(),
2024-04-30 19:57:02 +00:00
SizedBox.fromSize(
size: Size.square(MediaQuery.sizeOf(context).width - Dimens.paddingL * 4),
child: ValueListenableBuilder(
valueListenable: timelineAnimation,
2024-05-02 17:24:32 +00:00
builder: (_, value, child) => TimerTimeline(
2024-04-30 19:57:02 +00:00
progress: value,
child: TimerText(
timeLeft: Duration(milliseconds: (widget.duration.inMilliseconds * value).toInt()),
duration: widget.duration,
2024-04-30 19:57:02 +00:00
),
),
2024-04-30 19:50:09 +00:00
),
),
const Spacer(),
2024-04-30 20:45:46 +00:00
Row(
children: [
Expanded(
child: Center(
child: IconButton(
onPressed: () {
context.read<TimerBloc>().add(const ResetTimerEvent());
},
icon: const Icon(Icons.restore),
),
),
2024-04-30 19:50:09 +00:00
),
2024-04-30 20:45:46 +00:00
SizedBox.fromSize(
size: const Size.square(Dimens.grid72),
child: BlocBuilder<TimerBloc, TimerState>(
builder: (_, state) => FloatingActionButton(
shape: state is TimerResumedState ? null : const CircleBorder(),
onPressed: () {
if (timelineAnimation.value == 0) {
return;
}
final event =
state is TimerStoppedState ? const StartTimerEvent() : const StopTimerEvent();
context.read<TimerBloc>().add(event);
},
2024-04-30 20:45:46 +00:00
child: AnimatedIcon(
icon: AnimatedIcons.play_pause,
progress: startStopIconAnimation,
),
),
),
),
const Spacer(),
],
2024-04-30 19:50:09 +00:00
),
],
2024-04-30 14:32:01 +00:00
),
2024-04-30 19:50:09 +00:00
),
2024-04-30 14:32:01 +00:00
),
),
),
);
}
2024-04-30 19:50:09 +00:00
void _updateAnimations(TimerState state) {
switch (state) {
2024-04-30 20:45:46 +00:00
case TimerResetState():
startStopIconController.reverse();
timelineController.stop();
timelineController.animateTo(0, duration: Dimens.durationS);
2024-04-30 19:50:09 +00:00
case TimerResumedState():
startStopIconController.forward();
2024-04-30 19:57:02 +00:00
timelineController.forward();
2024-04-30 19:50:09 +00:00
case TimerStoppedState():
startStopIconController.reverse();
2024-04-30 19:57:02 +00:00
timelineController.stop();
2024-04-30 19:50:09 +00:00
}
}
2024-04-30 14:32:01 +00:00
}