How to show 5 min unstoppable timer in flutter app?
I want to show a 5 min timer in my app which does't stop even when the app is closed and show the time left in mm:ss format. How can I show the time?
asynchronous timer dart

add a comment |
I want to show a 5 min timer in my app which does't stop even when the app is closed and show the time left in mm:ss format. How can I show the time?
asynchronous timer dart

You'll need a persistent storage then, take a look at this plugin for instance: flutter.io/docs/cookbook/persistence/key-value and store the initial time and the value for the timer.
– cy3er
Jan 1 at 20:15
Persisting isn't my concern, how do I show the time left?
– Keerti Purswani
Jan 2 at 7:18
add a comment |
I want to show a 5 min timer in my app which does't stop even when the app is closed and show the time left in mm:ss format. How can I show the time?
asynchronous timer dart

I want to show a 5 min timer in my app which does't stop even when the app is closed and show the time left in mm:ss format. How can I show the time?
asynchronous timer dart

asynchronous timer dart

asked Jan 1 at 18:30


Keerti PurswaniKeerti Purswani
1648
1648
You'll need a persistent storage then, take a look at this plugin for instance: flutter.io/docs/cookbook/persistence/key-value and store the initial time and the value for the timer.
– cy3er
Jan 1 at 20:15
Persisting isn't my concern, how do I show the time left?
– Keerti Purswani
Jan 2 at 7:18
add a comment |
You'll need a persistent storage then, take a look at this plugin for instance: flutter.io/docs/cookbook/persistence/key-value and store the initial time and the value for the timer.
– cy3er
Jan 1 at 20:15
Persisting isn't my concern, how do I show the time left?
– Keerti Purswani
Jan 2 at 7:18
You'll need a persistent storage then, take a look at this plugin for instance: flutter.io/docs/cookbook/persistence/key-value and store the initial time and the value for the timer.
– cy3er
Jan 1 at 20:15
You'll need a persistent storage then, take a look at this plugin for instance: flutter.io/docs/cookbook/persistence/key-value and store the initial time and the value for the timer.
– cy3er
Jan 1 at 20:15
Persisting isn't my concern, how do I show the time left?
– Keerti Purswani
Jan 2 at 7:18
Persisting isn't my concern, how do I show the time left?
– Keerti Purswani
Jan 2 at 7:18
add a comment |
1 Answer
1
active
oldest
votes
Here's a very very rudimentary example of how such a timer could work which persistently stores the target time:
class TimerApp extends StatefulWidget {
@override
_TimerAppState createState() => _TimerAppState();
}
class _TimerAppState extends State<TimerApp> {
SharedPreferences prefs;
DateTime target;
String timeLeft = "";
bool running = true;
@override
void initState() async {
super.initState();
prefs = await SharedPreferences.getInstance();
target = DateTime.fromMillisecondsSinceEpoch(prefs.getInt('target'));
if (target == null || target < DateTime.now()) {
target = DateTime.now().add(Duration(minutes: 5));
}
executeTimer();
}
@override
void dispose() {
prefs.setInt('target', target.millisecondsSinceEpoch);
running = false;
super.dispose();
}
void executeTimer() async {
while (running) {
setState(() {
timeLeft = DateTime.now().isAfter(target)
? '5 min expired. Restart app to reset.'
: target.difference(DateTime.now()).toString();
});
await Future.delayed(Duration(seconds: 1), () {});
}
}
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Text(timeLeft),
);
}
}
Note that this example is not very fleshed out; several features are missing and using a while (running)
loop is probably not the most elegant solution.
Here are some more resources you could have a look at:
- The shared_preferences package for saving state persistently.
- This Fluttery egg timer tutorial that uses a more sophisticated form of state management.
This will definitely help! Thanks a lot :) I will try this out.
– Keerti Purswani
Jan 3 at 18:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997910%2fhow-to-show-5-min-unstoppable-timer-in-flutter-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's a very very rudimentary example of how such a timer could work which persistently stores the target time:
class TimerApp extends StatefulWidget {
@override
_TimerAppState createState() => _TimerAppState();
}
class _TimerAppState extends State<TimerApp> {
SharedPreferences prefs;
DateTime target;
String timeLeft = "";
bool running = true;
@override
void initState() async {
super.initState();
prefs = await SharedPreferences.getInstance();
target = DateTime.fromMillisecondsSinceEpoch(prefs.getInt('target'));
if (target == null || target < DateTime.now()) {
target = DateTime.now().add(Duration(minutes: 5));
}
executeTimer();
}
@override
void dispose() {
prefs.setInt('target', target.millisecondsSinceEpoch);
running = false;
super.dispose();
}
void executeTimer() async {
while (running) {
setState(() {
timeLeft = DateTime.now().isAfter(target)
? '5 min expired. Restart app to reset.'
: target.difference(DateTime.now()).toString();
});
await Future.delayed(Duration(seconds: 1), () {});
}
}
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Text(timeLeft),
);
}
}
Note that this example is not very fleshed out; several features are missing and using a while (running)
loop is probably not the most elegant solution.
Here are some more resources you could have a look at:
- The shared_preferences package for saving state persistently.
- This Fluttery egg timer tutorial that uses a more sophisticated form of state management.
This will definitely help! Thanks a lot :) I will try this out.
– Keerti Purswani
Jan 3 at 18:13
add a comment |
Here's a very very rudimentary example of how such a timer could work which persistently stores the target time:
class TimerApp extends StatefulWidget {
@override
_TimerAppState createState() => _TimerAppState();
}
class _TimerAppState extends State<TimerApp> {
SharedPreferences prefs;
DateTime target;
String timeLeft = "";
bool running = true;
@override
void initState() async {
super.initState();
prefs = await SharedPreferences.getInstance();
target = DateTime.fromMillisecondsSinceEpoch(prefs.getInt('target'));
if (target == null || target < DateTime.now()) {
target = DateTime.now().add(Duration(minutes: 5));
}
executeTimer();
}
@override
void dispose() {
prefs.setInt('target', target.millisecondsSinceEpoch);
running = false;
super.dispose();
}
void executeTimer() async {
while (running) {
setState(() {
timeLeft = DateTime.now().isAfter(target)
? '5 min expired. Restart app to reset.'
: target.difference(DateTime.now()).toString();
});
await Future.delayed(Duration(seconds: 1), () {});
}
}
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Text(timeLeft),
);
}
}
Note that this example is not very fleshed out; several features are missing and using a while (running)
loop is probably not the most elegant solution.
Here are some more resources you could have a look at:
- The shared_preferences package for saving state persistently.
- This Fluttery egg timer tutorial that uses a more sophisticated form of state management.
This will definitely help! Thanks a lot :) I will try this out.
– Keerti Purswani
Jan 3 at 18:13
add a comment |
Here's a very very rudimentary example of how such a timer could work which persistently stores the target time:
class TimerApp extends StatefulWidget {
@override
_TimerAppState createState() => _TimerAppState();
}
class _TimerAppState extends State<TimerApp> {
SharedPreferences prefs;
DateTime target;
String timeLeft = "";
bool running = true;
@override
void initState() async {
super.initState();
prefs = await SharedPreferences.getInstance();
target = DateTime.fromMillisecondsSinceEpoch(prefs.getInt('target'));
if (target == null || target < DateTime.now()) {
target = DateTime.now().add(Duration(minutes: 5));
}
executeTimer();
}
@override
void dispose() {
prefs.setInt('target', target.millisecondsSinceEpoch);
running = false;
super.dispose();
}
void executeTimer() async {
while (running) {
setState(() {
timeLeft = DateTime.now().isAfter(target)
? '5 min expired. Restart app to reset.'
: target.difference(DateTime.now()).toString();
});
await Future.delayed(Duration(seconds: 1), () {});
}
}
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Text(timeLeft),
);
}
}
Note that this example is not very fleshed out; several features are missing and using a while (running)
loop is probably not the most elegant solution.
Here are some more resources you could have a look at:
- The shared_preferences package for saving state persistently.
- This Fluttery egg timer tutorial that uses a more sophisticated form of state management.
Here's a very very rudimentary example of how such a timer could work which persistently stores the target time:
class TimerApp extends StatefulWidget {
@override
_TimerAppState createState() => _TimerAppState();
}
class _TimerAppState extends State<TimerApp> {
SharedPreferences prefs;
DateTime target;
String timeLeft = "";
bool running = true;
@override
void initState() async {
super.initState();
prefs = await SharedPreferences.getInstance();
target = DateTime.fromMillisecondsSinceEpoch(prefs.getInt('target'));
if (target == null || target < DateTime.now()) {
target = DateTime.now().add(Duration(minutes: 5));
}
executeTimer();
}
@override
void dispose() {
prefs.setInt('target', target.millisecondsSinceEpoch);
running = false;
super.dispose();
}
void executeTimer() async {
while (running) {
setState(() {
timeLeft = DateTime.now().isAfter(target)
? '5 min expired. Restart app to reset.'
: target.difference(DateTime.now()).toString();
});
await Future.delayed(Duration(seconds: 1), () {});
}
}
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Text(timeLeft),
);
}
}
Note that this example is not very fleshed out; several features are missing and using a while (running)
loop is probably not the most elegant solution.
Here are some more resources you could have a look at:
- The shared_preferences package for saving state persistently.
- This Fluttery egg timer tutorial that uses a more sophisticated form of state management.
answered Jan 2 at 23:09


MarcelMarcel
1,026615
1,026615
This will definitely help! Thanks a lot :) I will try this out.
– Keerti Purswani
Jan 3 at 18:13
add a comment |
This will definitely help! Thanks a lot :) I will try this out.
– Keerti Purswani
Jan 3 at 18:13
This will definitely help! Thanks a lot :) I will try this out.
– Keerti Purswani
Jan 3 at 18:13
This will definitely help! Thanks a lot :) I will try this out.
– Keerti Purswani
Jan 3 at 18:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997910%2fhow-to-show-5-min-unstoppable-timer-in-flutter-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You'll need a persistent storage then, take a look at this plugin for instance: flutter.io/docs/cookbook/persistence/key-value and store the initial time and the value for the timer.
– cy3er
Jan 1 at 20:15
Persisting isn't my concern, how do I show the time left?
– Keerti Purswani
Jan 2 at 7:18