Flutter 填坑篇

在安卓里面沉浸式效果可以下个第三方 statusBarUtil.java 来设置下,很简单

在Flutter 里面我发现并不简单(sdk 1.17)

首先网上很多方案基本都是如下:

import 'package:flutter/services.dart';

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark); 

 或者

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
  statusBarIconBrightness: Brightness.dark,
  statusBarColor: Colors.white,
  statusBarBrightness: Brightness.dark,
  systemNavigationBarIconBrightness: Brightness.dark,
));


总之上述代码的意思就是我想让我的statusColor 是白色的,icon 是黑色的

但是,没有效果~


后来我找到一篇文章说要放runApp()的后面,避免被 appBar 颜色覆盖,我做了如下的实验
实验一:
Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
    return Scaffold(
      body: Container(
        child: Column(
          children: <Widget>[
            _headerWidget(context),
            
          ],
        ),
      ),
    );
  }
我在某个界面build 里面加上这句话,同时 Scaffold 里面不用App组件而是在body里面写了一个,然后生效了,icon 的颜色变成了黑色,换成 SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light); 变回白色了

实验二:
Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        title: Text(
          '注册',
          style: TextStyle(
              color: Color.fromRGBO(51, 51, 51, 1),
              fontSize: ScreenUtil().setSp(36)),
        ),
        backgroundColor: Colors.black12,
      ),
      body: Center(
        child: _centerWidget(),
      ),
    );
  }
我在另一个界面加上这句,但是AppBar是有的,同时设置了backgroundColor(不设置也一样),这时会覆盖掉dark,所以可以肯定的是和AppBar 有关系~
这会我的项目布局已经完成了,如果AppBar不能改那我只能和设计商量了,然后我在AppBar里面发现了一个属性,顿时感觉有希望
brightness:这是属性,我显示设置成Brightness.dark,没有发生变化,心拔凉的~ 然后我就想试试 Brightness.light, 结果变黑了,我尼玛,爽

看下源码:
/// If this property is null, then [ThemeData.appBarTheme.brightness] is used,
  /// if that is also null, then [ThemeData.primaryColorBrightness] is used.
  final Brightness brightness;
enum Brightness {
  /// The color is dark and will require a light text color to achieve readable
  /// contrast.
  ///
  /// For example, the color might be dark grey, requiring white text.
  dark,

  /// The color is light and will require a dark text color to achieve readable
  /// contrast.
  ///
  /// For example, the color might be bright white, requiring black text.
  light,
}

 还有,SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark) 这句就不需要了

总结,就是多看源码,可能就有你想要的

谢谢




原文地址:https://www.cnblogs.com/linwen5723/p/12917408.html