Listview 如何在颤振列表视图中添加单击事件。。?
我已经通过FutureBuilder实现了一个ListView…我有一个问题,那就是列表点击项的代码应该放在哪里 这是我的Listview 如何在颤振列表视图中添加单击事件。。?,listview,flutter,dart,flutter-layout,Listview,Flutter,Dart,Flutter Layout,我已经通过FutureBuilder实现了一个ListView…我有一个问题,那就是列表点击项的代码应该放在哪里 这是我的FutureBuilder: new FutureBuilder < List < City >> ( future: fetchCountry(new http.Client()), builder: (context, snapshot) { if (snapshot.hasError) print(snapshot.e
FutureBuilder
:
new FutureBuilder < List < City >> (
future: fetchCountry(new http.Client()),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData ?
new CityList(city: snapshot.data) :
new Center(child: new CircularProgressIndicator());
}
),
如果要在点击某个项目时添加单击事件,可以使用以下墨水池包装要按下的内容
InkWell(
onTap: () {
model.selectProduct(product.productID);
Navigator.pushNamed<bool>(
context, '/product/' + model.getSelectedProduct.productID)
.then((_) => model.selectProduct(null));
},
child: //your content here
),
);
InkWell(
onTap:(){
model.selectProduct(product.productID);
Navigator.pushNamed(
上下文“/product/”+model.getSelectedProduct.productID)
.然后(()=>model.selectProduct(null));
},
child://此处显示您的内容
),
);
将单击侦听器添加到CityList中的每个列表项中
对每个列表项使用ListTile。或者,如果您想实现自己的设计,可以使用墨水池或手势检测器
以上所有操作都将允许您访问onTap
回调
范例
@覆盖
小部件构建(构建上下文){
返回新的ListView.builder(
itemCount:city==null?0:city.length,
itemBuilder:(构建上下文,int索引){
返回手势检测器(
ontap:(){},
卡片(
子容器:新容器(
填充:边缘设置。全部(6),
孩子:新中心(
子:SingleChildScrollView(
孩子:新的一排(
儿童:[
新圆星(
背景颜色:颜色。透明,
半径:15,
子:新图像。网络(城市[index]。图标。是否空?”https://www.lightlinksolutions.com/tollfreeadmin/images/ic_cat.png“:城市[索引].图标)
),
填充物(
填充:边缘设置。全部(8.0),
儿童:新文本(
城市[索引]。类别名称,
软包装:是的,
样式:新文本样式(
fontSize:16.0,颜色:Colors.lightBlueAccent),
),
),
],
),
)),
),
),
);
});
}
首先看一看。
现在,在CityList类/小部件中,您可以做几件事。最简单的方法可能是把卡片包在一个盒子里。以下是您发布的编辑过的list.dart文件的示例:
@override
Widget build(BuildContext context) {
return new ListView.builder(
itemCount: city == null ? 0 : city.length,
itemBuilder: (BuildContext context, int index) {
return InkWell(
onTap: () {
// put some code here.
print(city[index]);
},
child: new Card(
child: new Container(
padding: EdgeInsets.all(6),
child: new Center(
child: SingleChildScrollView(
child: new Row(
// Stretch the cards in horizontal axis
// crossAxisAlignment: CrossAxisAlignment.stretch,
children: < Widget > [
new CircleAvatar(
backgroundColor: Colors.transparent,
radius: 15,
child: new Image.network(city[index].icon.isEmpty ? "https://www.lightlinksolutions.com/tollfreeadmin/images/ic_cat.png" : city[index].icon)
// child:new Image.asset('images/launcher_icon.png')
),
Padding(
padding: EdgeInsets.all(8.0),
child: new Text(
// Read the name field value and set it in the Text widget
city[index].cat_name,
softWrap: true,
// set some style to text
style: new TextStyle(
fontSize: 16.0, color: Colors.lightBlueAccent),
),
),
/* new Text(
// Read the name field value and set it in the Text widget
"Category:- " + city[index].cat_name,
// set some style to text
style: new TextStyle(
fontSize: 20.0, color: Colors.amber),
)*/
],
),
)),
),
);
}),
);
}
@覆盖
小部件构建(构建上下文){
返回新的ListView.builder(
itemCount:city==null?0:city.length,
itemBuilder:(构建上下文,int索引){
回墨槽(
onTap:(){
//在这里输入一些代码。
印刷(城市[索引]);
},
孩子:新卡(
子容器:新容器(
填充:边缘设置。全部(6),
孩子:新中心(
子:SingleChildScrollView(
孩子:新的一排(
//在水平轴上拉伸卡片
//crossAxisAlignment:crossAxisAlignment.stretch,
子项:[
新圆星(
背景颜色:颜色。透明,
半径:15,
子:新建图像。网络(城市[index]。图标。是否空?”https://www.lightlinksolutions.com/tollfreeadmin/images/ic_cat.png“:城市[索引].图标)
//子级:new Image.asset('images/launcher\u icon.png')
),
填充物(
填充:边缘设置。全部(8.0),
儿童:新文本(
//读取名称字段值并在文本小部件中进行设置
城市[索引]。类别名称,
软包装:是的,
//将某些样式设置为文本
样式:新文本样式(
fontSize:16.0,颜色:Colors.lightBlueAccent),
),
),
/*新文本(
//读取名称字段值并在文本小部件中进行设置
“类别:-”+城市[索引]。类别名称,
//将某些样式设置为文本
样式:新文本样式(
fontSize:20.0,颜色:颜色。琥珀色),
)*/
],
),
)),
),
);
}),
);
}
请添加您的列表项是否要将其用于列表项或列表视图我要单击列表项并打开新屏幕我应该将其放置在列表项或futurebuilder中的何处。。。?
@override
Widget build(BuildContext context) {
return new ListView.builder(
itemCount: city == null ? 0 : city.length,
itemBuilder: (BuildContext context, int index) {
return InkWell(
onTap: () {
// put some code here.
print(city[index]);
},
child: new Card(
child: new Container(
padding: EdgeInsets.all(6),
child: new Center(
child: SingleChildScrollView(
child: new Row(
// Stretch the cards in horizontal axis
// crossAxisAlignment: CrossAxisAlignment.stretch,
children: < Widget > [
new CircleAvatar(
backgroundColor: Colors.transparent,
radius: 15,
child: new Image.network(city[index].icon.isEmpty ? "https://www.lightlinksolutions.com/tollfreeadmin/images/ic_cat.png" : city[index].icon)
// child:new Image.asset('images/launcher_icon.png')
),
Padding(
padding: EdgeInsets.all(8.0),
child: new Text(
// Read the name field value and set it in the Text widget
city[index].cat_name,
softWrap: true,
// set some style to text
style: new TextStyle(
fontSize: 16.0, color: Colors.lightBlueAccent),
),
),
/* new Text(
// Read the name field value and set it in the Text widget
"Category:- " + city[index].cat_name,
// set some style to text
style: new TextStyle(
fontSize: 20.0, color: Colors.amber),
)*/
],
),
)),
),
);
}),
);
}