dart format
This commit is contained in:
@@ -1,90 +1,91 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CooldownTimer extends StatefulWidget {
|
||||
final DateTime createdAt;
|
||||
final VoidCallback onTimerFinish;
|
||||
class CooldownTimer extends StatefulWidget {
|
||||
final DateTime createdAt;
|
||||
final VoidCallback onTimerFinish;
|
||||
|
||||
const CooldownTimer({
|
||||
Key? key,
|
||||
required this.createdAt,
|
||||
required this.onTimerFinish,
|
||||
}) : super(key: key);
|
||||
const CooldownTimer({
|
||||
Key? key,
|
||||
required this.createdAt,
|
||||
required this.onTimerFinish,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
_CooldownTimerState createState() => _CooldownTimerState();
|
||||
}
|
||||
@override
|
||||
_CooldownTimerState createState() => _CooldownTimerState();
|
||||
}
|
||||
|
||||
class _CooldownTimerState extends State<CooldownTimer> {
|
||||
late Timer _timer;
|
||||
late Duration _timeRemaining;
|
||||
class _CooldownTimerState extends State<CooldownTimer> {
|
||||
late Timer _timer;
|
||||
late Duration _timeRemaining;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_updateRemainingTime();
|
||||
// Update the timer every second
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
_updateRemainingTime();
|
||||
});
|
||||
}
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_updateRemainingTime();
|
||||
// Update the timer every second
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
_updateRemainingTime();
|
||||
});
|
||||
}
|
||||
|
||||
void _updateRemainingTime() {
|
||||
final cooldownEnd = widget.createdAt.add(const Duration(minutes: 60));
|
||||
final now = DateTime.now();
|
||||
void _updateRemainingTime() {
|
||||
final cooldownEnd = widget.createdAt.add(const Duration(minutes: 60));
|
||||
final now = DateTime.now();
|
||||
|
||||
if (now.isAfter(cooldownEnd)) {
|
||||
_timeRemaining = Duration.zero;
|
||||
_timer.cancel();
|
||||
// Notify the parent widget that the timer is done
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
widget.onTimerFinish();
|
||||
});
|
||||
} else {
|
||||
_timeRemaining = cooldownEnd.difference(now);
|
||||
}
|
||||
// Trigger a rebuild
|
||||
setState(() {});
|
||||
}
|
||||
if (now.isAfter(cooldownEnd)) {
|
||||
_timeRemaining = Duration.zero;
|
||||
_timer.cancel();
|
||||
// Notify the parent widget that the timer is done
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
widget.onTimerFinish();
|
||||
});
|
||||
} else {
|
||||
_timeRemaining = cooldownEnd.difference(now);
|
||||
}
|
||||
// Trigger a rebuild
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
@override
|
||||
void dispose() {
|
||||
_timer.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String _formatDuration(Duration duration) {
|
||||
final minutes = duration.inMinutes.remainder(60).toString().padLeft(2, '0');
|
||||
final seconds = duration.inSeconds.remainder(60).toString().padLeft(2, '0');
|
||||
return '$minutes:$seconds';
|
||||
}
|
||||
String _formatDuration(Duration duration) {
|
||||
final minutes = duration.inMinutes.remainder(60).toString().padLeft(2, '0');
|
||||
final seconds = duration.inSeconds.remainder(60).toString().padLeft(2, '0');
|
||||
return '$minutes:$seconds';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_timeRemaining == Duration.zero) {
|
||||
return const SizedBox.shrink(); // Or some other widget indicating it's enabled
|
||||
}
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_timeRemaining == Duration.zero) {
|
||||
return const SizedBox
|
||||
.shrink(); // Or some other widget indicating it's enabled
|
||||
}
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(
|
||||
'Enabled after:',
|
||||
style: TextStyle(
|
||||
color: Colors.red.shade700,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
_formatDuration(_timeRemaining),
|
||||
style: TextStyle(
|
||||
color: Colors.red.shade700,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(
|
||||
'Enabled after:',
|
||||
style: TextStyle(
|
||||
color: Colors.red.shade700,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
_formatDuration(_timeRemaining),
|
||||
style: TextStyle(
|
||||
color: Colors.red.shade700,
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user