0

I am implementing switch to change Theme in my application. As switch value I am using Theme.of(context).brightness == Brightness.dark and switch it's not working (I can drag switch but he is coming back to his position)

When I change value to Theme.of(context).brightness == Brightness.light switch is working but I need it in first option.

here is my switch code:

Switch(
                              value: Theme.of(context).brightness == Brightness.dark,
                              onChanged: (brightness) async {
                                print(brightness);
                                await MySecureStorage().writeData('theme', brightness ? 'dark' : 'light');
                                Provider.of<MainProvider>(context, listen: false).changeThemeMode(brightness ? ThemeMode.light : ThemeMode.dark);
                                SystemChrome.setSystemUIOverlayStyle(
                                                     SystemUiOverlayStyle(
                                                         statusBarColor: Colors.transparent,
                                                         statusBarIconBrightness: brightness
                                                             ? Brightness.dark
                                                             : Brightness.light
                                                     )
                                );
                              },
                            )

and why is compared to Brightness.dark it's not working? What I am missing?

  • will always return true/false based on what was provided to the MaterialApp.themeMode. check this [link](https://stackoverflow.com/questions/67794181/how-to-change-theme-in-flutter) –  Aug 22 '22 at 11:56
  • But why when I set value to ```== Brightness.light``` switch is working and it's changing theme? – Karol Wiśniewski Aug 22 '22 at 12:00

1 Answers1

0

Problem solved, it's embarrasing but I didn't change parameters in Provider function. It was the problem