我想根据背景图像更改文本(和图标)颜色以提高可见性。
我试过: 使用palette_generator包,检查背景图像的主色,并 从flutter_statusbarcolor包中使用WhiteForgroundForColor函数(返回一个bool)为我的文本(和图标)颜色选择黑色或白色。
问题:有时主色会变成空。在我的测试中,这种情况发生在黑色和白色,我不知道有什么方法可以找出哪一种。
Future<bool> useWhiteTextColor(String imageUrl) async {
PaletteGenerator paletteGenerator =
await PaletteGenerator.fromImageProvider(
NetworkImage(imageUrl),
// Images are square
size: Size(300, 300),
// I want the dominant color of the top left section of the image
region: Offset.zero & Size(40, 40),
);
Color dominantColor = paletteGenerator.dominantColor?.color;
// Here's the problem
// Sometimes dominantColor returns null
// With black and white background colors in my tests
if (dominantColor == null) print('Dominant Color null'); …Run Code Online (Sandbox Code Playgroud) 我查看了 Flutter 文档,试图找到一个事件、回调,甚至是在 FlexibleSpaceBar 折叠或展开时可以挂钩的状态。
return new FlexibleSpaceBar(
title: new Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
new Text(_name, style: textTheme.headline),
new Text(_caption, style: textTheme.caption)
]),
centerTitle: false,
background: getImage());`
Run Code Online (Sandbox Code Playgroud)
当 FlexibleSpaceBar 被对齐(折叠)时,我想隐藏_caption文本并只显示_name文本。当它完全展开时,我显然想同时显示_name和_caption。
我该怎么做?
我不熟悉颤振,所以我对此有些迷失。