Created
June 28, 2024 09:23
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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