在阅读此解决方案时,需要确认所出现的问题是否与我一致
原因分析界面反馈:由Native跳转到flutter界面时,状态栏的设置无效(包括对icon的颜色和statusBar背景颜色),具体表现为,状态栏有灰色蒙板。
首先,对于设置状态栏的相关属性都是需要通过flutter的SystemChannels.platform.invokeMethod方式调用到native方法来设置状态栏相关属性,
flutter/service/system_chrome.dart
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(
'SystemChrome.setSystemUIOverlayStyle',
_pendingStyle!._toMap(),
);
_latestStyle = _pendingStyle;
}
_pendingStyle = null;
});
}
io.flutter.embedding.engine.systemchannels.PlatformChannel.Java 行109
case "SystemChrome.setSystemUIOverlayStyle":
try {
SystemChromeStyle systemChromeStyle = decodeSystemChromeStyle((JSONObject) arguments);
platformMessageHandler.setSystemUiOverlayStyle(systemChromeStyle);
result.success(null);
} catch (JSONException | NoSuchFieldException exception) {
result.error("error", exception.getMessage(), null);
}
break;
解决方案在混合开发模式下,flutter模块已经执行到了setSystemUIOverlayStyle方法,而MethodChannel还未初始化完成(在这两个方法里打断点就知道是否会执行了),就会导致设置的状态栏属性未生效;
则需要修改其继承自FlutterActivity的类,在其onCreate方法或者onPostResume方法中添加对状态栏属性的修改;
因为我使用了flutter_boost框架,所以需要修改FlutterBoostActivity类
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//方案1
if (VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(0);
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onPostResume() {
super.onPostResume();
//方案2
getWindow().setStatusBarColor(0);
}



