1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-30 00:36:28 +00:00
comunicmobile/lib/ui/widgets/countdown_widget.dart

88 lines
2.1 KiB
Dart
Raw Normal View History

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 {
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) {
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),
),
)
],
),
);
}
}