Skip to content

Instantly share code, notes, and snippets.

@dipendra-sharma
Created June 28, 2024 09:23
Show Gist options
  • Save dipendra-sharma/044b66996d25d2cdeb86a7140e2d990e to your computer and use it in GitHub Desktop.
Save dipendra-sharma/044b66996d25d2cdeb86a7140e2d990e to your computer and use it in GitHub Desktop.
A lightweight, flexible Flutter widget for managing app initialisation with a customisable splash screen.
class AppInitializer extends StatefulWidget {
final Future Function() onAppInit;
final Widget Function(BuildContext) splashBuilder;
final Widget Function(BuildContext) appBuilder;
const AppInitializer(
{super.key,
required this.splashBuilder,
required this.appBuilder,
required this.onAppInit});
@override
State<AppInitializer> createState() => _AppInitializerState();
}
class _AppInitializerState extends State<AppInitializer> {
bool _initialized = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (!_initialized) {
await widget.onAppInit();
setState(() {
_initialized = true;
});
}
});
}
@override
Widget build(BuildContext context) {
return _initialized
? widget.appBuilder(context)
: widget.splashBuilder(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment