109 lines
2.7 KiB
Dart
109 lines
2.7 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class GuidedStepTimer extends StatefulWidget {
|
|
final int seconds;
|
|
final String expiredMessage;
|
|
final VoidCallback? onExpired;
|
|
/// true = barre de progression + texte MM:SS (mode escape)
|
|
/// false = texte MM:SS seul (mode peek carte)
|
|
final bool showAsBar;
|
|
|
|
const GuidedStepTimer({
|
|
Key? key,
|
|
required this.seconds,
|
|
required this.expiredMessage,
|
|
this.onExpired,
|
|
this.showAsBar = false,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
State<GuidedStepTimer> createState() => _GuidedStepTimerState();
|
|
}
|
|
|
|
class _GuidedStepTimerState extends State<GuidedStepTimer> {
|
|
late int _remaining;
|
|
Timer? _timer;
|
|
bool _expired = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_remaining = widget.seconds;
|
|
_timer = Timer.periodic(const Duration(seconds: 1), _tick);
|
|
}
|
|
|
|
void _tick(Timer t) {
|
|
if (!mounted) return;
|
|
if (_remaining <= 0) {
|
|
_timer?.cancel();
|
|
setState(() => _expired = true);
|
|
widget.onExpired?.call();
|
|
return;
|
|
}
|
|
setState(() => _remaining--);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_timer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_expired) {
|
|
return Text(
|
|
widget.expiredMessage,
|
|
style: const TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
|
|
);
|
|
}
|
|
|
|
final timeText = _formatTime(_remaining);
|
|
|
|
if (widget.showAsBar) {
|
|
final progress = widget.seconds > 0 ? _remaining / widget.seconds : 0.0;
|
|
final color = progress > 0.5
|
|
? Colors.green
|
|
: progress > 0.25
|
|
? Colors.orange
|
|
: Colors.red;
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.timer, size: 18),
|
|
const SizedBox(width: 6),
|
|
Text(timeText, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
|
|
],
|
|
),
|
|
const SizedBox(height: 4),
|
|
LinearProgressIndicator(
|
|
value: progress,
|
|
backgroundColor: Colors.grey[300],
|
|
valueColor: AlwaysStoppedAnimation<Color>(color),
|
|
minHeight: 8,
|
|
borderRadius: BorderRadius.circular(4),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
return Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(Icons.timer, size: 14),
|
|
const SizedBox(width: 4),
|
|
Text(timeText, style: const TextStyle(fontSize: 13)),
|
|
],
|
|
);
|
|
}
|
|
|
|
String _formatTime(int s) {
|
|
final m = s ~/ 60;
|
|
final sec = s % 60;
|
|
return '${m.toString().padLeft(2, '0')}:${sec.toString().padLeft(2, '0')}';
|
|
}
|
|
}
|