Skip to content

Instantly share code, notes, and snippets.

@TheAnimatrix
Created November 5, 2019 12:50
Show Gist options
  • Save TheAnimatrix/b2187ddb8362f41fc970e4bb9b0c0ced to your computer and use it in GitHub Desktop.
Save TheAnimatrix/b2187ddb8362f41fc970e4bb9b0c0ced to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'package:flutter/material.dart';
class CountdownList extends StatefulWidget {
@override
_CountdownListState createState() => _CountdownListState();
}
class _CountdownListState extends State<CountdownList> {
String countdownText = "";
@override
void initState() {
super.initState();
startTimer();
}
//parsing the duration difference into D : H : M : S
void parseCountdownToText() {
List<String> xs = countdownText.split(":");
int hours = int.parse(xs[0]) % 24;
int days = int.parse(xs[0]) ~/ 24;
int min = int.parse(xs[1]);
int s = double.parse(xs[2]).toInt();
countdownText = "$days\d $hours\h $min\m $s\s ";
print(countdownText);
}
//simple text widget to show countdown
@override
Widget build(BuildContext context) {
return Center(
child: Text(
"$countdownText",
style: TextStyle(fontFamily: "Nexa", fontSize: 30),
),
);
}
void startTimer() {
new Timer.periodic(Duration(seconds: 1), (Timer timer) {
setState(() {
countdownText = DateTime.fromMillisecondsSinceEpoch(1589049000000)
.difference(DateTime.now())
.abs()
.toString(); //random fixed future date
parseCountdownToText();
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment