WidgetsBindingObserver class

Interface for classes that register with the Widgets layer binding.

See WidgetsBinding.addObserver and WidgetsBinding.removeObserver.

This class can be extended directly, to get default behaviors for all of the handlers, or can used with the implements keyword, in which case all the handlers must be implemented (and the analyzer will list those that have been omitted).

Sample code

This StatefulWidget implements the parts of the State and WidgetsBindingObserver protocols necessary to react to application lifecycle messages. See didChangeAppLifecycleState.

class AppLifecycleReactor extends StatefulWidget {
  const AppLifecycleReactor({ Key key }) : super(key: key);

  @override
  _AppLifecycleReactorState createState() => new _AppLifecycleReactorState();
}

class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  AppLifecycleState _notification;

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

  @override
  Widget build(BuildContext context) {
    return new Text('Last notification: $_notification');
  }
}

To respond to other notifications, replace the didChangeAppLifecycleState method above with other methods from this class.

Constructors

WidgetsBindingObserver()

Properties

hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

didChangeAppLifecycleState(AppLifecycleState state) → void
Called when the system puts the app in the background or returns the app to the foreground. [...]
didChangeLocale(Locale locale) → void
Called when the system tells the app that the user's locale has changed. For example, if the user changes the system language settings. [...]
didChangeMetrics() → void
Called when the application's dimensions change. For example, when a phone is rotated. [...]
didChangeTextScaleFactor() → void
Called when the platform's text scale factor changes. [...]
didHaveMemoryPressure() → void
Called when the system is running low on memory. [...]
didPopRoute() Future<bool>
Called when the system tells the app to pop the current route. For example, on Android, this is called when the user presses the back button. [...]
didPushRoute(String route) Future<bool>
Called when the host tells the app to push a new route onto the navigator. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited