TopStatelessWidget class Null safety

Used instead of StatelessWidget on top of MaterialApp widget to listen to InjectedI18N and InjectedTheme

It disposes all non auto disposed injected model when the app closes.

Useful also to dispose resources and reset injected states for test.

It can also be used to display a splash screen while initialization plugins.

These are the Hooks offered by this widget: TopStatelessWidget.ensureInitialization, TopStatelessWidget.splashScreen, TopStatelessWidget.errorScreen, TopStatelessWidget.didMountWidget, TopStatelessWidget.didUnmountWidget and TopStatelessWidget.didChangeAppLifecycleState

Example of TopAppWidget used to provide InjectedTheme and InjectedI18N

 void main() {
   runApp(MyApp());
 }

 class MyApp extends TopStatelessWidget {
   // This widget is the root of your application.
   @override
   Widget build(BuildContext context) {
     return MaterialApp(
       //
       theme: themeRM.lightTheme, //light theme
       darkTheme: themeRM.darkTheme, //dark theme
       themeMode: themeRM.themeMode, //theme mode
       //
       locale: i18nRM.locale,
       localeResolutionCallback: i18nRM.localeResolutionCallback,
       localizationsDelegates: i18nRM.localizationsDelegates,
       home: HomePage(),
     );
   }
 }

Example of initializing plugins

In Flutter it is common to initialize plugins inside the main method:

void main()async{
 WidgetsFlutterBinding.ensureInitialized();

 await initializeFirstPlugin();
 await initializeSecondPlugin();
 runApp(MyApp());
}

If you want to initialize plugins and display splash screen while waiting for them to initialize and display an error screen if any of them fails to initialize or request for permission with the ability to retry the initialization you can use TopStatelessWidget:

class MyApp extends TopStatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  List<Future<void>>? ensureInitialization() {
    return [
      initializeFirstPlugin(),
      initializeSecondPlugin(),
    ];
  }

  @override
  Widget? splashScreen() {
    return Material(
      child: Scaffold(
        body: Center(
          child: CircularProgressIndicator(),
        ),
      ),
    );
  }

  @override
  Widget? errorScreen(error, void Function() refresh) {
    return ElevatedButton.icon(
      onPressed: () => refresh(),
      icon: Icon(Icons.refresh),
      label: Text('Retry again'),
    );
  }

  @override
  Widget build(BuildContext context) {
    return MyHomePage();
  }
}

To invoke side effects depending on the app life cycle,

class MyApp extends TopStatelessWidget {
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print(state);
  }

  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
Inheritance
Implementers

Constructors

TopStatelessWidget({Key? key})
const

Properties

hashCode int
The hash code for this object. [...]
@nonVirtual, read-only, inherited
key Key?
Controls how one widget replaces another widget in the tree. [...]
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

build(BuildContext context) Widget
createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree. [...]
inherited
createState() → _TopStatelessWidgetState
Creates the mutable state for this widget at a given location in the tree. [...]
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children. [...]
@protected, inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node. [...]
inherited
didChangeAppLifecycleState(AppLifecycleState state) → void
Called when the system puts the app in the background or returns the app to the foreground. [...]
didMountWidget() → void
Called when the widget is first inserted in the widget tree
didUnmountWidget() → void
Called when the widget is removed from the widget tree
ensureInitialization() List<Future<void>>?
List of future (plugins initialization) to wait for, and display a waiting screen while waiting
errorScreen(dynamic error, void refresh()) Widget?
Hook to be called if initialization fails.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
splashScreen() Widget?
Hook to be called while waiting for plugins initialization.
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep. [...]
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object. [...]
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants. [...]
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object. [...]
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator. [...]
@nonVirtual, inherited