我想根据背景图像更改文本(和图标)颜色以提高可见性。
我试过: 使用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中使用带有背景小部件的SliverAppBar。
问题是,展开后,标题和图标(引导和动作)应为白色,以便正确看到;折叠时,应将其更改为黑色。
我如何能得到任何想法布尔出来的吗?或其他解决此问题的方法。
谢谢。
class SliverExample extends StatefulWidget {
final Widget backgroundWidget;
final Widget bodyWidgets;
SliverExample({
this.backgroundWidget,
this.body,
});
@override
_SliverExampleState createState() => _SliverExampleState();
}
class _SliverExampleState extends State<SliverExample> {
// I need something like this
// To determine if SliverAppBar is expanded or not.
bool isAppBarExpanded = false;
@override
Widget build(BuildContext context) {
// To change the item's color accordingly
// To be used in multiple places …Run Code Online (Sandbox Code Playgroud)