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 get index value using VoidCallBack onTap function

I have having this problem, and wondering if someone can help me out.

class GridViewSelection extends StatelessWidget {
  GridViewSelection(
      {super.key,
      required this.menuList,
      required this.onTap,
      this.imageList,
      this.nameList,});

  VoidCallback onTap;
  int menuList;
  List? imageList;
  List? nameList;
  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.all(15),
        child: GridView.builder(
            physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3, mainAxisSpacing: 10, crossAxisSpacing: 10),
            itemCount: menuList,
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                onTap: onTap,
                child: Card(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)),
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          SvgPicture.asset(
                            imageList![index],
                            fit: BoxFit.contain,
                            width: MediaQuery.of(context).size.width * 0.15,
                          ),
                          const SizedBox(
                            height: 10,
                          ),
                          Text(
                            textAlign: TextAlign.center,
                            nameList![index],
                            style: TextStyle(
                                fontSize: 14,
                                color: AppTheme.colors.greyFontColor),
                          ),
                        ]),
                  ),
                ),
              );
            }));
  }
}

I have this custom GridView widget, here is the snippet I am using to render the gridView,

GridViewSelection(
   menuList: oldMenu.length, 
   imageList: oldMenu.map((e) => e.image).toList(), 
   nameList: oldMenu.map((e) => e.name).toList(),
      onTap: (){}), 

I have this VoidCallBack and I want to get the index value of the clicked item. for example if i click on ‘Next Page’, I want to print the ‘Next Page’ String to console and if I click on ‘Previous Page’, I want to print the ‘Previous Page’ String to console,
Here is the map I am using to propagate the Grid.

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

class OldMenu{
  String name;
  String image; 

  OldMenu({this.name = '', this.image = ''});
}
var oldMenu = [
  OldMenu(name: 'Previous Page' , image: 'assets/previous_page.svg'),
  OldMenu(name: 'Next Page' , image: 'assets/next_page.svg'),
]

any help is highly appreciated.

>Solution :

You can use Function like this, first change your VoidCallback to Function like this:

class GridViewSelection extends StatelessWidget {
  GridViewSelection(
      {super.key,
      required this.menuList,
      required this.onTap,
      this.imageList,
      this.nameList,});

  Function(int) onTap;
  int menuList;
  List? imageList;
  List? nameList;

 ...
}

then use it like this:

GestureDetector(
     onTap: (){
        onTap(index);
     },
     child: Card(
      ...
)

then in your main class do this:

GridViewSelection(
   menuList: oldMenu.length, 
   imageList: oldMenu.map((e) => e.image).toList(), 
   nameList: oldMenu.map((e) => e.name).toList(),
   onTap: (int index){
     //use index here
   }
), 
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