From e01377e100b559b1438faecfbe436be772dd7f1c Mon Sep 17 00:00:00 2001 From: 21pages Date: Tue, 15 Aug 2023 09:52:02 +0800 Subject: [PATCH] tag color same as tag name if name is color, remove yellow random tag color Signed-off-by: 21pages --- flutter/lib/common.dart | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/flutter/lib/common.dart b/flutter/lib/common.dart index 6bed2c2e0..794815b86 100644 --- a/flutter/lib/common.dart +++ b/flutter/lib/common.dart @@ -1078,25 +1078,32 @@ Color str2color(String str, [alpha = 0xFF]) { } Color str2color2(String str, [alpha = 0xFF]) { - List colorList = [ - Colors.red, - Colors.green, - Colors.blue, - Colors.orange, - Colors.yellow, - Colors.purple, - Colors.grey, - Colors.cyan, - Colors.lime, - Colors.teal, - Colors.pink, - Colors.indigo, - Colors.brown, - ]; + Map colorMap = { + "red": Colors.red, + "green": Colors.green, + "blue": Colors.blue, + "orange": Colors.orange, + "purple": Colors.purple, + "grey": Colors.grey, + "cyan": Colors.cyan, + "lime": Colors.lime, + "teal": Colors.teal, + "pink": Colors.pink[200]!, + "indigo": Colors.indigo, + "brown": Colors.brown, + }; + final color = colorMap[str.toLowerCase()]; + if (color != null) { + return color.withAlpha(alpha); + } + if (str.toLowerCase() == 'yellow') { + return Colors.yellow.withAlpha(alpha); + } var hash = 0; for (var i = 0; i < str.length; i++) { hash += str.codeUnitAt(i); } + List colorList = colorMap.values.toList(); hash = hash % colorList.length; return colorList[hash].withAlpha(alpha); }