我有一个扩展的小部件,它围绕Card的Listview.builder
包裹。如何使我的卡不仅可以检测onTap
,还可以在Navigation上将变量传递给新的.dart
文件。我目前遇到尺寸错误?
已使用代码更新
这是我的代码:
new Expanded(
child: new ListView.builder(
itemCount: id == null ? 0 : id.length,
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new Column(
children: <Widget>[
new Image.network(video[index]),
new Padding(padding: new EdgeInsets.all(3.0)),
new Text(title[index],
style: new TextStyle(fontWeight: FontWeight.bold,
color: Colors.black),
),
new GestureDetector(onTap: (){
print(id[index]);
},)
],
),
);
}))
这是引发的异常:
The following assertion was thrown during performLayout():
RenderPointerListener object was given an infinite size during layout.
This probably means that it is a render object that tries to be as big as possible, but it was put
inside another render object that allows its children to pick their own size.
我想传递类似于iOS Swift中的
title[index]
的video[index]
和didSelectRowAtIndexPath
。 最佳答案
您将GestureDetector
添加为Column
的一个子项,而Flutter无法理解该GestureDetector
需要在哪个UI上检测不同的触摸事件(您未指定在什么位置执行此GestureDetector
才能执行其任务)
如果您需要整个Card
进行交互,则需要将Card
包装在GestureDecetor
中,如下所示
var id = ["title 1", "title 2", "title 3", "title 4", "title 5",];
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new ListView.builder(
itemCount: id == null ? 0 : id.length,
itemBuilder: (BuildContext context, int index) {
return new GestureDetector( //You need to make my child interactive
onTap: () => print(id[index]),
child: new Card( //I am the clickable child
child: new Column(
children: <Widget>[
//new Image.network(video[index]),
new Padding(padding: new EdgeInsets.all(3.0)),
new Text(id[index],
style: new TextStyle(fontWeight: FontWeight.bold,
color: Colors.black),
),
],
),),
);
}),
);
}
关于navigation - 在Flutter中将GestureDetector添加到Listview.builder卡的正确方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46416024/