RaiiWidgetsBindingObserver class

A lifecycle implementation that manages the lifecycle of a WidgetsBindingObserver.

This class automatically handles registration and removal of WidgetsBindingObservers with the WidgetsBinding instance. It's particularly useful for managing app lifecycle events, keyboard visibility changes, system settings changes, and other app-wide events.

See also WidgetsBindingRaiiExt.

Example:

// Basic app lifecycle observer
class AppLifecycleObserver with WidgetsBindingObserver {
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        print('App resumed');
        break;
      case AppLifecycleState.inactive:
        print('App inactive');
        break;
      case AppLifecycleState.paused:
        print('App paused');
        break;
      case AppLifecycleState.detached:
        print('App detached');
        break;
    }
  }
}

// Attach the observer
final lifecycleObserver = AppLifecycleObserver();
RaiiWidgetsBindingObserver.withLifecycle(
  lifecycleAware,
  lifecycleObserver,
  debugLabel: 'AppLifecycle',
);

// System settings observer
class SystemSettingsObserver with WidgetsBindingObserver {
  @override
  void didChangePlatformBrightness() {
    print('Brightness changed');
  }

  @override
  void didChangeLocales(List<Locale>? locales) {
    print('Locales changed');
  }
}

// Attach the system observer
final settingsObserver = SystemSettingsObserver();
RaiiWidgetsBindingObserver.withLifecycle(
  lifecycleAware,
  WidgetsBinding.instance,
  settingsObserver,
  debugLabel: 'SystemSettings',
);
Mixed-in types

Constructors

RaiiWidgetsBindingObserver.withLifecycle(RaiiLifecycleAware lifecycleAware, WidgetsBinding widgetsBinding, WidgetsBindingObserver observer, {String? debugLabel})
Creates a RaiiWidgetsBindingObserver and attaches it to the provided lifecycleAware.

Properties

debugLabel String?
Optional label for debugging purposes.
final
hashCode int
The hash code for this object.
no setterinherited
observer WidgetsBindingObserver
The observer that will be registered with WidgetsBinding.instance.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
widgetsBinding WidgetsBinding
Widgets Binding instance.
final

Methods

disposeLifecycle() → void
Disposes of any resources held by this object.
override
initLifecycle() → void
Initializes the lifecycle of this object.
override
isLifecycleMounted() bool
Returns whether this object's lifecycle is currently mounted.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited