根据 flutter decoration 中的 firebase 值更改颜色
Change color based on firebase value within flutter decoration
我有一个装饰,我希望根据来自 firebase 的值 return 进行更改。
目前,我的装修如下
decoration: const BoxDecoration(
border: Border(
left: BorderSide(
color: getColor(task.status), width: 5))),
这是我的 switch 语句,我希望它 return 颜色正确
Color getColor(Color status) {
switch (status.toString()) {
case 'Draft':
return const Color.fromRGBO(255, 203, 51, 1);
case 'Posted':
return const Color.fromRGBO(10, 217, 163, 1);
case 'Offered':
return const Color.fromRGBO(20, 152, 204, 1);
case 'Assigned':
return const Color.fromRGBO(14, 116, 178, 1);
case 'Overdue':
return const Color.fromRGBO(222, 80, 66, 1);
default:
return const Color.fromRGBO(144, 163, 167, 1);
}
}
getColor(Color status)
上述方法说明您应该传递 Color 类型,然后尝试将其转换为字符串并将其与“Draft”、“Posted”等匹配。我的朋友,没有该名称的颜色。
首先,如果您遇到“无效常量值”之类的错误。请从您的代码中删除 const。不需要。
您的 getColor() 方法看起来很好,应该会像您期望的那样工作。您只需要传递正确的值(可能不是 Color 类型,但简单的字符串应该可以)。
我在 flutter dev 上试过你的代码,它有效。
我有一个装饰,我希望根据来自 firebase 的值 return 进行更改。
目前,我的装修如下
decoration: const BoxDecoration(
border: Border(
left: BorderSide(
color: getColor(task.status), width: 5))),
这是我的 switch 语句,我希望它 return 颜色正确
Color getColor(Color status) {
switch (status.toString()) {
case 'Draft':
return const Color.fromRGBO(255, 203, 51, 1);
case 'Posted':
return const Color.fromRGBO(10, 217, 163, 1);
case 'Offered':
return const Color.fromRGBO(20, 152, 204, 1);
case 'Assigned':
return const Color.fromRGBO(14, 116, 178, 1);
case 'Overdue':
return const Color.fromRGBO(222, 80, 66, 1);
default:
return const Color.fromRGBO(144, 163, 167, 1);
}
}
getColor(Color status)
上述方法说明您应该传递 Color 类型,然后尝试将其转换为字符串并将其与“Draft”、“Posted”等匹配。我的朋友,没有该名称的颜色。
首先,如果您遇到“无效常量值”之类的错误。请从您的代码中删除 const。不需要。
您的 getColor() 方法看起来很好,应该会像您期望的那样工作。您只需要传递正确的值(可能不是 Color 类型,但简单的字符串应该可以)。
我在 flutter dev 上试过你的代码,它有效。