Flutter设置(appBar)状态栏背景色,状态栏字体

Container(
      height: ScreenUtil.screenHeight,
      color: Color(0xFF007AFF),
      child: AnnotatedRegion<SystemUiOverlayStyle>(
        // 设置 AppBar 颜色属性
        value:SystemUiOverlayStyle.light,
      ),
    );

如 AnnotatedRegion 下 value 属性值就是对应的 light模式还是dark模式,如果Container 是最外层容器,状态栏背景色就是透明的,看到的效果就是上面的color色值,也可以在initState()时给状态栏设置指定的背景色

SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(statusBarColor: Colors.red));

setSystemUIOverlayStyle 即设置状态栏颜色

设置状态栏字体颜色

Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 360, height: 640);
    FToast().init(context);
    return Scaffold(
      backgroundColor: Colors.redAccent,
      appBar: AppBar(
        title: Text(Title dark),
        centerTitle: true,
        elevation: 0,
        bottom: null,
        backgroundColor: Colors.orangeAccent,
        brightness:
             Brightness.dark,
      ),
      body: Container(),
    );
  }
Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 360, height: 640);
    FToast().init(context);
    return Scaffold(
      backgroundColor: Colors.blue,
      appBar: AppBar(
        title: Text(Title light),
        centerTitle: true,
        elevation: 0,
        bottom: null,
        backgroundColor: Colors.orangeAccent,
        brightness:
             Brightness.light,
      ),
      body: Container(),
    );
  }

如果AppBar属性toolbarHeight设置了为 0 ,则brightness相关属性就会失效.Scaffold 也有适配底部输入框不被键盘遮挡

综上可以使用 AnnotatedRegion SystemChrome 或者 Scaffold 使用对应属性进行状态栏相关属性设置.

经验分享 程序员 微信小程序 职场和发展