oi-narendra / multiselect-dropdown

Streamlined Flutter widget for versatile multi-selection with extensive customization.
https://pub.dev/packages/multi_dropdown
GNU General Public License v3.0
73 stars 83 forks source link

Change chip color according to its value #164

Open mathlizee opened 2 months ago

mathlizee commented 2 months ago

Hello!

In chipConfig, I can change the background color of all chips easily.

However, I would like to have a different background color for each chip according to its value. My usecase is that each chip represents a document state (In Progress, Closed, Billed, etc.) and that each state is represented by a color.

Would it be possible to allow configuring a chip according to its binded value?

Thanks! Mathieu

Yogesh070 commented 1 month ago

You can do that using selectedItemBuilder property to conditionally style item. However you have to manage remove selected chip yourself using controller. Example : Assuming you are using v2 of the API. @mathlizee

selectedItemBuilder: (context,item) {
                              return Chip(
                                label: Text(
                                  item.label,
                                  style: const TextStyle(color: Colors.white),
                                ),
                                backgroundColor: switch (item.label) {
                                  'Closed' => Colors.red,
                                  'Billed' => Colors.blue,
                                  _ => Colors.green,
                                },
                              );
                            },

Using v3 api

selectedItemBuilder: (item) {
                              return Chip(
                                label: Text(
                                  item.label,
                                  style: const TextStyle(color: Colors.white),
                                ),
                                backgroundColor: switch (item.label) {
                                  'Closed' => Colors.red,
                                  'Billed' => Colors.blue,
                                  _ => Colors.green,
                                },
                              );
                            },