I am having multiple icons in a Gesture detector but when i tap one it needs to be reflected in the primary icon button

120 Views Asked by At

This is my gesture detector icons in my code..

 GestureDetector(
                        onTap: () {},
                        child:
                            const Icon(Iconsax.music, color: Colors.deepPurple),
                      ),
                      GestureDetector(
                        onTap: () {},
                        child:
                            const Icon(Iconsax.pet, color: Colors.deepPurple),
                      ),
                      GestureDetector(
                        onTap: () {},
                        child:
                            const Icon(Iconsax.lamp4, color: Colors.deepPurple),
                      ),
                      GestureDetector(
                        onTap: () {},
                        child: const Icon(Iconsax.devices,
                            color: Colors.deepPurple),
                      ),

But i need to change it in icon button, i have declared a bool value, but i dont know how to change it in icon button

IconButton(
                    onPressed: () {
                      setState(() {
                        isExpanded = !isExpanded;
                      });
                    },
                    tooltip: 'Change room icon',
                    icon: Icon(Iconsax.lamp,color: AppColor.primaryColor,size: 25,),
                  ),
1

There are 1 best solutions below

0
dstreissi On BEST ANSWER

you can create a variable where you store the icon which should be shown on primary button.

something like this:

Icon _selectedIcon;

GestureDetector(
    onTap: () {
        setState(() {
             _selectedIcon = Iconsax.music;
        });
    },
    child: const Icon(Iconsax.music, color: Colors.deepPurple),
),

and in the IconButton you use the _selectedIcon variable.

IconButton(
    onPressed: () {
         setState(() {
             isExpanded = !isExpanded;
         });
    },
    tooltip: 'Change room icon',
    icon: Icon(_selectedIcon ,color: AppColor.primaryColor,size: 25,),
),