mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-26 06:49:22 +00:00
Add support for countdown posts
This commit is contained in:
parent
c714d24dfa
commit
bcf9a1586a
@ -15,6 +15,7 @@ import 'package:comunic/models/post.dart';
|
|||||||
import 'package:comunic/models/user.dart';
|
import 'package:comunic/models/user.dart';
|
||||||
import 'package:comunic/ui/tiles/comment_tile.dart';
|
import 'package:comunic/ui/tiles/comment_tile.dart';
|
||||||
import 'package:comunic/ui/widgets/account_image_widget.dart';
|
import 'package:comunic/ui/widgets/account_image_widget.dart';
|
||||||
|
import 'package:comunic/ui/widgets/countdown_widget.dart';
|
||||||
import 'package:comunic/ui/widgets/network_image_widget.dart';
|
import 'package:comunic/ui/widgets/network_image_widget.dart';
|
||||||
import 'package:comunic/ui/widgets/survey_widget.dart';
|
import 'package:comunic/ui/widgets/survey_widget.dart';
|
||||||
import 'package:comunic/utils/date_utils.dart';
|
import 'package:comunic/utils/date_utils.dart';
|
||||||
@ -176,6 +177,10 @@ class _PostTileState extends State<PostTile> {
|
|||||||
postContent = _buildPostWebLink();
|
postContent = _buildPostWebLink();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case PostKind.COUNTDOWN:
|
||||||
|
postContent = _buildCountDownTimer();
|
||||||
|
break;
|
||||||
|
|
||||||
case PostKind.SURVEY:
|
case PostKind.SURVEY:
|
||||||
postContent = _buildPostSurvey();
|
postContent = _buildPostSurvey();
|
||||||
break;
|
break;
|
||||||
@ -320,6 +325,13 @@ class _PostTileState extends State<PostTile> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _buildCountDownTimer() {
|
||||||
|
return CountdownWidget(
|
||||||
|
startTime: widget.post.timeSent,
|
||||||
|
endTime: widget.post.timeEnd,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// Build post survey
|
/// Build post survey
|
||||||
Widget _buildPostSurvey() {
|
Widget _buildPostSurvey() {
|
||||||
return SurveyWidget(
|
return SurveyWidget(
|
||||||
|
84
lib/ui/widgets/countdown_widget.dart
Normal file
84
lib/ui/widgets/countdown_widget.dart
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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),
|
||||||
|
backgroundColor: Theme.of(context).accentColor,
|
||||||
|
valueColor: AlwaysStoppedAnimation<Color>(
|
||||||
|
Theme.of(context).backgroundColor),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user