nagendra nag
nagendra nag

Reputation: 1332

Splash screen with auto route

I want to implement an animated splash screen

Generally in MaterialApp, we have a property called home but when I use autoroute (MaterialApp.router) am unable to find a home in it then how can I implement a splash screen with autoroute

package link

main.dart file

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();

  runApp(
    EasyLocalization(
      supportedLocales: const <Locale>[Locale('en')],
      path: 'assets/i18n',
      fallbackLocale: const Locale('en'),
      child: const App(),
    ),
  );
}
class Appp extends StatefulWidget {
  const Appp({Key? key}) : super(key: key);

  @override
  State<Appp> createState() =>
      _ApppState();
}

class _ApppState extends State<Appp> {
  final _themeStore = ThemeStore();
  final _appRouter = AppRouter();

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      debugShowCheckedModeBanner: false,
      title: 'App',
      theme: _themeStore.selectedThemeData,
      routerDelegate: _appRouter.delegate(),
      routeInformationParser: _appRouter.defaultRouteParser(),
    );
  }
}

Upvotes: 2

Views: 3618

Answers (1)

iamkj
iamkj

Reputation: 63

First You have to show the Splash screen and then for the next screen, you can use MaterialApp.router

eg.

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

class SplashScreen extends StatelessWidget {
  SplashScreen({Key? key}) : super(key: key);
  final _appRouter = AppRouter();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Splash App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: AnimatedSplashScreen(
        animationDuration: const Duration(seconds: 2),
        splashTransition: SplashTransition.slideTransition,
        splash: Icons.code,
        nextScreen: MaterialApp.router(
          routeInformationParser: _appRouter.defaultRouteParser(),
          routerDelegate: _appRouter.delegate(),
        ),
      ),
    );
  }
}

Packages Used:

Upvotes: 6

Related Questions