简体   繁体   English

当应用栏出现在 Flutter 中时,我无法更改 Android 状态栏颜色

[英]I can't change Android status bar color when app bar present in flutter

I'd like to change the background color of the status bar and app bar in Android, but I don't know how to do it.我想在Android中更改状态栏和应用程序栏的背景颜色,但我不知道该怎么做。

Below is my sample code and screen capture.下面是我的示例代码和屏幕截图。

Code:代码:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle(
        statusBarColor: Colors.black,
      ),
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.red,
        ),
        body: Center(),
      ),
    );
  }
}

Screen Capture:屏幕截图: 结果

Update更新

I try to using SystemChrome.setSystemUIOverlayStyle();我尝试使用SystemChrome.setSystemUIOverlayStyle(); First page can change the background color of the status bar and app bar, but second page does not change the background color of status bar.第一页可以更改状态栏和应用栏的背景颜色,但第二页不会更改状态栏的背景颜色。

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      routes: <String, WidgetBuilder>{
        'Page 1': (BuildContext context) => MyHomePage(),
        'Page 2': (BuildContext context) => MyHomePage2(),
      },
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.black,
    ));
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red,
      ),
      body: Center(
        child: MaterialButton(
          onPressed: () {
            Navigator.pushNamed(context, 'Page 2');
          },
          child: Text('Next Page'),
        ),
      ),
    );
  }
}

class MyHomePage2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.white,
    ));
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.yellow,
      ),
      body: Center(),
    );
  }
}

Screen Recording屏幕录制在此处输入图片说明

This Package can help you这个包可以帮助你

Use this Package使用这个包

Then import library in main.dart file然后在 main.dart 文件中导入库

import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';

Now just add this line in under build context FlutterStatusbarcolor.setStatusBarColor(Colors.white);现在只需在构建上下文中添加这一行FlutterStatusbarcolor.setStatusBarColor(Colors.white);

您可以尝试创建一个全局 Color 变量,当您更改屏幕时,您可以在推送新屏幕之前更新该变量。

I found the solution.我找到了解决方案。

I use AnnotatedRegion<SystemUiOverlayStyle>() to change status bar color of Android and use AppBar() to change status bar color of iOS.我使用AnnotatedRegion<SystemUiOverlayStyle>()更改 Android 的状态栏颜色并使用AppBar()更改 iOS 的状态栏颜色。

I put another AppBar in body, so I can set other background color for the AppBar .我在 body 中放了另一个AppBar ,所以我可以为AppBar设置其他背景颜色。

Like this:像这样:

return AnnotatedRegion<SystemUiOverlayStyle>(
  value: SystemUiOverlayStyle(
    statusBarColor: Colors.black,
    statusBarBrightness: Brightness.dark,
    statusBarIconBrightness: Brightness.light,
  ),
  child: Scaffold(
    appBar: Platform.isIOS
        ? AppBar(
            brightness: Brightness.dark,
            elevation: 0.0,
            backgroundColor: Colors.black,
            toolbarHeight: 0.0,
          )
        : null,
    body: SafeArea(
      child: Column(
        children: [
          AppBar(
            title: Text('Page 1'),
            backgroundColor: Colors.green,
          ),
          Center(
           child: MaterialButton(
              onPressed: () {
                Navigator.pushNamed(context, 'Page 2');
              },
              child: Text('Next Page'),
            ),
          ),
        ],
      ),
    ),
  ),
);

Complete code: https://gist.github.com/jack24254029/f3302ecf10045ae6172984495a7a39a4完整代码: https : //gist.github.com/jack24254029/f3302ecf10045ae6172984495a7a39a4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM