flutterdartfilterchips

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

Solution

  • You just need to add the item on selected list

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