修改状态栏颜色

  1. import 'package:flutter/services.dart';
  2. ...
  3. // 修改为透明背景
  4. void main() {
  5. runApp(new StartApp());
  6. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(statusBarColor:Colors.transparent)); // 状态栏背景透明
  7. // or
  8. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light); // 状态栏图标为白色
  9. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark); // 状态栏图标为黑色
  10. }

沉浸式状态栏

  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. void main() {
  5. runApp(new StartApp());
  6. if (Platform.isAndroid) {
  7. // 以下 设置android状态栏为透明的沉浸。写在组件渲染之后,是为了在渲染后进行set赋值,覆盖状态栏,写在渲染之前MaterialApp组件会覆盖掉这个值。
  8. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  9. }
  10. }
  11. class StartApp extends StatefulWidget {
  12. @override
  13. State<StatefulWidget> createState() {
  14. return new _StartAppState();
  15. }
  16. }
  17. class _StartAppState extends State<StartApp>{
  18. @override
  19. Widget build(BuildContext context) {
  20. return Scaffold(
  21. appBar: PreferredSize(
  22. child: Container(
  23. width: double.infinity,
  24. height: double.infinity,
  25. decoration: BoxDecoration(
  26. gradient: LinearGradient(colors: [Colors.yellow, Colors.pink])), // 也可换成 color 属性
  27. child: SafeArea(child: Center(
  28. child: Text("首页", style: TextStyle(
  29. color: Colors.white,
  30. fontSize: 20
  31. ))),
  32. ),
  33. ),
  34. preferredSize: Size(double.infinity, 60)),
  35. );
  36. }
  37. }

效果:
004.png

全息透明状态栏

  1. Widget build(BuildContext context) {
  2. return new MaterialApp(
  3. home: Container(
  4. decoration: BoxDecoration(
  5. image: DecorationImage(
  6. image: NetworkImage('https://img.zcool.cn/community/0372d195ac1cd55a8012062e3b16810.jpg'),
  7. fit: BoxFit.cover,
  8. )),
  9. child: Scaffold(
  10. backgroundColor: Colors.transparent, // 把scaffold的背景色改成透明
  11. appBar: AppBar(
  12. backgroundColor: Colors.transparent, // 把appbar的背景色改成透明
  13. elevation: 2, // appbar的阴影
  14. title: Text('首页'),
  15. ),
  16. body: Center(
  17. child: Text('Hello World'),
  18. ))),
  19. );
  20. }

效果:
005.png

flutter_statusbarcolor

flutter_statusbarcolor 是用于改变状态栏颜色的库, 非常好用
依赖:

  1. dependencies:
  2. flutter_statusbarcolor: ^0.2.2

示例:

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
  4. void main() {
  5. runApp(new StartApp());
  6. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
  7. }
  8. class StartApp extends StatefulWidget {
  9. @override
  10. State<StatefulWidget> createState() {
  11. return new _StartAppState();
  12. }
  13. }
  14. class _StartAppState extends State<StartApp> {
  15. @override
  16. Widget build(BuildContext context) {
  17. return MaterialApp(
  18. title: '首页',
  19. theme: new ThemeData(
  20. primarySwatch: Colors.blue,
  21. ),
  22. home: new Scaffold(
  23. appBar: new AppBar(
  24. title: new Text('Welcome to Flutter'),
  25. ),
  26. body: new Center(
  27. child: FlatButton(
  28. child: Text('click'),
  29. onPressed: () async {
  30. await FlutterStatusbarcolor.setNavigationBarColor(Colors.purple); // 设置底部导航按钮背景色
  31. await FlutterStatusbarcolor.setStatusBarColor(Colors.green[400]); // 设置状态栏背景色
  32. FlutterStatusbarcolor.setStatusBarWhiteForeground(true); // 状态栏前景设置为白色, 若为false则设置为黑色
  33. },
  34. ),
  35. ),
  36. ),
  37. );
  38. }
  39. }

效果:
006.gif

各种API的最小支持版本

Android

  • getStatusBarColor (5.0)
  • setStatusBarColor (5.0)
  • setStatusBarWhiteForeground (6.0)
  • getNavigationBarColor (5.0)
  • setNavigationBarColor (5.0)
  • setNavigationBarWhiteForeground (8.0)

iOS

  • getStatusBarColor (7+)
  • setStatusBarColor (7+)
  • setStatusBarWhiteForeground (7+)

flutter_statusbar_manager

这个库感觉封装得非常好, 基本的状态栏操作都涉及到了, 但需要 AndroidX 支持, 没折腾, 详情参考: flutter_statusbar_manager, AndroidX 配置参考: AndroidX Migration

参考资料