我想将我的列表视图放在下拉列表底部
I want to put my listview inside a dropdownbottom
此数据来自 API,我想将所有选项放在下拉列表中
有人可以帮助我吗?我是 flutter 的新手,还在学习使用
这是我的代码:
class _ApiFipePageState extends State<ApiFipePage>{
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<dynamic>(
future: pegarUsuarios(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
var usuario = snapshot.data![index];
return ListTile(
title: Text(usuario['nome']),
);
});
} else if (snapshot.hasError) {
return Center(
child: Text('${snapshot.error}'),
);
}
return Center(
child: CircularProgressIndicator(),
);
},
),
);
}
pegarUsuarios() async {
var url = Uri.parse('https://parallelum.com.br/fipe/api/v1/carros/marcas');
var resposta = await http.get(url);
if (resposta.statusCode == 200) {
return jsonDecode(resposta.body);
} else {
throw Exception('não foi possivel carregar os usuarios');
}
}
试试下面的回答希望对你有帮助。您也可以参考我的回答 and here,因为数据来自 API 并将其显示到下拉列表中。
声明变量:
String? sid;
List data = [];
var urls = "https://parallelum.com.br/fipe/api/v1/carros/marcas";
你的API调用函数:
Future fetchData() async {
var result = await http.get(Uri.parse(urls), headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
});
var jsonData = json.decode(result.body);
setState(() {
data = jsonData;
});
return jsonData;
}
在 initState()
中调用您的 API fetchData()
@override
void initState() {
fetchData();
super.initState();
}
您的下拉小部件:
Container(
width: 200,
child: InputDecorator(
decoration: InputDecoration(
border: OutlineInputBorder(),
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
isDense: true,
isExpanded: true,
value: sid,
hint: Text("Select Data",
style: TextStyle(color: Colors.black)),
items: data.map((list) {
return DropdownMenuItem(
child: Text(list['nome']),
value: list['codigo'].toString(),
);
}).toList(),
onChanged: (value) {
setState(() {
sid = value as String?;
});
},
),
),
),
),
DropdownButton 的结果屏幕 ->
您的下拉结果屏幕 ->
此数据来自 API,我想将所有选项放在下拉列表中
有人可以帮助我吗?我是 flutter 的新手,还在学习使用
这是我的代码:
class _ApiFipePageState extends State<ApiFipePage>{
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<dynamic>(
future: pegarUsuarios(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
var usuario = snapshot.data![index];
return ListTile(
title: Text(usuario['nome']),
);
});
} else if (snapshot.hasError) {
return Center(
child: Text('${snapshot.error}'),
);
}
return Center(
child: CircularProgressIndicator(),
);
},
),
);
}
pegarUsuarios() async {
var url = Uri.parse('https://parallelum.com.br/fipe/api/v1/carros/marcas');
var resposta = await http.get(url);
if (resposta.statusCode == 200) {
return jsonDecode(resposta.body);
} else {
throw Exception('não foi possivel carregar os usuarios');
}
}
试试下面的回答希望对你有帮助。您也可以参考我的回答
声明变量:
String? sid;
List data = [];
var urls = "https://parallelum.com.br/fipe/api/v1/carros/marcas";
你的API调用函数:
Future fetchData() async {
var result = await http.get(Uri.parse(urls), headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
});
var jsonData = json.decode(result.body);
setState(() {
data = jsonData;
});
return jsonData;
}
在 initState()
中调用您的 API fetchData()@override
void initState() {
fetchData();
super.initState();
}
您的下拉小部件:
Container(
width: 200,
child: InputDecorator(
decoration: InputDecoration(
border: OutlineInputBorder(),
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
isDense: true,
isExpanded: true,
value: sid,
hint: Text("Select Data",
style: TextStyle(color: Colors.black)),
items: data.map((list) {
return DropdownMenuItem(
child: Text(list['nome']),
value: list['codigo'].toString(),
);
}).toList(),
onChanged: (value) {
setState(() {
sid = value as String?;
});
},
),
),
),
),
DropdownButton 的结果屏幕 ->
您的下拉结果屏幕 ->