1
votes

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
Welcome to Stack Overflow! Please take the tour and read up on How to Ask. Great that you've got something you want to do, but why did you post it here? SO is for questions and you haven't asked anything. Please edit the post to actually ask one. - Adriaan

1 Answers

1
votes

You can use mixin WidgetsBindingObserver to observe the state of the app.

  1. Widget
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver
  1. initState()
WidgetsBinding.instance.addObserver(this);
  1. dispose()
WidgetsBinding.instance.removeObserver(this);
  1. 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) {

  }
}
  1. Check the state and track whether the user is active or not.