app_initializer 1.0.0 app_initializer: ^1.0.0 copied to clipboard
A Flutter widget that streamlines app initialization, managing splash screens, async tasks, and error handling
import 'package:app_initializer/app_initializer.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return AppInitializer(
splashBuilder: (context) => const SplashScreen(),
application: const MainApp(),
initializer: _initializeApp,
errorBuilder: (context) => const ErrorScreen(),
);
}
Future<void> _initializeApp() async {
// Simulate app initialization tasks
await Future.delayed(const Duration(seconds: 5));
// Uncomment the line below to simulate an initialization error
// throw Exception('Initialization failed');
}
}
class SplashScreen extends StatelessWidget {
const SplashScreen({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text('Loading...'),
],
),
),
),
);
}
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(primarySwatch: Colors.blue),
home: const Scaffold(
body: Center(child: Text('Welcome to My App!')),
),
);
}
}
class ErrorScreen extends StatelessWidget {
const ErrorScreen({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.error, color: Colors.red, size: 48),
const SizedBox(height: 16),
const Text('An error occurred during initialization'),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
// Add retry logic here
},
child: const Text('Retry'),
),
],
),
),
),
);
}
}