flutter - Flutter中LinearPercentIndicator的不同progressColor
问题描述
我得到了 3 个不同的值;$finalD, $finalA, $finalS
因此,我得到了 3 个不同的 linearPercentIndicator ;
//linear1
new Text(" $finalD "),
LinearPercentIndicator(
width: MediaQuery.of(context).size.width - 50,
animation: true,
lineHeight: 25.0,
animationDuration: 2500,
percent: percentage,
center: Text(message),
linearStrokeCap: LinearStrokeCap.roundAll,
progressColor: currentProgressColor(),
),
//linear2
new Text(" $finalA "),
LinearPercentIndicator(
width: MediaQuery.of(context).size.width - 50,
animation: true,
lineHeight: 25.0,
animationDuration: 2500,
percent: percentage1,
center: Text(message1),
linearStrokeCap: LinearStrokeCap.roundAll,
progressColor: currentProgressColor(),
),
//linear3
new Text(" $finalS "),
LinearPercentIndicator(
width: MediaQuery.of(context).size.width - 50,
animation: true,
lineHeight: 25.0,
animationDuration: 2500,
percent: percentage,
center: Text(message),
linearStrokeCap: LinearStrokeCap.roundAll,
progressColor: currentProgressColor(),
),
LinearPercentIndicator(
width: MediaQuery.of(context).size.width - 50,
animation: true,
lineHeight: 25.0,
animationDuration: 2500,
percent: percentage3,
center: Text(message3),
linearStrokeCap: LinearStrokeCap.roundAll,
progressColor: currentProgressColor(),
),
为了区分颜色,我对$finalD、$finalA、$finalS使用currentProgressColor()
currentProgressColor(){
//for $finalD
if (finalD < 10) {
return Colors.green;
}else if(finalD < 14) {
return Colors.yellow;
}else if(finalD < 21){
return Colors.orange;
}else if(finalD < 28){
return Colors.deepOrangeAccent;
}else{
return Colors.red;
}
//for &finalA
else if(finalA < 8){
return Colors.green;
}else if(finalA < 10) {
return Colors.yellow;
}else if(finalA < 15){
return Colors.orange;
}else if(finalA < 20){
return Colors.deepOrangeAccent;
}else if(finalA > 19){
return Colors.red;
}
//for &finalS
else if(finalS < 15){
return Colors.green;
}else if(finalS < 19) {
return Colors.yellow;
}else if(finalS < 26){
return Colors.orange;
}else if(finalS < 34){
return Colors.deepOrangeAccent;
}else{
return Colors.red;
}
}
但它只显示第一个($finalD),其他人只是跟随而不改变它应该的颜色。
我应该如何安排它们?或者我还有其他方法可以区分不同颜色的值吗?:)
解决方案
您的方法 currentProgressColor() 的逻辑不会让您超出 else 语句之外的else{ return Colors.red; }
所有内容都是死代码,这就是为什么 finalS 和 finalA 将具有与 finalD 相同的值。最好的解决方案是有不同的方法,因为当你想要返回一个基于什么 finalD/A/S 的值时很难用这种方式来判断(该方法不知道你想使用什么值)
currentProgressColorD(){
//for $finalD
if (finalD < 10) return Colors.green;
else if(finalD < 14) return Colors.yellow;
else if(finalD < 21) return Colors.orange;
else if(finalD < 28) return Colors.deepOrangeAccent;
return Colors.red; //no need of else because it will run this line at the end if the other conditions aren't true
}
currentProgressColorA(){
//for $finalA
if (finalA < 8) return Colors.green;
else if(finalA < 10) return Colors.yellow;
else if(finalA < 15) return Colors.orange;
else if(finalA < 20) return Colors.deepOrangeAccent;
return Colors.red; //no need of else because it will run this line at the end if the other conditions aren't true
}
currentProgressColorS(){
//for $finalS
if (finalS < 15) return Colors.green;
else if(finalS < 19) return Colors.yellow;
else if(finalS < 26) return Colors.orange;
else if(finalS < 34) return Colors.deepOrangeAccent;
return Colors.red; //no need of else because it will run this line at the end if the other conditions aren't true
}
推荐阅读
- r - R中ggplot2中地图上点名称的问题
- excel - 数据透视表和功率图的 VBA 编码问题
- javascript - 反应垂直范围滑块总是重置为默认忽略新状态
- javascript - React Router 将 props 传递给由?渲染的组件
- visual-studio - 如何减慢函数的输出?
- android - Kotlin“也”在Android Studio中出现奇怪的警告
- reactjs - 在 React 中,是否可以通过 ref 从异步获取中访问类方法?
- r - 在横截面时间序列数据中寻找指标的简单平均值
- vlang - 命令行解析 lib VLang
- postgresql - PostgreSQL - 查询 JSONB 数据中键的第 N 个实例的值