【问题标题】:Make Flutter Status Bar Solid使颤振状态栏稳定
【发布时间】:2020-10-29 14:49:03
【问题描述】:

我的应用没有使用 AppBar,滚动时内容与状态栏重叠。我尝试使用AnnotatedRegion<SystemUiOverlayStyle>,但它并没有改变任何东西。

Overlapped

我最好的尝试是使用 SafeArea,但它会将状态栏变成灰色。

Grey Safe Area

有没有办法保持状态栏的自动颜色,而不与内容重叠?

补充说明: 颤振 1.17.5

我的树

SafeArea
|_ SingleChildScrollView
   |_ Column
      |_ Container
      |_ Container
      |_ Container

编辑:我刚刚意识到自动颜色(状态栏跟随第一个容器的颜色)是因为它是透明的。我正在考虑让它在运行时稳定,并从第一个彩色小部件中读取它。

【问题讨论】:

  • 是否可以全屏运行您的应用程序?

标签: flutter android-statusbar


【解决方案1】:
import 'package:flutter/services.dart';

@override
Widget build(BuildContext context){
  return AnnotatedRegion<SystemUiOverlayStyle>(
    value: SystemUiOverlayStyle(
      statusBarColor: Colors.blue, //or the color you prefer
    ),
    child: SafeArea(child: Scaffold(...)),
  );
}

【讨论】:

  • 有没有办法让颜色“自动”?比如安全区域下的容器是红色的,没有明确指定就变成红色?
  • 我注意到这是因为条形图是透明的,有没有办法让它在运行时稳定或在其他小部件颜色上读取?如果没有,也许这是最好的方法
  • 您可以使用 Theme.of(context) 并选择脚手架或 AppBar 的背景颜色,例如
【解决方案2】:

您需要用Container 小部件包装您的SafeArea 小部件以更改SafeArea 小部件的颜色

示例代码

Scaffold(
  backgroundColor: Colors.white,
  body: Container(
    color: Colors.red, /* Set your status bar color here */
    child: SafeArea(
       child: Container(
            /* Add your Widget here */
    )),
  ),
); 

另一种方式你可以使用AppBar

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        backgroundColor: Colors.red,
      ),
      body: Container(
        child: //widget
      )
    );
  }

【讨论】:

  • 不幸的是,这使得其他容器也变成了红色,而不仅仅是状态栏
  • @Vayth 尝试在您的Scaffold 中添加backgroundColor: Colors.white,
猜你喜欢
  • 2022-08-07
  • 2021-04-19
  • 1970-01-01
  • 2019-01-11
  • 2020-06-12
  • 2023-01-31
  • 1970-01-01
  • 2022-01-25
  • 2021-11-19
相关资源
最近更新 更多