'Flutter GetX Changing Theme needs Hot Reload?

hi guys I'm using the flutter GetX package to change my app ThemeMode. it works fine but the problem is it needs a hot reload to change the ThemeMode here is my code

changeThemeMode() {darkModeSwitch.value == true? Get.changeTheme(Apptheme.dark): Get.changeTheme(Apptheme.light);}



Solution 1:[1]

No, you don't need it.

Make ensure which you are using GetMaterialApp instead of MaterialApp. Here's how I got it:

 class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

I create a changeTheme method to do it

 void changeTheme() {
    Get.changeTheme(Get.isDarkMode ? ThemeData.light() : ThemeData.dark());
  }

And I call him in the page like that:

floatingActionButton: FloatingActionButton(
  onPressed: changeTheme,
  child: Icon(Icons.add),
), 

That's works for me.

Solution 2:[2]

 @override
  void onInit() async {
    super.onInit();
    isDarkMode = settings.read('isDarkMode') ?? false;
    ever(_isDarkMode, (_) => settings.write('isDarkMode', isDarkMode));
    ever(
        _isDarkMode,
        (_) => Get.changeTheme(
            _isDarkMode.value ? getDarkTheme() : getLightTheme()));
    
  }

...

ThemeData getDarkTheme() {
  return darkTheme;
}
ThemeData getLightTheme() {
  return lightTheme;
}

This code is an example of using getStorage to save setting values to a local device and turning on/off dark mode by manipulating a switch.

 ever(
        _isDarkMode,
        (_) => Get.changeTheme(
            _isDarkMode.value ? getDarkTheme() : getLightTheme()));

This may help you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Caio Jesus
Solution 2 Hyungju Moon