2021-01-06 19:25:53 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-02-16 07:28:29 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2021-01-06 19:25:53 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:selfprivacy/utils/named_font_weight.dart';
|
|
|
|
|
|
|
|
class BrandTimer extends StatefulWidget {
|
|
|
|
const BrandTimer({
|
2021-03-15 15:39:44 +00:00
|
|
|
required this.startDateTime,
|
|
|
|
required this.duration,
|
2022-10-26 16:26:09 +00:00
|
|
|
super.key,
|
2022-06-05 19:36:32 +00:00
|
|
|
});
|
2021-01-06 19:25:53 +00:00
|
|
|
|
2021-03-23 19:50:11 +00:00
|
|
|
final DateTime startDateTime;
|
|
|
|
final Duration duration;
|
2021-01-06 19:25:53 +00:00
|
|
|
|
|
|
|
@override
|
2022-05-25 12:21:56 +00:00
|
|
|
State<BrandTimer> createState() => _BrandTimerState();
|
2021-01-06 19:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _BrandTimerState extends State<BrandTimer> {
|
2021-03-15 15:39:44 +00:00
|
|
|
String? _timeString;
|
|
|
|
late Timer timer;
|
2021-01-06 19:25:53 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2021-01-19 12:05:40 +00:00
|
|
|
_timerStart();
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
void _timerStart() {
|
2022-02-16 07:28:29 +00:00
|
|
|
_timeString = differenceFromStart;
|
2022-06-05 19:36:32 +00:00
|
|
|
timer = Timer.periodic(const Duration(seconds: 1), (final Timer t) {
|
2022-06-05 22:40:34 +00:00
|
|
|
final Duration timePassed =
|
|
|
|
DateTime.now().difference(widget.startDateTime);
|
2021-03-23 19:50:11 +00:00
|
|
|
if (timePassed > widget.duration) {
|
2021-01-06 19:25:53 +00:00
|
|
|
t.cancel();
|
|
|
|
} else {
|
|
|
|
_getTime();
|
|
|
|
}
|
|
|
|
});
|
2021-01-19 12:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-06-05 19:36:32 +00:00
|
|
|
void didUpdateWidget(final BrandTimer oldWidget) {
|
2021-01-19 12:05:40 +00:00
|
|
|
if (timer.isActive) {
|
|
|
|
timer.cancel();
|
|
|
|
}
|
|
|
|
_timerStart();
|
|
|
|
super.didUpdateWidget(oldWidget);
|
2021-01-06 19:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-03-27 17:02:44 +00:00
|
|
|
Widget build(final BuildContext context) => Text(
|
|
|
|
_timeString ?? '',
|
|
|
|
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
|
|
|
fontWeight: NamedFontWeight.demiBold,
|
|
|
|
color: Theme.of(context).colorScheme.onSurface,
|
|
|
|
),
|
2022-06-05 22:40:34 +00:00
|
|
|
);
|
2021-01-06 19:25:53 +00:00
|
|
|
|
|
|
|
void _getTime() {
|
|
|
|
setState(() {
|
2022-02-16 07:28:29 +00:00
|
|
|
_timeString = differenceFromStart;
|
2021-01-06 19:25:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-16 07:28:29 +00:00
|
|
|
String get differenceFromStart =>
|
2021-03-23 19:50:11 +00:00
|
|
|
_durationToString(DateTime.now().difference(widget.startDateTime));
|
2021-01-06 19:25:53 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
String _durationToString(final Duration duration) {
|
|
|
|
final Duration timeLeft = widget.duration - duration;
|
|
|
|
String twoDigits(final int n) => n.toString().padLeft(2, '0');
|
|
|
|
final String twoDigitSeconds = twoDigits(timeLeft.inSeconds);
|
2021-01-06 19:25:53 +00:00
|
|
|
|
2022-05-24 18:55:39 +00:00
|
|
|
return 'timer.sec'.tr(args: [twoDigitSeconds]);
|
2021-01-06 19:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
if (timer.isActive) {
|
|
|
|
timer.cancel();
|
|
|
|
}
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|