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

How to select multiple container in flutter?

For now I’m able to select one item at a time but I want to select multiple item. I found some package for multiple selection, but want to achieve without using any packages.

enter image description here

  int? selectedIndex;
  final List<String> _wordName = [
    "Engaged in my Life",
    "Feel Alive",
    "Happy",
    "Love my Life",
  ];



GridView.builder(
                      scrollDirection: Axis.vertical,
                      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,
                        crossAxisSpacing: 3,
                        mainAxisSpacing: 2,
                        childAspectRatio: (16 / 8),
                      ),
                      itemCount: _wordName.length,
                      itemBuilder: (context, index) {
                        return GestureDetector(
                          onTap: () {
                            setState(() {
                              print("now selected ===>>> $index");
                              selectedIndex = index;
                              showButton = true;
                            });
                          },
                          child: Container(
                            margin: EdgeInsets.all(10),
                            decoration: BoxDecoration(
                              color: selectedIndex == index
                                  ? Color(0xffDEB988).withOpacity(0.2)
                                  : Color(0xffF4F4F6).withOpacity(0.5),
                              borderRadius: BorderRadius.circular(5.0),
                              border: Border.all(
                                  color: selectedIndex == index
                                      ? Color(0xffDEB988)
                                      : Colors.transparent,
                                  width: 0.5),
                              image: const DecorationImage(
                                image: AssetImage('assets/images/bg2.png'),
                                fit: BoxFit.cover,
                              ),
                            ),
                            child: Row(
                              children: [
                                Flexible(
                                  child: Center(
                                    child: Text(
                                      _wordName[index].toUpperCase(),
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: selectedIndex == index
                                            ? Color(0xffDEB988)
                                            : Colors.black,
                                        fontWeight: selectedIndex == index
                                            ? FontWeight.bold
                                            : FontWeight.normal,
                                        fontFamily: "Poppins",
                                      ),
                                  }

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 can have selected items in an array

Example:

List<int> selectedItems = [];

GestureDetector(
   onTap: () {
     setState(() {
        if (selectedItems.contains(index)){
           selectedItems.remove(index);
        } else {
           selectedItems.add(index);
        }
     }
   },
   child: Container(
       color: selectedItems.contains(index) ? Colors.red : Colors.blue,
       child: Something(),
   ),
),


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