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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With