Cha*_* Jr 5 navigation widget gesturedetector flutter
我有一个Expanded小部件包裹在Listview.builder的Card.如何让我的卡不仅可以检测Listview.builder,还可以将变量传递给我在导航上的新.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]);
},)
],
),
);
}))
Run Code Online (Sandbox Code Playgroud)
这是抛出的异常......
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.
Run Code Online (Sandbox Code Playgroud)
我想传递onTap和'视频[索引]'类似于.dartSWIFT
azi*_*iza 13
您正在添加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),
),
],
),),
);
}),
);
}
Run Code Online (Sandbox Code Playgroud)
与aziza的建议类似,您可以看看InkWell,它基本上是一个GestureDetector,但具有材料设计上的优势。
您还询问了如何将变量传递给另一个类。您可以通过在实例化中将它们作为构造函数变量移交来实现。看一下代码示例中的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
),
),
],
),
),
);
}),
);
}
Run Code Online (Sandbox Code Playgroud)
*代码未经测试