flutter ios 13 dark mode

前言

ios 13 开启 dark model,flutter页面status bar文字一直是白色

flutter issues

1
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);

设置dark style 并没有用

SystemChrome

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
static void setSystemUIOverlayStyle(SystemUiOverlayStyle style) {
assert(style != null);
if (_pendingStyle != null) {
// The microtask has already been queued; just update the pending value.
_pendingStyle = style;
return;
}
if (style == _latestStyle) {
// Trivial success: no microtask has been queued and the given style is
// already in effect, so no need to queue a microtask.
return;
}
_pendingStyle = style;
scheduleMicrotask(() {
assert(_pendingStyle != null);
if (_pendingStyle != _latestStyle) {
SystemChannels.platform.invokeMethod<void>(
'SystemChrome.setSystemUIOverlayStyle',
_pendingStyle._toMap(),
);
_latestStyle = _pendingStyle;
}
_pendingStyle = null;
});
}

FlutterPlatformPlugin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- (void)setSystemChromeSystemUIOverlayStyle:(NSDictionary*)message {
NSString* style = message[@"statusBarBrightness"];
if (style == (id)[NSNull null])
return;

UIStatusBarStyle statusBarStyle;
if ([style isEqualToString:@"Brightness.dark"])
statusBarStyle = UIStatusBarStyleLightContent;
else if ([style isEqualToString:@"Brightness.light"])
statusBarStyle = UIStatusBarStyleDefault;
else
return;

NSNumber* infoValue = [[NSBundle mainBundle]
objectForInfoDictionaryKey:@"UIViewControllerBasedStatusBarAppearance"];
Boolean delegateToViewController = (infoValue == nil || [infoValue boolValue]);

if (delegateToViewController) {
// This notification is respected by the iOS embedder
[[NSNotificationCenter defaultCenter]
postNotificationName:@(kOverlayStyleUpdateNotificationName)
object:nil
userInfo:@{@(kOverlayStyleUpdateNotificationKey) : @(statusBarStyle)}];
} else {
// Note: -[UIApplication setStatusBarStyle] is deprecated in iOS9
// in favor of delegating to the view controller
[[UIApplication sharedApplication] setStatusBarStyle:statusBarStyle];
}
}

engine 源码中 可以看到 没有 UIStatusBarStyleDarkContent

尝试 去掉 info.plist 中的 UIViewControllerBasedStatusBarAppearance

然后 监听 通知

1
2
3
4
5
6
7
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appStatusBar:) name:@"io.flutter.plugin.platform.SystemChromeOverlayNotificationName" object:nil];

- (void)appStatusBar:(id)notification {
if (@available(iOS 13.0, *)) {
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDarkContent;
}
}