Flutter 如何在颤振中显示列表
我正在构建一个颤振应用程序,但我不知道如何显示我的列表以及其他元素 我将从我的代码开始Flutter 如何在颤振中显示列表,flutter,dart,Flutter,Dart,我正在构建一个颤振应用程序,但我不知道如何显示我的列表以及其他元素 我将从我的代码开始 class showVehicle extends StatelessWidget { final Future<Vehicle> vehicle; showVehicle({Key key, this.vehicle}) : super(key: key); @override Widget build(BuildContext context) { return S
class showVehicle extends StatelessWidget {
final Future<Vehicle> vehicle;
showVehicle({Key key, this.vehicle}) : super(key: key);
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Center(
child: FutureBuilder<Vehicle>(
future: vehicle,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('none');
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
case ConnectionState.active:
return Text('');
case ConnectionState.done:
if (snapshot.hasError) {
return Text(
'${snapshot.error}',
style: TextStyle(color: Colors.red),
);
} else {
return InkWell(
child: Column(
children: <Widget>[
Stack(children: <Widget>[
FadeInImage.memoryNetwork(
placeholder: kTransparentImage,
image:
'${snapshot.data.defImage}',
height: 250,
fit: BoxFit.cover,
),
Container(
padding: EdgeInsets.all(5.0),
margin:
EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0),
//width: 250,
height: 250,
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: <Color>[
Colors.black.withAlpha(0),
Colors.black12,
Colors.black87
],
),
),
child: Text(
'${snapshot.data.brand} ${snapshot.data.model}',
style: TextStyle(
color: Colors.white, fontSize: 20.0),
),
)
])
],
),
);
}
}
}),
)
)
);
}
}
class showVehicle扩展了无状态小部件{
最终未来车辆;
showVehicle({Key,this.vehicle}):超级(Key:Key);
@凌驾
小部件构建(构建上下文){
返回安全区(
孩子:脚手架(
正文:中(
孩子:未来建设者(
未来:汽车,
生成器:(上下文,快照){
交换机(快照.连接状态){
案例连接状态。无:
返回文本(“无”);
案例连接状态。正在等待:
返回中心(子项:CircularProgressIndicator());
案例连接状态.active:
返回文本(“”);
案例连接状态。完成:
if(snapshot.hasError){
返回文本(
“${snapshot.error}”,
样式:TextStyle(颜色:Colors.red),
);
}否则{
回墨槽(
子:列(
儿童:[
堆栈(子对象:[
FadeInImage.memoryNetwork(
占位符:K透明图像,
图片:
“${snapshot.data.defImage}”,
身高:250,
适合:BoxFit.cover,
),
容器(
填充:所有边缘设置(5.0),
保证金:
LTRB(0.0,5.0,0.0,5.0)中的边集,
//宽度:250,
身高:250,
对齐:对齐.bottomCenter,
装饰:盒子装饰(
梯度:线性梯度(
开始:Alignment.topCenter,
结束:对齐。底部中心,
颜色:[
颜色。黑色。带alpha(0),
颜色。黑色12,
颜色。黑色87
],
),
),
子:文本(
“${snapshot.data.brand}${snapshot.data.model}”,
样式:TextStyle(
颜色:颜色。白色,字体大小:20.0),
),
)
])
],
),
);
}
}
}),
)
)
);
}
}
我丢失的是snapshot.data.fields是字段列表。我不知道如何循环这个列表来显示每个字段
每个字段项都有一个标签和一个值
有人能告诉我如何在已有元素的情况下将其添加到这个小部件的底部吗?我的问题更多的是关于颤振,如果你能给我指出正确的方向,那么我想我能完成它
编辑:我正在添加车辆和字段的未来,以便您可以查看结构
Future<Vehicle> fetchVehicle(String id) async {
final response = await http.get(globals.urlPath + 'vehicles/'+id+'/json/');
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON.
return Vehicle.fromJson(convert.jsonDecode(response.body));
} else {
// If that call was not successful, throw an error.
throw Exception('Failed to load post');
}
}
class Vehicle {
final String id;
final String title;
final String url;
final String defImage;
final String brand;
final String model;
final List<Fields> fields;
Vehicle({this.id, this.title, this.url, this.defImage, this.brand, this.model, this.fields});
factory Vehicle.fromJson(Map<String, dynamic> json) {
List<Fields> tempFields = [];
if(json['[fields]'] != null) {
for (int i = 0; i < json['[fields]'].length; i++) {
//print("Image: " + i.toString() + " of " + json['images'].length.toString());
Fields field = Fields.fromJson(json['fields'][i]);
tempFields.add(field);
}
}
//"Vehicle Type"
var brand = "";
var model = "";
if (json['content'] != null && json['content']['[fields]'] != null){ // we have our fields
json['content']['[fields]'].forEach((field) {
Map<String, dynamic> params = field;
if(field['[label]'] == "Brand"){
//assign this to something
brand = field['[fieldFormattedVal]'];
}
if(field['[label]'] == "Model"){
//assign this to something
model = field['[fieldFormattedVal]'];
}
});
}
return Vehicle(
id: json['id'] ?? json['dataObj']['id'],
title: json['[title]'] ?? json['content']['[fields]'][0]['[fieldFormattedVal]'] ?? '',
url: json['[url]'] ?? '',
defImage: json['[fileName]'] ?? json['content']['[images]'][0]['[fileName]'] ?? '',
brand: brand ?? '',
model: model ?? '',
fields: tempFields, //field.fromJson(json['[fields]']) ?? '',
);
}
}
class Fields {
final String conditions;
final String label;
final String name;
final String rawval;
final String val;
Fields({this.conditions, this.label, this.name, this.rawval, this.val});
factory Fields.fromJson(Map<String, dynamic> json) {
return Fields(
conditions: json['conditions'] ?? '',
label: json['label'] ?? '',
name: json['name'] ?? '',
rawval: json['fieldRawVal'] ?? '',
val: json['fieldFormattedVal'] ?? '',
);
}
}
Future-fetchVehicle(字符串id)异步{
最终响应=等待http.get(globals.urlPath+'vehicles/'+id+'/json/');
如果(response.statusCode==200){
//如果对服务器的调用成功,则解析JSON。
return Vehicle.fromJson(convert.jsonDecode(response.body));
}否则{
//如果该调用未成功,则抛出一个错误。
抛出异常(“加载post失败”);
}
}
等级车辆{
最终字符串id;
最后的字符串标题;
最终字符串url;
最后的字符串重影;
最终串品牌;
最终串模型;
最终列表字段;
车辆({this.id,this.title,this.url,this.defImage,this.brand,this.model,this.fields});
factory Vehicle.fromJson(映射json){
列表字段=[];
如果(json['[字段]']!=null){
for(int i=0;icase ConnectionState.active:
return Text(snapshot.data[title]); // you can reach any field
List<Vehicle> YourLocalList=[];
@override
Widget build(BuildContext context) {
final Future<List<Vehicle>> future = //get in from your function
return FutureBuilder(future: future, builder: (BuildContext context, snapshot) {
print('${snapshot.data}'); //your list
if(snapshot.hasData&&snapshot.data.length>=1) {
YourLocalList = snapshot.data;
return ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: (YourLocalList.length),
itemBuilder: (BuildContext context, int index) {
return Container(
margin: EdgeInsets.only(top: 10, bottom: 10),
child: _taskWidget(index));
});
}
}