Detect if iOS13 Dark Mode is enabled from Flutter/Dart [duplicate]

点点圈 提交于 2020-05-25 13:33:11

问题


I'm writing a Flutter app for Android and iOS which will change its style/theme based on iOS' Dark Mode status.

Is there currently anything like a MediaQuery.isDarkModeEnabled in Flutter?

There is a GitHub issue open here but the Flutter team must be overwhelmed in issues so I can't see this being implemented too soon.

I can use 'traitCollection.userInterfaceStyle' from iOS-specific code channels, but adding platform-specific code for Flutter/Dart apps is not something I'm experienced in. Currently working on this solution!

For example, someone could have a CupertinoPicker with adaptive colors:

CupertinoPicker(
    backgroundColor: isDarkModeEnabled ? Colors.black : Colors.white,
    children: items.map((thisItem) => Text(thisItem.name)).toList(),
    itemExtent: 32,
    onSelectedItemChanged: (newItem) {
        setState(() => this.item = items[newItem]);
    }
)

回答1:


Here's how you can set different colors for light and dark mode, the app will automatically switch if the phone is set to dark mode or light mode.

MaterialApp(
  theme: ThemeData(
    brightness: Brightness.light,
    primaryColor: Colors.red,
  ),
  darkTheme: ThemeData(
    brightness: Brightness.dark,
    // additional settings go here
  ),
);

::Update::

You can also get the platform brightness (Brightness.light / Brightness.dark) using

WidgetsBinding.instance.window.platformBrightness

but you will have to use the WidgetsBindingObserver mixin and override the method below

@override
void didChangePlatformBrightness() {
    print(WidgetsBinding.instance.window.platformBrightness); // should print Brightness.light / Brightness.dark when you switch
    super.didChangePlatformBrightness(); // make sure you call this
}

see https://api.flutter.dev/flutter/widgets/WidgetsBindingObserver-class.html on how to use the mixin.




回答2:


Yes it is possible. Look here https://github.com/flutter/flutter/issues/33873#issuecomment-536309491 and here is the answer.

if(ios13==true){
    bool qDarkmodeEnable;
    var qdarkMode = MediaQuery.of(context).platformBrightness;
    if (qdarkMode == Brightness.dark){
        qDarkmodeEnable=true;
    } else {
        qDarkmodeEnable=false;
    }
}



回答3:


Actually doesnt posbile with the master channel, look https://github.com/flutter/flutter/issues/39685



来源:https://stackoverflow.com/questions/57475630/detect-if-ios13-dark-mode-is-enabled-from-flutter-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!