简体   繁体   English

Flutter 状态栏动态变色

[英]Flutter status bar change color dynamically

Most of the answers about this topics suggests the follow solution.关于此主题的大多数答案都建议采用以下解决方案。

import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    FlutterStatusbarcolor.setStatusBarColor(Colors.white);

But according to this answer但根据这个答案

The build method is designed in such a way that it should be pure/without side effects.构建方法的设计方式应该是纯粹的/没有副作用。

The proposed solution has one drawback - it's does not work in case when I have to change status color depends on current screen.建议的解决方案有一个缺点 - 如果我必须根据当前屏幕更改状态颜色,则它不起作用。

Is there way how I can handle it?有什么办法可以处理吗?

You can use AnnotatedRegion to specify a different statusBarColor for each route:您可以使用AnnotatedRegion为每个路由指定不同的statusBarColor

class MyApp extends StatelessWidget {
  Map<String, WidgetBuilder> get routes {
    return {
      '/': (context) {
        return AnnotatedRegion<SystemUiOverlayStyle>(
          value: const SystemUiOverlayStyle(statusBarColor: Colors.blue),
          child: Scaffold(body: Home()),
        );
      },
      '/foo': (context) {
        return AnnotatedRegion<SystemUiOverlayStyle>(
          value: const SystemUiOverlayStyle(statusBarColor: Colors.red),
          child: Scaffold(body: Foo()),
        );
      },
    };
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: routes,
    );
  }
}

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

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