Flutter GridTile can be triggered with tap?
You can also work with grid builder
child: new GridView.builder(
itemCount: 20,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new InkResponse(
child: Image.asset('assets/whats-best-for-your-app-objective-cswift.jpg'),
onTap: (){
print(index);
},
),
);
}),
You can just wrap the GridTile
in an InkResponse
or GestureDetector
and pass a function to be invoked when clicked
Example:
// Function to be called on click
void _onTileClicked(int index){
debugPrint("You tapped on item $index");
}
// Get grid tiles
List<Widget> _getTiles(List<File> iconList) {
final List<Widget> tiles = <Widget>[];
for (int i = 0; i < iconList.length; i++) {
tiles.add(new GridTile(
child: new InkResponse(
enableFeedback: true,
child: new Image.file(iconList[i], fit: BoxFit.cover,),
onTap: () => _onTileClicked(i),
)));
}
return tiles;
}
// GridView
new GridView.count(
crossAxisCount: 4,
childAspectRatio: 1.0,
padding: const EdgeInsets.all(4.0),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
children: _getTiles(_imageList),
)
Hope that helps!