'Flutter how to change color depending status from JSON API

I want to change dynamically box color depending status in a JSON API. For example, Approved = Colors.green, Rejected = Colors.red, Submitted = Colors.blue.

this is the image of my current app i underline the status

how can achieve it?

this is how i get the API:

Future getCalendar() async{
  List<Events> list;

    String api = "http://200.0.0.104/api/dynamic/uspGetCalendarEvents_mobile?EmployeeId=5";
    var res = await http.get(Uri.encodeFull(api), headers: {"Accept": "application/json"});

        var data = json.decode(res.body);
        var rest = data["data"] as List;
        list = rest.map<Events>((json) => Events.fromJson(json)).toList();

    return list;
  }

this is how i loop the data :

getCalendar().then((data){

          for (var a =0; a <  data.length; a++ )
          {
             _events.addAll({DateTime.parse(data[a].start.toString().replaceAll("-", "")) :  data[a].title.toString().split(",")  });
          }
    });

this is my widget :

 Widget _buildEventList() {
    return ListView(
      children: _selectedEvents
          .map((event) => Container(
                decoration: BoxDecoration(
                  color: Colors.blue, // i want to change the color
                  border: Border.all(width: 0.8),
                  borderRadius: BorderRadius.circular(12.0),
                ),
                margin: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
                child: ListTile(
                  title: Text(event.toString()),
                  onTap: () => print('$event tapped!'),
                ),
              ))
          .toList(),
    );
  }


Solution 1:[1]

You can create a method to return the Color according the condition (assuming event.toString() = Rejected , Approved or Submitted):


Color _getColorByEvent(String event) {
  if (event == "Approved") return Colors.green;
  if (event == "Rejected") return Colors.red;
  return Colors.blue;
}

...

BoxDecoration(
                color: _getColorByEvent(event.toString()), // i want to change the color
                border: Border.all(width: 0.8),
                borderRadius: BorderRadius.circular(12.0),
              ),

Solution 2:[2]

Create a map key value pair, like this.

  Map<String, Color> colorCode ={"Approved":Colors.green,"Rejected":Colors.red};
eg:- 
    final color=colorCode["Approved"];

and then pass key to map you will get value to corresponding key.

Solution 3:[3]

color call method for example

 return ListView(
children: _selectedEvents
    .map((event) => Container(
          decoration: BoxDecoration(
            color: Colors.blue, // i want to change the color
            border: Border.all(width: 0.8),
            borderRadius: BorderRadius.circular(12.0),
          ),
          margin: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
          child: ListTile(
            title: Text(event.toString()),
            onTap: () => print('$event tapped!'),
          ),
        ))
    .toList(),
  );

}

Color _getColor(Map event){ swith(event) { return value } }

Solution 4:[4]

This is How I did ...

Color getColorForStatus(OrderStatus event) {
      switch (event) {
        case OrderStatus.PLACED:
          return Colors.black;
          break;
        case OrderStatus.ORDER_CONFIRMED:
          return Colors.lightBlue;
          break;
        case OrderStatus.OUT_FOR_DELIVERY:
          return new Color(0xFFE5AC0E);
          break;
        case OrderStatus.DELIVERED:
          return Colors.green;
          break;
        case OrderStatus.PAYMENT_DECLINED:
          return new Color(0xFFFFA401);
          break;
        case OrderStatus.CANCELED:
          return Colors.red;
          break;
        default:
          return Colors.black;
          break;
      }
    }

Solution 5:[5]

Color can be updated dynamically like:-

color: Color(int.parse("0xFF"+"c8a988"// your color key from json)// match pattern like //eg: Color(0xFF000000) for white

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 diegoveloper
Solution 2 satish
Solution 3 Junsu Cho
Solution 4 Savad
Solution 5 Aris_choice