Gridview 颤振网格可以通过点击触发吗?
我已经创建了一个Flitter应用程序,并使用图像选择器将图像作为网格视图发送到中心,并使用网格图块循环图像阵列。点击网格图块时是否可能触发网格图块? 下面是main.dart中的代码,用于获取将图像添加到阵列和栅格视图中的图像Gridview 颤振网格可以通过点击触发吗?,gridview,dart,flutter,Gridview,Dart,Flutter,我已经创建了一个Flitter应用程序,并使用图像选择器将图像作为网格视图发送到中心,并使用网格图块循环图像阵列。点击网格图块时是否可能触发网格图块? 下面是main.dart中的代码,用于获取将图像添加到阵列和栅格视图中的图像 class _MyHomePageState extends State<MyHomePage> { int _counter = 0; File _imageFile; List<File> _imageList=[]; getImage()
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),
),
);
}
}
class\u MyHomePageState扩展状态{
int _计数器=0;
文件imageFile;
列表_imageList=[];
getImage()异步{
var_fileName=等待ImagePicker.pickImage();
设置状态(){
_imageFile=\u文件名;
_添加(_imageFile);
});
}
移除图像(整型索引){
设置状态(){
_imageList.removeAt(索引);
});
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(widget.title),
),
正文:新中心(
子项:_imageList.length==0
?新文本('未选择图像')
:new GridView.count(
交叉轴计数:4,
childAspectRatio:1.0,
填充:常数边集全部(4.0),
主轴间距:4.0,
交叉轴间距:4.0,
子项:_imageList.map((文件){
返回新网格块(
child:newimage.file(file,fit:BoxFit.cover,);
}).toList()),
),
floatingActionButton:新的floatingActionButton(
onPressed:getImage,
工具提示:“拾取图像”,
子:新图标(图标。添加照片),
),
);
}
}
您只需将网格块
包装在InkResponse
或GestureDetector
中,并传递单击时要调用的函数
例如:
// 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),
)
//单击要调用的函数
void\u onTileClicked(整数索引){
debugPrint(“您点击了项$index”);
}
//获取网格瓷砖
列表文件(列表图标列表){
最终列表瓷砖=[];
for(int i=0;i\u onTileClicked(i),
)));
}
返回瓷砖;
}
//网格视图
新建GridView.count(
交叉轴计数:4,
childAspectRatio:1.0,
填充:常数边集全部(4.0),
主轴间距:4.0,
交叉轴间距:4.0,
子项:_getiles(_imageList),
)
希望有帮助 也可以使用网格生成器
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);
},
),
);
}),