【问题标题】:Flutter GridTile can be triggered with tap?Flutter GridTile 可以用点击触发吗?
【发布时间】:2018-08-12 17:45:50
【问题描述】:

我创建了一个颤振应用程序并使用图像选择器将图像作为 gridView 获取到中心,并使用网格图块循环图像数组。当它被点击时是否可以触发 gridTile? 下面是 main.dart 中的代码,用于获取图像并将其添加到数组和网格视图中。

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
File _imageFile;
List<File> _imageList=[];

getImage() async {
var _fileName = await ImagePicker.pickImage();
setState(() {
  _imageFile = _fileName;
  _imageList.add(_imageFile);
});
}

 removeImage(int index){
  setState((){
  _imageList.removeAt(index);
});
}
 @override
 Widget build(BuildContext context) {

 return new Scaffold(
  appBar: new AppBar(

    title: new Text(widget.title),
  ),
  body: new Center(

    child:_imageList.length == 0
        ? new Text('No image selected.')
        :new GridView.count(
        crossAxisCount: 4,
        childAspectRatio: 1.0,
        padding: const EdgeInsets.all(4.0),
        mainAxisSpacing: 4.0,
        crossAxisSpacing: 4.0,
        children: _imageList.map((File file) {
          return new GridTile(

              child: new Image.file(file, fit: BoxFit.cover,));

        }).toList()),
  ),

  floatingActionButton: new FloatingActionButton(
    onPressed:getImage,
    tooltip: 'Pick Image',
    child: new Icon(Icons.add_a_photo),
  ),
);
}
}

【问题讨论】:

    标签: gridview dart flutter


    【解决方案1】:

    您可以将GridTile 包装在InkResponseGestureDetector 中,并传递一个单击时调用的函数

    例子:

    // 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),
    )
    

    希望有帮助!

    【讨论】:

      【解决方案2】:

      您也可以使用网格生成器

      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);
                      },
                    ),
                  );
                }),
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-09
        • 2014-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-22
        相关资源
        最近更新 更多