keyur
keyur

Reputation: 181

time count continue update when app is lock in flutter

How can I count time in when the app is locked because I need to navigate to the screen after the second is over can anyone help me in this task

Upvotes: 1

Views: 1097

Answers (1)

aoiTenshi
aoiTenshi

Reputation: 667

First of all, you need to determine the lifecylestate if app is locked or not. You can have a look at https://api.flutter.dev/flutter/widgets/WidgetsBindingObserver/didChangeAppLifecycleState.html How you use it is: Add with WidgetsBindingObserver to your Stateful Widget like

class _MyState extends State<MyStatefulWidget>
    with WidgetsBindingObserver

then use didChangeAppLifecycleState like this:

    @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        super.didChangeAppLifecycleState(state);
        switch (state) {
          case AppLifecycleState.resumed:    
            //make your operations        
            break;
          case AppLifecycleState.inactive: 
//make your operations          
            break;
          case AppLifecycleState.paused: 
//make your operations           
            break;
          case AppLifecycleState.detached: 
//make your operations          
            break;
          default:
            break;
        }
      }

When coming to counting time, look at flutter_countdown_timer package https://pub.dev/packages/flutter_countdown_timer Implement it and when app is paused, start your timer. When it finishes, do your operations and under resume, reset your countdown timer. Note that, you only need to write this in 1 of your StatefulWidgets, it works globally.

Upvotes: -2

Related Questions