Flutter-记一次关于Flutter之Android的状态栏颜色调整

在android api 30之上设置style,flutter里面设置SystemChrome什么的都不管用,必须如下

window.statusBarColor = Color.TRANSPARENT
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.insetsController?.setSystemBarsAppearance(
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
            )
        } else {
            window.decorView?.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
        }

 2025.11.13 沉浸式状态栏,兼容Android和Ios的写法(未所有类型设备都测试),有缺陷:在未设置systemOverlayStyle的page时状态栏的颜色时变化了,但是里面的icon颜色没变化(比如导致暗黑模式标题栏时黑色,状态栏的icon也是黑色,重叠了),测试发现了会提bug

      return Scaffold(
        appBar: PreferredSize(preferredSize: Size.zero, child: AppBar( //PreferredSize的作用是当不需要标题栏的时候的写法
          backgroundColor: Colors.transparent,
          systemOverlayStyle: SystemUiOverlayStyle(
            statusBarColor: Colors.transparent,
            statusBarIconBrightness: isDarkMode ? Brightness.light : Brightness.dark,
            statusBarBrightness: isDarkMode ? Brightness.light : Brightness.dark,
          ),
        )),
        extendBodyBehindAppBar: true, //需要
        body: Container(),
      );

 终极版,SystemChrome还是起作用的,只是要套一层在页面刷新之后,放在根目录的build方法之中

    final theme = Theme.of(context);
    bool isDarkMode = theme.brightness == Brightness.dark;
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {//不能直接调用SystemChrome.setSystemUIOverlayStyle
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: isDarkMode ? Brightness.light : Brightness.dark,
        statusBarBrightness: isDarkMode ? Brightness.light : Brightness.dark,
      ));
    });

 

posted @ 2024-04-04 17:25  呢哇哦比较  阅读(248)  评论(0)    收藏  举报