Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onResume() and onPause() for widgets on Flutter

Right now, a widget only has initeState() that gets triggered the very first time a widget is created, and dispose(), which gets triggered when the widget is destroyed. Is there a method to detect when a widget comes back to the foreground? and when a widget is about to go to the background because another widget just was foregrounded? It's the equivalent of onResume and onPause being triggered for Android, and viewWillAppear and viewWillDisappear for ios

like image 709
user3217522 Avatar asked Jun 02 '17 14:06

user3217522


People also ask

How do widgets work in Flutter?

Flutter widgets are built using a modern framework that takes inspiration from React. The central idea is that you build your UI out of widgets. Widgets describe what their view should look like given their current configuration and state.

How many types of widget are there in Flutter?

Types of Widgets: There are broadly two types of widgets in the flutter: Stateless Widget. Stateful Widget.


1 Answers

There is an abstract class caller WidgetsBindingObserver

https://docs.flutter.io/flutter/widgets/WidgetsBindingObserver-class.html

in

  @override   void didChangeAppLifecycleState(AppLifecycleState state) {     setState(() {             _notification = state;     });   } 

there is the "state", can be manage as

switch(state) {   case AppLifecycleState.resumed:     // Handle this case     break;   case AppLifecycleState.inactive:     // Handle this case     break;   case AppLifecycleState.paused:     // Handle this case     break;   case AppLifecycleState.suspending:     // Handle this case     break; } 
like image 127
Mamnarock Avatar answered Sep 19 '22 00:09

Mamnarock