Created
July 4, 2019 18:43
-
-
Save stargazing-dino/361693a51d13d3c9e8138f906fce60cc to your computer and use it in GitHub Desktop.
countdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:mobile/utils/formatters.dart'; | |
class Countdown extends StatelessWidget { | |
const Countdown({ | |
Key key, | |
@required this.millisecondsToTime, | |
this.style, | |
this.textAlign, | |
this.onEnd, | |
}) : super(key: key); | |
final int millisecondsToTime; | |
final TextStyle style; | |
final TextAlign textAlign; | |
final Function onEnd; | |
@override | |
Widget build(BuildContext context) { | |
return StreamBuilder( | |
stream: Stream.periodic(Duration(seconds: 1), (i) => i), | |
builder: (BuildContext context, AsyncSnapshot<int> snapshot) { | |
int now = DateTime.now().millisecondsSinceEpoch; // Changes every tim | |
Duration remaining = Duration(milliseconds: (millisecondsToTime - now)); | |
if (remaining == Duration.zero && onEnd != null) onEnd(); | |
var dateString = | |
'${remaining.inHours}:${minuteSecondFormatter.format(DateTime.fromMillisecondsSinceEpoch(remaining.inMilliseconds))}'; | |
return Text( | |
dateString, | |
style: style, | |
textAlign: textAlign, | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment