栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Flutter混合开发状态栏问题(Android)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flutter混合开发状态栏问题(Android)

在阅读此解决方案时,需要确认所出现的问题是否与我一致

界面反馈:由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);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271455.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号