繁体   English   中英

Flutter 中 LinearPercentIndicator 的不同进度颜色

[英]different progressColor for LinearPercentIndicator in Flutter

我得到了 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(),
          ),

为了区分 colors,我对$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),其他人只是跟随而不改变它应该的颜色。

我应该如何安排它们? 或者我有什么其他方法可以区分不同 colors 的值吗? :)

您的方法 currentProgressColor() 的逻辑不允许您 go 超出 else 语句else{ return Colors.red; } 之后的一切都是死代码,这就是为什么else{ return Colors.red; }和 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM