首页 > 解决方案 > 在 Flutter 中仅更改状态栏的颜色

问题描述

我有这样的布局,我想只更改状态栏的颜色,因为我没有使用过 Appbar。

布局

这是我用于此的代码:

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(children: [
          Container(
              color: Colors.green,
              padding: EdgeInsets.all(15),
              child: TextFormField(
                cursorColor: Colors.green,
                decoration: InputDecoration(
                  contentPadding:
                      EdgeInsets.symmetric(vertical: 0.0, horizontal: 10.0),
                  hintText: 'Search a product',
                  fillColor: Colors.white,
                  filled: true,
                  prefixIcon: Visibility(
                    visible: true,
                    child: Icon(
                      Icons.search,
                      color: Colors.grey.shade900,
                    ),
                  ),
                  enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(6),
                      borderSide: BorderSide(
                          color: Colors.grey.shade200, width: 1.5
                    )
                  ),
                )
              ),
            ),

            //AND OTHER ELEMENTS

        ],),
      ),
    );
  }
}

我想将状态栏的颜色更改为深绿色

标签: flutterflutter-layoutflutter-appbar

解决方案


您可以在您的应用程序栏中直接在 MaterialApp 栏主题中提供它

在 Material 应用全局主题中

MaterialApp(
        
          theme: ThemeData(
            appBarTheme: AppBarTheme(
              brightness: Brightness.dark,

              backwardsCompatibility: false,
              systemOverlayStyle:
                  SystemUiOverlayStyle(statusBarColor: Colors.orange),
)

statusBarColor是您要更改的 topBar 的颜色。设置很重要,backwardsCompatibility: false因为它不起作用。


推荐阅读