gpt4 book ai didi

Flutter - 带有 SafeArea 的系统栏颜色

转载 作者:IT王子 更新时间:2023-10-29 07:04:01 32 4
gpt4 key购买 nike

我正在尝试为带有彩色系统栏的 flutter 应用程序添加 SafeArea 小部件,但不知何故它们总是变黑。

  @override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle.light.copyWith(
systemNavigationBarIconBrightness: Brightness.dark,
systemNavigationBarColor: kSurfaceColor,
statusBarIconBrightness: Brightness.dark,
statusBarColor: Colors.red, // Note RED here
),
);

return SafeArea(
child: Scaffold(
backgroundColor: kWhiteColor,
appBar: _buildAppBar(context), // Title and Avatar are built here
body: _buildBody(), // This function just returns blank Container for now
floatingActionButton: _buildFAB(context),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
),
);
}

这是我看到的 enter image description here

如果我将 SafeArea 包装在 Container 中,并将 color 属性设置为白色,它可以工作,但系统栏图标也会变成白色 enter image description here

最佳答案

基于@david-carrilho 的回答,我创建了这个简单的小部件

import 'package:flutter/material.dart';

class ColoredSafeArea extends StatelessWidget {
final Widget child;
final Color color;

const ColoredSafeArea({Key key, @required this.child, this.color})
: super(key: key);

@override
Widget build(BuildContext context) {
return Container(
color: color ?? Theme.of(context).colorScheme.primaryVariant,
child: SafeArea(
child: child,
),
);
}
}

然后无论我要使用 SafeArea,我都会使用我的小包装器小部件 ColoredSafeArea

class MyExampleView extends StatelessWidget {
const MyExampleView({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return ColoredSafeArea(
child: Center(
child: Text('Nice color bar'),
),
);
}
}

之所以可行,是因为它在您的内容后面创建了一个具有指定颜色的容器,然后 SafeArea 只需根据设备添加必要的填充。

关于Flutter - 带有 SafeArea 的系统栏颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55250493/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com