For Example Assume You Have List of 10 Postive and Negative Values. So We can Go with ListView Builder. Now what the Changes Should be Happen is If the Positive Value Should Take as Green Card in Listview if the Value is Negative the It Should Show Red Card...
CodePudding user response:
Sure you can do that, I am assuming that you are receiving JSON from API. i.e. balance is the value you want to check. if it has a negative value then use logic like below(psudo code)
if( balance > 1000){
//... the used green colour in widget
} else {
// use color red in widget
}
CodePudding user response:
Yes you can do that.
Widget greenCard() {
return Card(
//green card
);
}
Widget redCard() {
return Card(
//red card
)
}
And call these functions like this in widget tree.
Widget build(BuildContext context) {
// other widgets in tree
// in listview builder
(value >= 0) ? greenCard () : redCard(),
//other widget in the tree
}
CodePudding user response:
The simplest way to do this in thru if else condition
Example Code:
if (list[index].value == "positive")...[
Card(
color: Colors.green,
)
] else...[
Card(
color: Colors.red,
)
],
or
Card(
color: list[index].value == "positive"
? Colors.green
: Colors.red
)