Getx flutter when i change theme from light to dark mode and then return to light mode its not returning my custom theme its load old app bar background even theme changes
ThemeData lightTheme = ThemeData(
useMaterial3: true,
backgroundColor: Colors.white,
appBarTheme: AppBarTheme(
elevation: 1000,
backgroundColor: Colors.white,
)
);
ThemeData darkTheme = ThemeData.dark().copyWith(primaryColor: Colors.red);
Getx flutter when i change theme from light to dark mode and then return to light mode its not returning my custom theme its load old app bar background even theme changes
class _MyMaterialAppState extends State<MyMaterialApp> {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: appName,
theme: lightTheme,
darkTheme: darkTheme,
translations: Languages(),
locale: Get.deviceLocale,
fallbackLocale: const Locale('en', 'US'),
home: const MyHomePage(),
);
}
}
2
Answers
Probably, this is because of the
GetMaterialApp
widget in thebuild
method. Because thebuild
method is called every time the widget is updated, this means that your custom theme will be overwritten. You can do something like this:This code uses
GetBuilder
to listen for changes to the_themeController
and updatetheme
property.To change the theme:
The documentation of
GetX
Itself says that you should not depend on any higher level widget thanGetMaterialApp
in order to update it. This can trigger duplicate keys.I have seen your code and I tried to test this on physical devide and it works perfectly fine.
Here’s the code: