Measuring amount of time the app has been open - flutter

I am trying to trigger an event after the app is open for ten minutes. Is there an easy way to do this? I imagine I need to start a timer when the app is first built, but then be able to cancel or pause that timer if the user navigates away from the app somehow.
I have found the screen state library, but that library only listens for the screen turning off and on, and not for events like navigating home or to another app. I'm familiar with WillPopScope, and related to that I found back button interceptor, but my understanding is that only intercepts when the user presses the back button, and not if the user presses home or switches to another app.
Is there some central way for listening to anything that will close or navigate away from the app, or a combination of things to listen to?

Start a Timer when your main method runs:
import 'dart:async';
void main() {
Timer(Duration(minutes: 10), () {
// Handle the event
});
runApp(MyApp());
}
If you want to be able to control the timer, set it up in your root widget and have that widget listen to lifecycle events:
class MyApp extends StatefulWidget {
...
}
class MyAppState extends State<MyApp> with WidgetsBindingObserver {
static const _appTimerDuration = const Duration(minutes: 10);
Timer appTimer;
#override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
appTimer = Timer(_appTimerDuration, _timerElapsed);
}
#override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
appTimer?.cancel();
super.dispose();
}
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
appTimer = Timer(_appTimerDuration, _timerElapsed);
} else {
appTimer?.cancel();
}
}
void _timerElapsed() {
// Handle the event
}
...
}

Related

Flutter Timer app using shared preferences

how to store starting and ending time of a timer using shared preference in flutter and when i close the app and reopen app should start from ending timer time
if anyone knows anything regarding this issue please share
By using WidgetsBindingObserver you can listen to your application state.
class MyPage extends StatefulWidget {
#override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> with WidgetsBindingObserver {
#override
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
}
#override
void dispose() {
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
// Here you will get your application state like resumed, inactive, paused, detached
// when the application will close `detached` state will come, and store the latest timer data in your shared preferences
if(state == AppLifecycleState.detached){
// store last timer data into shared preferences
}
print('Current state = $state');
}
#override
Widget build(BuildContext context) => Scaffold();
}
and when your application starts :
Get the data from Shared preferences and Start the timer from there
Do this thing after initializing the shared preferences in main.dart or in your root widget.

Call a function when user leaves current screen in Flutter [duplicate]

