SimpleBoilerplates / Flutter

A boilerplate project for Flutter using RiverPod, Dio, go_router, Freezed and generated with very_good_cli
MIT License
566 stars 103 forks source link

Add more pages to the app. #26

Open paakjis opened 1 year ago

paakjis commented 1 year ago

Hi. Im new to AutoRoute, I cant figure out how to make more pages with this boilerplate. So I made something liek this.

@AdaptiveAutoRouter(
  replaceInRouteName: 'Page,Route',
  routes: <AutoRoute>[
    //RedirectRoute(path: '*', redirectTo: '/'),
    AutoRoute(page: AppStartPage, initial: true),
    AutoRoute(
      path: '/home',
      page: HomePage,
      children: <AutoRoute>[
        AutoRoute<FieldInfoPage>(
          name: 'FieldInfoRoute',
          path: 'fieldInfo',
          page: FieldInfoPage,
        ),
      ],
    ),
    signInRouter,
    signUpRouter,
  ],
)

But the problem here is that after flutters "hot reload" it triggers the AppStartPage, and opens the HomePage again.

How do I make it so it doesn't open HomePage everytime it reloads?

paakjis commented 1 year ago

I got it. After hours of seraching. The App Widget needs to be StatefulWidget for the hot-reload to work properly.

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  final _appRouter = AppRouter();

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      theme: ThemeData(
        appBarTheme: const AppBarTheme(color: Color.blue),
        colorScheme: ColorScheme.fromSwatch(accentColor: Color.blue),
        // fontFamily: 'Roboto',
      ),
      routerDelegate: AutoRouterDelegate(
        _appRouter,
        navigatorObservers: () => [AppRouteObserver()],
      ),
      routeInformationProvider: _appRouter.routeInfoProvider(),
      routeInformationParser: _appRouter.defaultRouteParser(),
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
      ],
      useInheritedMediaQuery: true,
      supportedLocales: AppLocalizations.supportedLocales,
      builder: (BuildContext context, Widget? child) {
        final data = MediaQuery.of(context);

        return MediaQuery(
          data: data.copyWith(
            textScaleFactor: 1,
          ),
          child: child!,
        );
      },
    );
  }
}