gpt4 book ai didi

dart - 如何根据背景图像更改文本颜色 - Flutter

转载 作者:IT老高 更新时间:2023-10-28 12:41:28 28 4
gpt4 key购买 nike

我想根据背景图像更改文本(和图标)颜色以提高可见性。

我试过了:使用 palette_generator 包检查背景图像的主色,并使用 flutter_statusbarcolor 包中的 useWhiteForgroundForColor 函数(返回 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');

return useWhiteForeground(dominantColor);
}

我找到了其他语言的 other methods,但我不知道在 dart 中实现相同方法的方法。

补充说明:我的实际代码包括一些额外的复杂性。我正在使用 SliverAppBar,在这里我想确定flexibleSpace 展开时的标题和图标颜色。在基于 this 的社区的帮助下,我在折叠时更改了它们的颜色。

最佳答案

颜色类已经有一个计算亮度的方法,称为computeLuminance()

Color textColor = color.computeLuminance() > 0.5 ? Colors.black : Colors.white;

关于dart - 如何根据背景图像更改文本颜色 - Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54438359/

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