Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter – How to make the first chip in the list selected by default?

I’m making a filter screen that uses Filter Chips, creating a list of filter chips. I want the first item to be selected as default. How to achieve this?

Code:

final items2 = ["Both", "Boy", "Girl", "Infant"];
List<String> gender = [];
Wrap(
                        spacing: 8,
                        runSpacing: 8,
                        children: items2
                            .map(
                              (e) => FilterChip(
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(10),
                                  ),
                                  backgroundColor: Colors.white,
                                  selectedColor: const Color(0xff6fedf6),
                                  label: Container(
                                    height: 46,
                                    width: 50,
                                    alignment: Alignment.center,
                                    child: Text(e),
                                  ),
                                  labelStyle: GoogleFonts.poppins(
                                    textStyle: const TextStyle(
                                      color: Colors.black,
                                      fontSize: 14,
                                      fontWeight: FontWeight.w500,
                                      letterSpacing: 0.5,
                                    ),
                                  ),
                                  selected: gender.contains(e),
                                  onSelected: (bool value) {
                                    if (gender.contains(e)) {
                                      gender.remove(e);
                                    } else {
                                      gender.add(e);
                                    }
                                    setState(() {});
                                  }),
                            )
                            .toList(),
                      ),

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You just need to add the item on selected list

final items2 = ["Both", "Boy", "Girl", "Infant"];
late List<String> gender = [items2 .first];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading