flutter - Flutter中LinearPercentIndicator的不同progressColor

标签 flutter dart indicator

我得到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(),
          ),
为了区分颜色,我使用 currentProgressColor()来表示那些 $ finalD,$ finalA,$ finalS
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
}

关于flutter - Flutter中LinearPercentIndicator的不同progressColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62648250/

相关文章:

flutter - Flutter中的复选框

flutter - 如何修复 GoRouter.of(context).pop() 以便它导航到上一页?

dart - 如何在 BoxDecoration 中剪辑溢出

firebase - 在 Flutter 中关闭 AlertDialog

Android TabPage Indicator 内容包装问题

QTreeView 绘制下降指示器

flutter - 如何为每个切换按钮添加一个int值? [ flutter ]

mobile - 是否可以在 Linux 虚拟机上使用 Flutter 开发 iOS 应用程序?

android:在可扩展 ListView 中设置指示器的大小

dart - IndexedWidgetBuilder,它是怎么知道索引的呢?