Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter ListView Item Click Listener

Tags:

flutter

dart

I have a ListView and I want to navigate to the next page on the item click.

I need an index of the clicked item of my ListView. I know this can be done using Controller. But I couldn't find any example.

like image 630
Developine Avatar asked Jul 19 '18 06:07

Developine


1 Answers

When adding the onTap for your GestureRecognizer, (or button), your closure can capture the index passed through in the itemBuilder.

E.g.

 body: ListView.builder(             itemBuilder: (BuildContext context, int index) {               return GestureDetector(                 child: Text(index.toString()),                 onTap: () => Scaffold                     .of(context)                     .showSnackBar(SnackBar(content: Text(index.toString()))),               );             },             itemCount: 10)); 

This code will display a snack bar containing the index of the ListItem that you have tapped.

Once you have the index of the item, you can navigate to a new page using code provided by other answers to this question.

like image 109
markt Avatar answered Sep 20 '22 19:09

markt