Flutter GridTile can be triggered with tap?

前端 未结 2 1207
死守一世寂寞
死守一世寂寞 2021-02-04 05:45

I have created a flutter app and used the image-picker to get images to the center as a gridView with looping the image array with grid tiles.Is it possible to trigger the gridT

2条回答
  •  心在旅途
    2021-02-04 06:06

    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 _getTiles(List iconList) {
      final List tiles = [];
      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!

提交回复
热议问题