2024-04-30 14:32:01 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2024-04-30 19:50:09 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-04-30 14:32:01 +00:00
|
|
|
import 'package:lightmeter/generated/l10n.dart';
|
|
|
|
import 'package:lightmeter/res/dimens.dart';
|
2024-04-30 19:50:09 +00:00
|
|
|
import 'package:lightmeter/screens/timer/bloc_timer.dart';
|
|
|
|
import 'package:lightmeter/screens/timer/event_timer.dart';
|
|
|
|
import 'package:lightmeter/screens/timer/state_timer.dart';
|
2024-04-30 14:32:01 +00:00
|
|
|
import 'package:material_color_utilities/material_color_utilities.dart';
|
|
|
|
|
2024-04-30 19:50:09 +00:00
|
|
|
class TimerScreen extends StatefulWidget {
|
2024-04-30 19:57:02 +00:00
|
|
|
final Duration duration;
|
|
|
|
|
|
|
|
const TimerScreen({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();
|
|
|
|
|
2024-04-30 19:57:02 +00:00
|
|
|
timelineController = AnimationController(vsync: this, duration: widget.duration);
|
|
|
|
timelineAnimation = Tween<double>(begin: 1, end: 0).animate(timelineController);
|
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>(
|
|
|
|
listenWhen: (previous, current) =>
|
|
|
|
previous is TimerStoppedState && current is TimerResumedState ||
|
|
|
|
previous is TimerResumedState && current is TimerStoppedState,
|
|
|
|
listener: (context, state) => _updateAnimations(state),
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
automaticallyImplyLeading: false,
|
|
|
|
centerTitle: true,
|
|
|
|
elevation: 0,
|
|
|
|
title: Text(
|
|
|
|
'Test',
|
|
|
|
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,
|
|
|
|
builder: (_, value, child) => _TimerTimeline(
|
|
|
|
progress: value,
|
|
|
|
child: child!,
|
|
|
|
),
|
|
|
|
child: BlocBuilder<TimerBloc, TimerState>(
|
|
|
|
buildWhen: (previous, current) => previous.timeLeft != current.timeLeft,
|
|
|
|
builder: (_, state) => _Timer(
|
|
|
|
timeLeft: state.timeLeft,
|
|
|
|
duration: state.duration,
|
|
|
|
),
|
|
|
|
),
|
2024-04-30 19:50:09 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
const Spacer(),
|
|
|
|
BlocBuilder<TimerBloc, TimerState>(
|
|
|
|
builder: (_, state) => FloatingActionButton(
|
|
|
|
onPressed: () {
|
|
|
|
context.read<TimerBloc>().add(
|
|
|
|
state is TimerStoppedState ? const StartTimerEvent() : const StopTimerEvent(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: AnimatedIcon(
|
|
|
|
icon: AnimatedIcons.play_pause,
|
|
|
|
progress: startStopIconAnimation,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
class _Timer extends StatelessWidget {
|
2024-04-30 19:50:09 +00:00
|
|
|
final Duration timeLeft;
|
|
|
|
final Duration duration;
|
2024-04-30 14:32:01 +00:00
|
|
|
|
|
|
|
const _Timer({
|
2024-04-30 19:50:09 +00:00
|
|
|
required this.timeLeft,
|
|
|
|
required this.duration,
|
2024-04-30 14:32:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-04-30 19:50:09 +00:00
|
|
|
return Text(
|
|
|
|
parseSeconds(),
|
|
|
|
style: Theme.of(context).textTheme.headlineLarge,
|
2024-04-30 14:32:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
String parseSeconds() {
|
|
|
|
String addZeroIfNeeded(int value) {
|
|
|
|
if (value == 0) {
|
|
|
|
return '00';
|
|
|
|
} else if (value < 10) {
|
|
|
|
return '0$value';
|
|
|
|
} else {
|
|
|
|
return '$value';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final buffer = StringBuffer();
|
2024-04-30 19:50:09 +00:00
|
|
|
int remainingSeconds = timeLeft.inSeconds;
|
2024-04-30 14:32:01 +00:00
|
|
|
// longer than 1 hours
|
2024-04-30 19:50:09 +00:00
|
|
|
if (duration.inSeconds >= 3600) {
|
2024-04-30 14:32:01 +00:00
|
|
|
final hours = remainingSeconds ~/ 3600;
|
|
|
|
buffer.writeAll([addZeroIfNeeded(hours), ':']);
|
|
|
|
remainingSeconds -= hours * 3600;
|
|
|
|
}
|
|
|
|
// longer than 1 minute
|
2024-04-30 19:50:09 +00:00
|
|
|
if (duration.inSeconds >= 60 || duration.inSeconds == 0) {
|
2024-04-30 14:32:01 +00:00
|
|
|
final minutes = remainingSeconds ~/ 60;
|
|
|
|
buffer.writeAll([addZeroIfNeeded(minutes), ':']);
|
|
|
|
remainingSeconds -= minutes * 60;
|
|
|
|
}
|
|
|
|
// longer than 1 second
|
|
|
|
buffer.write(addZeroIfNeeded(remainingSeconds));
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 19:50:09 +00:00
|
|
|
class _TimerTimeline extends StatelessWidget {
|
|
|
|
final double progress;
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
const _TimerTimeline({
|
|
|
|
required this.progress,
|
|
|
|
required this.child,
|
|
|
|
}) : assert(progress >= 0 && progress <= 1);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return CustomPaint(
|
|
|
|
painter: _TimelinePainter(
|
|
|
|
backgroundColor: ElevationOverlay.applySurfaceTint(
|
|
|
|
Theme.of(context).cardTheme.color!,
|
|
|
|
Theme.of(context).cardTheme.surfaceTintColor,
|
|
|
|
Theme.of(context).cardTheme.elevation!,
|
|
|
|
),
|
|
|
|
progressColor: Theme.of(context).colorScheme.primary,
|
|
|
|
progress: progress,
|
|
|
|
),
|
|
|
|
willChange: true,
|
|
|
|
child: Center(child: child),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 14:32:01 +00:00
|
|
|
class _TimelinePainter extends CustomPainter {
|
|
|
|
final Color progressColor;
|
|
|
|
final Color backgroundColor;
|
|
|
|
final double progress;
|
|
|
|
|
|
|
|
late final double timelineEdgeRadius = strokeWidth / 2;
|
|
|
|
late final double radiansProgress = 2 * pi * progress;
|
|
|
|
static const double radiansQuarterTurn = -pi / 2;
|
|
|
|
static const double strokeWidth = Dimens.grid8;
|
|
|
|
|
|
|
|
_TimelinePainter({
|
|
|
|
required this.progressColor,
|
|
|
|
required this.backgroundColor,
|
|
|
|
required this.progress,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
void paint(Canvas canvas, Size size) {
|
|
|
|
final radius = size.height / 2;
|
|
|
|
final timerCenter = Offset(radius, radius);
|
|
|
|
final timelineSegmentPath = Path.combine(
|
|
|
|
PathOperation.difference,
|
|
|
|
Path()
|
|
|
|
..arcTo(
|
|
|
|
Rect.fromCenter(
|
|
|
|
center: timerCenter,
|
|
|
|
height: size.height,
|
|
|
|
width: size.width,
|
|
|
|
),
|
|
|
|
radiansQuarterTurn,
|
|
|
|
radiansProgress,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
..lineTo(radius, radius)
|
|
|
|
..lineTo(radius, 0),
|
|
|
|
Path()
|
|
|
|
..addOval(
|
|
|
|
Rect.fromCircle(
|
|
|
|
center: timerCenter,
|
|
|
|
radius: radius - strokeWidth,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final smoothEdgesPath = Path.combine(
|
|
|
|
PathOperation.union,
|
|
|
|
Path()
|
|
|
|
..addOval(
|
|
|
|
Rect.fromCircle(
|
|
|
|
center: Offset(radius, timelineEdgeRadius),
|
|
|
|
radius: timelineEdgeRadius,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Path()
|
|
|
|
..addOval(
|
|
|
|
Rect.fromCircle(
|
|
|
|
center: Offset(
|
|
|
|
(radius - timelineEdgeRadius) * cos(radiansProgress + radiansQuarterTurn) + radius,
|
|
|
|
(radius - timelineEdgeRadius) * sin(radiansProgress + radiansQuarterTurn) + radius,
|
|
|
|
),
|
|
|
|
radius: timelineEdgeRadius,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
canvas.drawPath(
|
|
|
|
Path.combine(
|
|
|
|
PathOperation.difference,
|
|
|
|
Path()
|
|
|
|
..addOval(
|
|
|
|
Rect.fromCircle(
|
|
|
|
center: timerCenter,
|
|
|
|
radius: radius,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Path()
|
|
|
|
..addOval(
|
|
|
|
Rect.fromCircle(
|
|
|
|
center: timerCenter,
|
|
|
|
radius: radius - strokeWidth,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Paint()..color = backgroundColor,
|
|
|
|
);
|
|
|
|
|
|
|
|
canvas.drawPath(
|
|
|
|
Path.combine(
|
|
|
|
PathOperation.union,
|
|
|
|
timelineSegmentPath,
|
|
|
|
smoothEdgesPath,
|
|
|
|
),
|
|
|
|
Paint()..color = progressColor,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool shouldRepaint(_TimelinePainter oldDelegate) => oldDelegate.progress != progress;
|
|
|
|
}
|