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

toggle theme on icon button click bug #42

Closed yagizdo closed 2 years ago

yagizdo commented 2 years ago

Describe the bug Thank you for preparing such a package first of all. When I assign the toggletheme code to the icon button, it does not detect some clicks. It works when I click it again.

When I first open the application, the theme does not change at the first click. It changes when I click it for the second time. Nothing happens on the first click.

Smartphone

Bug Video :

https://user-images.githubusercontent.com/31064552/177759508-d0773155-ed0c-44f7-9323-6a58b26ff256.mp4

Codes :

app.dart

 class MyApp extends StatelessWidget {
  MyApp({Key? key,required this.savedThemeMode}) : super(key: key);
  var savedThemeMode;
  @override
  Widget build(BuildContext context) {
    // App Theme
    final appTheme = AppTheme();
    return AdaptiveTheme(
      light: appTheme.lightTheme,
      dark: appTheme.darkTheme,
      initial: savedThemeMode ?? AdaptiveThemeMode.light,
      builder: (theme,darkTheme) => MaterialApp(
        title: 'Material App',
        theme: theme,
        darkTheme: darkTheme,
        home: const HomeView(),
      ),
    );
  }
} 

homeView.dart

class HomeView extends StatelessWidget {
  const HomeView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home View'),
        actions: [
          IconButton(
            icon: Icon(Icons.settings),
            onPressed: () {
              AdaptiveTheme.of(context).toggleThemeMode();
            },
          ),
        ],
      ),
      body: Center(
        child: Container(
          child: Text('Hello World'),
        ),
      ),
    );
  }
} 
BirjuVachhani commented 2 years ago

@yagizdo This is not a bug. Its an expected behavior. AdaptiveTheme supports 3 theme modes: Light, dart and System. Calling AdaptiveTheme. toggleThemeMode() goes through all 3.

So, for example, your OS is running in light mode and you are also on light mode in the app and then if you toggle theme mode, it may go from light -> system mode where system mode also is light so you see no change. Then when you toggle again, it may go to dark mode and you see the change.

It seems that you wanna toggle between only light and dark, not the system. So, instead of calling toggleThemeMode() do this:

final manager  = AdaptiveTheme.of(context);
if(manager.mode.isLight){
  manager.setDark();
}else {
  manager.setLight();
}
yagizdo commented 2 years ago

@yagizdo This is not a bug. Its an expected behavior. AdaptiveTheme supports 3 theme modes: Light, dart and System. Calling AdaptiveTheme. toggleThemeMode() goes through all 3.

So, for example, your OS is running in light mode and you are also on light mode in the app and then if you toggle theme mode, it may go from light -> system mode where system mode also is light so you see no change. Then when you toggle again, it may go to dark mode and you see the change.

It seems that you wanna toggle between only light and dark, not the system. So, instead of calling toggleThemeMode() do this:

final manager  = AdaptiveTheme.of(context);
if(manager.mode.isLight){
  manager.setDark();
}else {
  manager.setLight();
}

Oh okay I get it. Sorry my bad @BirjuVachhani

BirjuVachhani commented 2 years ago

@yagizdo Np. Glad I could help😊