flutter 状态栏背景色以及文本颜色设置

5,423 阅读1分钟

在实际的开发中状态栏沉浸已是常态,控制状态栏色值的同时,也要适时控制状态栏文本的色值形成色差,不然容易看不到。

  1. 无appBar情况。

 runApp(MyApp());
 SystemUiOverlayStyle systemUiOverlayStyle =
    SystemUiOverlayStyle(statusBarColor: Colors.transparent,statusBarIconBrightness:Brightness.dark);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);

  //statusBarColor 控制背景色值
  //statusBarIconBrightness:Brightness.dark 控制本文色值为黑色 / Brightness.light 这个就是白色
    
  1. 有appBar
//不要阴影
runApp(MyApp());
 SystemUiOverlayStyle systemUiOverlayStyle =
    SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    
 appBar: AppBar(
        title: Text(widget.title),
        brightness: Brightness.light,
      ),    
    //brightness: Brightness.light 此处注意,Brightness.light>黑  Brightness.dark>白色
    
    
    //这是源码。
     final Brightness brightness = widget.brightness
      ?? appBarTheme.brightness
      ?? themeData.primaryColorBrightness;
    final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark
      ? SystemUiOverlayStyle.light
      : SystemUiOverlayStyle.dark;
    

如果不想要状态栏(比如splash界面)

SystemChrome.setEnabledSystemUIOverlays([]);//全屏,不显示状态栏,也不现实底部虚拟键盘

想要控制它显示 :
 SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]); //top : bottom

热重载对main 方法下的内容好像无效