TopStatelessWidget constructor
- Key? key,
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.activeTheme(),
//
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 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();
}
}
Implementation
const TopStatelessWidget({Key? key}) : super(key: key);