flutterdarticonbutton

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


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

Solution

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