This question already has answers here:
Detect if the user leaves the current page in Flutter?
(6 answers)
Closed 1 year ago.
I want to call a function or get notified when a user leaves the current screen whether he/she navigates back i.e pop the screen or navigate forward in the app.
You need to implement a route observer.
First you need to define an observer :
final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
then you need to add the observer to your material app :
MaterialApp(
navigatorObservers: [routeObserver], ...
Ok. Now when you create a screen, you can add RouteAware to your state. You will need to subscribe to the observer, and dispose it as well. Therefore you need a stateful widget. Let's say you have a screen called MyScreen, it would look like this :
class MyScreen extends StatefulWidget {
#override
_MyScreenState createState() => _MyScreenState();
}
class _MyScreenState extends State<MyScreen> with RouteAware{
#override
void didChangeDependencies() {
routeObserver.subscribe(this, ModalRoute.of(context));
super.didChangeDependencies();
}
#override
void dispose() {
routeObserver.unsubscribe(this);
super.dispose();
} ...
Great! Now you have access to these extra events such as :
#override
void didPopNext() {
super.didPopNext();
}
#override
void didPush() {
super.didPush();
}
#override
void didPushNext() {
super.didPushNext();
}
#override
void didPop() {
super.didPop();
}
Let me know if something was unclear.
you can use dispose() lifecycle function
quote from API
dispose method
Called when this object is removed from the tree permanently.
for example i'm using it to unsubscribe a Timer when user leaves the screen
example:
#override
void dispose() {
super.dispose();
timer.cancel();
}

Flutter | Stop music when minimised using audioplayers

In my app, I am playing music (local) in a loop, which plays continuously unless the user stops it. I am using audioplayers package.
Future playLoop(String filePath) async {
player.stop();
player = await cache.loop(filePath);
}
Currently, when app is minimised, the music is not getting stoped. The feature I want to implement is that when the app is minimised, it should stop playing music in the background.
Thanks in advance.
Solutions :
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
//stop your audio player
}else{
print(state.toString());
}
}
#override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
There are mainly 4 states for it:
resumed: The application is visible and responding to user input.
inactive: The application is in an inactive state and is not receiving
user input.
paused: The application is not currently visible to the user, not
responding user input, and running in the background.
detached: The application is still hosted on a flutter engine but is
detached from any host views.
The solution above is correct, but some steps are needed before to get it
1 add WidgetsBindingObserver to your class
class AnyClass extends StatefulWidgets {
_AnyClassState createState() => _AnyClassState();
}
class _AnyClassState extends State<AnyClass> with
WidgetsBindingObserver {
Widget build(BuildContext context) {
return ...
}
}
2 Now it will work, we can added the methods inside class
class _AnyClassState extends State<AnyClass> with
WidgetsBindingObserver {
// ADD THIS AppLifecycleState VARIABLE
late AppLifecycleState appLifecycle;
// ADD THIS FUNCTION WITH A AppLifecycleState PARAMETER
didChangeAppLifecycleState(AppLifecycleState state) {
appLifecycle = state;
setStae(() {});
if(state == AppLifecycle.paused) {
// IF YOUT APP IS IN BACKGROUND...
// YOU CAN ADDED THE ACTION HERE
print('My app is in background');
}
}
// CREATE INITSTATE AND DISPOSE METHODS
initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Widget build(BuildContext context) {
return ...
}
}
NOW IT WILL WORK FINE!

didChangeAppLifecycleState doesn't work as expected

I hope I understand how didChangeAppLifecycleState worked correctly.
I have page A and page B . When I click the back device button from page B ( Navigator.of(context).pop(); ), I expect didChangeAppLifecycleState in pageA will get called, but it doesn't.
PageA
class _ABCState extends State<ABCrList> with WidgetsBindingObserver {
#override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
....
}
#override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
setState(() {
print(...);
});
}else{
print(state.toString());
}
}
....
This is the initState in pageA. The function used to call backend service.
#override
void initState() {
super.initState();
_bloc.getList(context); // return list and populate to ListView
});
}
The way you're thinking it is Android's way where onResume works, but in Flutter, things don't happen this way.
Generally, this gets called when the system puts the app in the background or returns the app to the foreground.
There are mainly 4 states for it:
resumed: The application is visible and responding to user input.
inactive: The application is in an inactive state and is not receiving user input.
paused: The application is not currently visible to the user, not responding user input, and running in the background.
detached: The application is still hosted on a flutter engine but is detached from any host views.
Edit:
When you're navigating to PageB from PageA, use something like:
Navigator.pushNamed(context, "/pageB").then((flag) {
if (flag) {
// you're back from PageB, perform your function here
setState(() {}); // you may need to call this if you want to update UI
}
});
And from PageB, you'll can use
Navigator.pop(context, true);

Flutter Capture event when app comes to Foreground from triggered Intent

I am trying to pull off some basic stuff here. Scenario: I am checking for GPS status on init() using isLocationServiceEnabled. If the GPS is off, I'm showing a popup that redirects to Location settings using AndroidIntent. If hit back without turning on the GPS, I want to capture the event when my app comes to foreground. I guessed it has to do with the lifecycle and tried like below, nothing gets print on the console
AppLifecycleState _notification;
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
setState(() {
_notification = state;
print('onResumed called 1');
print(_notification);
});
if( state == AppLifecycleState.resumed){
print('onResumed called 2');
}
}
Am I missing something here?
Did you extend class with WidgetsBindingObserver like so:
class _WhateverWidget extends State<WhateverWidget> with WidgetsBindingObserver
and then initialize an instance like so:
#override void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}
#override void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}