Home > Mobile >  Does not change the icon after switching the theme
Does not change the icon after switching the theme

Time:12-05

There is an animated_theme_switcher package and assigned a code to change the icon after switching, but it does not change .. what code is needed? Help please .. I will be grateful my code:

return IconButton(
                                        onPressed: () async {
                                          var themeName =
                                          ThemeModelInheritedNotifier.of(context)
                                              .theme
                                              .brightness ==
                                              Brightness.light
                                              ? 'dark'
                                              : 'light';
                                          var service = await ThemeService.instance
                                            ..save(themeName);
                                          var theme = service.getByName(themeName);
                                          ThemeSwitcher.of(context).changeTheme(theme: theme);
                                        },
                                        icon: Icon(ThemeModelInheritedNotifier.of(context).theme == Brightness.light ? Icons.light_mode_outlined : Icons.dark_mode_outlined),
                                      );

CodePudding user response:

Wrap the screen where you whant to make them switch with ThemeSwitchingArea widget, as it has shown in the following example:

ThemeSwitchingArea(
        child: Builder(builder: (context)
    {
      return IconButton(
        onPressed: () async {
          var themeName =
          ThemeModelInheritedNotifier
              .of(context)
              .theme
              .brightness ==
              Brightness.light
              ? 'dark'
              : 'light';
          var service = await ThemeService.instance
            ..save(themeName);
          var theme = service.getByName(themeName);
          ThemeSwitcher.of(context).changeTheme(theme: theme);
        },
        icon: Icon(ThemeModelInheritedNotifier
            .of(context)
            .theme == Brightness.light ? Icons.light_mode_outlined : Icons.dark_mode_outlined),
      );
    },);

CodePudding user response:

in this case, you can use this package, I have used it and it works well

https://pub.dev/packages/adaptive_theme

  • Related