getx
getx copied to clipboard
get 4.6.5.. Get.isDarkMode is always giving false value.
This code is working fine...
Get.changeThemeMode(value ? ThemeMode.dark : ThemeMode.light);
but it is not updating the ThemeMode value therefore i am always getting false from Get.isDarkMode please correct this issue...
e.g
color: Get.isDarkMode ? ColorHelper.darkTertiary : ColorHelper.lightPrimary,
I have same issue
any updated ?
I think, func changetheme/changethememode, sometime can not change the theme
Use Theme.of(context)
You have to use context with function isDarkMode because it is not working with Get keyword.
Like:
context.isDarkMode
as i have found the solution so i am closing this issue.
import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart';
class ThemeService { final _box = GetStorage(); final _key = 'isDarkMode';
/// Get isDarkMode info from local storage and return ThemeMode ThemeMode get theme => loadThemeFromBox() ? ThemeMode.dark : ThemeMode.light;
/// Load isDArkMode from local storage and if it's empty, returns false (that means default theme is light) bool loadThemeFromBox() => _box.read(_key) ?? false;
/// Save isDarkMode to local storage _saveThemeToBox(bool isDarkMode) => _box.write(_key, isDarkMode);
/// Switch theme and save to local storage void switchTheme() { Get.changeThemeMode(loadThemeFromBox() ? ThemeMode.light : ThemeMode.dark); _saveThemeToBox(!loadThemeFromBox()); } }
use when ThemeService().loadThemeFromBox() to replace with Get.isDarkMode. its works fine.
you need to configure theme and darkTheme of GetMaterialApp. Open the lib/main.dart file and modify it like the below.
import 'package:get/get.dart';
class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key);
@override Widget build(BuildContext context) { return GetMaterialApp( title: 'Flutter Demo', home: const MyHomePage(), ); } }
i'm trying to use Get.isDarkMode, context.isDarkMode, Get.context!.isDarkMode and SchedulerBinding.instance.platformDispatcher.platformBrightness but nothing works, some solution?
i'm trying to use Get.isDarkMode, context.isDarkMode, Get.context!.isDarkMode and SchedulerBinding.instance.platformDispatcher.platformBrightness but nothing works, some solution?
Are u using? Get.changeThemeMode(value ? ThemeMode.dark : ThemeMode.light);
i'm trying to use Get.isDarkMode, context.isDarkMode, Get.context!.isDarkMode and SchedulerBinding.instance.platformDispatcher.platformBrightness but nothing works, some solution?
Are u using? Get.changeThemeMode(value ? ThemeMode.dark : ThemeMode.light);
Yes Sr, my solution was to use a controller with the current theme value and also using local storage to store the current theme in memory.
Use Theme.of(context)
Worked for me
One thing to remember:
Create Theme classes like this :
static final dark = ThemeData.dark().copyWith(
static final light = ThemeData.light().copyWith(
after doing like this u will get the right value from context.isDarkMode