I want do build an app in flutter that is capable to track the ON screen activity, meaning that it would be able to track the total time for which the user was active. The motivation behind this is I want to build an app that can tell user for how he is using his device.
1
votes
1 Answers
1
votes
You can use mixin WidgetsBindingObserver to observe the state of the app.
- Widget
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver
- initState()
WidgetsBinding.instance.addObserver(this);
- dispose()
WidgetsBinding.instance.removeObserver(this);
- override didChangeAppLifecycleState()
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.inactive) {
} else if (state == AppLifecycleState.paused) {
} else if (state == AppLifecycleState.resumed) {
} else if (state == AppLifecycleState.detached) {
}
}
- Check the state and track whether the user is active or not.