1

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(),
    );
  }

5 Answers 5

5

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),
              ),
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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.

Comments

0

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 } }

Comments

0

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;
      }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.