[英]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,我有这个自定义 GridView 小部件,这是我用来呈现 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.我有这个 VoidCallBack 并且我想获取被点击项目的索引值。 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.例如,如果我点击“下一页” ,我想打印“下一页”字符串到控制台,如果我点击“上一页” ,我想打印“上一页”字符串到控制台,这是地图我正在使用传播网格。
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.非常感谢任何帮助。
You can use Function
like this, first change your VoidCallback
to Function
like this:您可以像这样使用Function
,首先将您的VoidCallback
更改为Function
,如下所示:
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
}
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.