2019-11-02 19:14:34 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:comunic/utils/date_utils.dart';
|
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Countdown widget
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
class CountdownWidget extends StatefulWidget {
|
|
|
|
final int startTime;
|
|
|
|
final int endTime;
|
|
|
|
|
|
|
|
const CountdownWidget({
|
|
|
|
Key key,
|
|
|
|
@required this.startTime,
|
|
|
|
@required this.endTime,
|
|
|
|
}) : assert(startTime != null),
|
|
|
|
assert(endTime != null),
|
|
|
|
super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_CountdownWidgetState createState() => _CountdownWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CountdownWidgetState extends State<CountdownWidget> {
|
|
|
|
int get remainingTime => widget.endTime - time();
|
|
|
|
|
|
|
|
int get totalDuration => (widget.endTime - widget.startTime).abs();
|
|
|
|
|
|
|
|
String get remainingTimeStr {
|
2019-11-02 19:17:00 +00:00
|
|
|
final remaining = Duration(seconds: remainingTime.abs());
|
2019-11-02 19:14:34 +00:00
|
|
|
|
|
|
|
return tr(
|
|
|
|
"%days% Days %hours% Hours %minutes% Minutes %seconds% Seconds",
|
|
|
|
args: {
|
|
|
|
"days": remaining.inDays.toString(),
|
|
|
|
"hours": (remaining.inHours % 24).toString(),
|
|
|
|
"minutes": (remaining.inMinutes % 60).toString(),
|
|
|
|
"seconds": (remaining.inSeconds % 60).toString(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
Timer.periodic(Duration(seconds: 1), _timerCb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _timerCb(Timer timer) {
|
2019-11-02 19:17:00 +00:00
|
|
|
if (!mounted) {
|
|
|
|
timer.cancel();
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 19:14:34 +00:00
|
|
|
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Container(
|
|
|
|
width: 300,
|
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Text(remainingTimeStr),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: LinearProgressIndicator(
|
|
|
|
value: remainingTime <= 0
|
|
|
|
? 1.0
|
|
|
|
: 1 - (remainingTime / totalDuration),
|
2021-12-28 14:33:27 +00:00
|
|
|
backgroundColor: Theme.of(context).colorScheme.secondary,
|
2019-11-02 19:14:34 +00:00
|
|
|
valueColor: AlwaysStoppedAnimation<Color>(
|
|
|
|
Theme.of(context).backgroundColor),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|