Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to keep state of AutoTabsRouter #2105

Open
giordy16 opened this issue Dec 9, 2024 · 0 comments
Open

How to keep state of AutoTabsRouter #2105

giordy16 opened this issue Dec 9, 2024 · 0 comments

Comments

@giordy16
Copy link

giordy16 commented Dec 9, 2024

Hi, I want to use a ValueListenableBuilder to handle theme change on my app, but every time it's get triggered and rebuilds the app, AutoTabsRouter looses the current page. In this example, when I change the theme from the Switch, I am brought back to Home, but I want to stay in Settings

ValueNotifier<ThemeData> themeNotifier = ValueNotifier(ThemeData.light());

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(ValueListenableBuilder(
      valueListenable: themeNotifier,
      builder: (context, theme, child) {
        return MaterialApp.router(
          theme: theme,
          routerConfig: AppRouter().config(),
        );
      }));
}

@RoutePage()
class MainLandingScreen extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return AutoTabsRouter(
      routes: [
        HomeRoute(),
        SettingsRoute(),
      ],
      builder: (context, child) {
        final tabsRouter = AutoTabsRouter.of(context);

        return Scaffold(
          body: child,
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: tabsRouter.activeIndex,
            onTap: tabsRouter.setActiveIndex,
            type: BottomNavigationBarType.fixed,
            selectedLabelStyle: const TextStyle(fontSize: 12),
            unselectedLabelStyle: const TextStyle(fontSize: 12),
            items: [
              BottomNavigationBarItem(
                  label: 'Home', icon: Icon(Icons.home_outlined)),
              BottomNavigationBarItem(
                  label: 'Settings', icon: Icon(Icons.settings)),
            ],
          ),
        );
      },
    );
  }
}

@RoutePage()
class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Center(child: Text("Home")));
  }
}

@RoutePage()
class SettingsScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
        body: Center(
      child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: [
            Switch(
                value: themeNotifier.value == ThemeData.light(),
                onChanged: (value) {
                  themeNotifier.value =
                      value ? ThemeData.light() : ThemeData.dark();
                })
          ]),
    ));
  }
}


@AutoRouterConfig(replaceInRouteName: 'Screen|Page,Route')
class AppRouter extends RootStackRouter {
  @override
  RouteType get defaultRouteType => RouteType.material();

  @override
  List<AutoRoute> get routes => [
        AutoRoute(
          page: MainLandingRoute.page,
          initial: true,
          children: [
            AutoRoute(page: HomeRoute.page),
            AutoRoute(page: SettingsRoute.page),
          ],
        )
      ];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant