BirjuVachhani / adaptive_theme

Easiest way to add support for light and dark theme in your flutter app.
https://pub.dev/packages/adaptive_theme
Apache License 2.0
464 stars 37 forks source link

Unexpected behavior with go_router #51

Closed mpenza closed 10 months ago

mpenza commented 10 months ago

When adaptive_theme is used with go_router, and the theme changes, the router is re-initialized and returns to the default route.

Here is my root app in main.dart.

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return AdaptiveTheme(
      light: MyTheme.buildTheme(Brightness.light),
      dark: MyTheme.buildTheme(Brightness.dark),
      debugShowFloatingThemeButton: true,
      initial: AdaptiveThemeMode.system,
      builder: (theme, darkTheme) => MaterialApp.router(
        title: 'Project Snowcone',
        routerConfig: MyRouter.createRouter(),
        theme: theme,
        darkTheme: darkTheme,
      ),
    );
  }
}

Everything work fine before integrating go_router and widgets would update dynamically on theme change.

BirjuVachhani commented 10 months ago

Seems like MyRouter.createRouter() may have caused this issue but I am not sure. Try making this into a stateful widget and assign MyRouter.createRouter() to a variable in the state and use that variable here instead!

mpenza commented 10 months ago

That fixed it. Thank you!