我有一个围绕卡的Listview.builder
包裹的窗口小部件。如何使我的卡不仅可以检测onTap
,还可以将变量传递到导航时的新.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),
),
],
),),
);
}),
);
}
类似于Aziza的建议,您可以看一下Inkwell,它基本上是一个姿势,但具有材料设计的溅出。
您还询问了如何将变量传递给另一个类。您可以将它们作为构造函数变量传递到实例化中来做到这一点。在代码示例中查看ONTAP方法。
代码看起来像这样:
@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 InkWell(
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) {
return new OtherClass(id[index], video[index]);
},
),
);
},
child: new Card(
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
),
),
],
),
),
);
}),
);
}
*代码未测试