[英]Flutter: How to show array data from ListView.builder inside a ListView.builder?
I have a JSON file like that:我有一个像这样的 JSON 文件:
[
{
"id": 1,
"continent": "North America",
"country": [
{
"name": "United States",
"capital": "Washington, D.C.",
"language": [
"English"
]
},
{
"name": "Canada",
"capital": "Ottawa",
"language": [
"English",
"French"
]
}
]
},
{
"id": 2,
"continent": "Europe",
"country": [
{
"name": "Germany",
"capital": "Berlin",
"language": [
"German"
]
}
]
}
]
I used https://app.quicktype.io/ to parse JSON data.我使用https://app.quicktype.io/来解析 JSON 数据。 I have created a
ListView.builder
to display the names of continents.我创建了一个
ListView.builder
来显示大陆的名称。 Now I want to show "country name", "capital", and "language" for each continent.现在我想显示每个大陆的“国家名称”、“首都”和“语言”。
So please help me do that, this is the main file所以请帮我这样做,这是主文件
import 'package:flutter/material.dart';
import 'model/continent_model.dart';
import 'services/continent_services.dart';
class ContinentPage extends StatefulWidget {
ContinentPage() : super();
@override
_ContinentPageState createState() => _ContinentPageState();
}
class _ContinentPageState extends State<ContinentPage> {
List<Continent> _continent;
List<Country> _country;
@override
void initState() {
super.initState();
ContinentServices.getContinent().then((continents) {
setState(() {
_continent = continents;
});
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(
child: ListView.builder(
itemCount: null == _continent ? 0 : _continent.length,
itemBuilder: (context, index) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(_continent[index].continent),
// how to create a ListView show a Column that includes:
// _country[index].name,
// _country[index].capital,
// _country[index].language,
],
);
})));
}
}
You need to place the second listview.builder inside a container with a defined height.您需要将第二个 listview.builder 放置在具有定义高度的容器内。
Container(
height: 120,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: map.length,
itemBuilder: (context, index) => Column(
children: [
Text(_country.elementAt(index).name),
Text(_country.elementAt(index).capital),
Text(_country.elementAt(index).language),
],
),
),
)
You can copy paste run full code below您可以在下面复制粘贴运行完整代码
You can use nested ListView.separated
您可以使用嵌套
ListView.separated
code snippet代码片段
ListView.separated(
separatorBuilder: (BuildContext context, int index) {
return SizedBox(
height: 10,
);
},
shrinkWrap: true,
itemCount: null == _continent ? 0 : _continent.length,
itemBuilder: (context, index) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
flex: 1, child: Text(_continent[index].continent)),
Expanded(
flex: 2,
child: Container(
height: 50,
child: ListView.separated(
separatorBuilder:
It's too long to describe all the detail, you can directly reference full code描述的太长了,可以直接参考完整代码
working demo工作演示
full code完整代码
import 'package:flutter/material.dart';
// To parse this JSON data, do
//
// final continent = continentFromJson(jsonString);
import 'dart:convert';
List<Continent> continentFromJson(String str) =>
List<Continent>.from(json.decode(str).map((x) => Continent.fromJson(x)));
String continentToJson(List<Continent> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Continent {
Continent({
this.id,
this.continent,
this.country,
});
int id;
String continent;
List<Country> country;
factory Continent.fromJson(Map<String, dynamic> json) => Continent(
id: json["id"],
continent: json["continent"],
country:
List<Country>.from(json["country"].map((x) => Country.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id,
"continent": continent,
"country": List<dynamic>.from(country.map((x) => x.toJson())),
};
}
class Country {
Country({
this.name,
this.capital,
this.language,
});
String name;
String capital;
List<String> language;
factory Country.fromJson(Map<String, dynamic> json) => Country(
name: json["name"],
capital: json["capital"],
language: List<String>.from(json["language"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"name": name,
"capital": capital,
"language": List<dynamic>.from(language.map((x) => x)),
};
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class ContinentServices {
Future<List<Continent>> getContinent() {
String jsonString = '''
[
{
"id": 1,
"continent": "North America",
"country": [
{
"name": "United States",
"capital": "Washington, D.C.",
"language": [
"English"
]
},
{
"name": "Canada",
"capital": "Ottawa",
"language": [
"English",
"French"
]
}
]
},
{
"id": 2,
"continent": "Europe",
"country": [
{
"name": "Germany",
"capital": "Berlin",
"language": [
"German"
]
}
]
}
]
''';
List<Continent> continentList = continentFromJson(jsonString);
return Future.value(continentList);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Continent> _continent;
@override
void initState() {
super.initState();
ContinentServices().getContinent().then((continents) {
setState(() {
_continent = continents;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.separated(
separatorBuilder: (BuildContext context, int index) {
return SizedBox(
height: 10,
);
},
shrinkWrap: true,
itemCount: null == _continent ? 0 : _continent.length,
itemBuilder: (context, index) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
flex: 1, child: Text(_continent[index].continent)),
Expanded(
flex: 2,
child: Container(
height: 50,
child: ListView.separated(
separatorBuilder:
(BuildContext context, int index) {
return SizedBox(
width: 10,
);
},
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: null == _continent
? 0
: _continent[index].country.length,
itemBuilder: (context, countryIndex) {
print(_continent[index]
.country[countryIndex]
.name);
return Row(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
Container(
width: 120,
child: Column(
children: <Widget>[
Text(
_continent[index]
.country[countryIndex]
.name,
style: TextStyle(
color: Colors.red),
),
Text(
_continent[index]
.country[countryIndex]
.capital,
style: TextStyle(
color: Colors.red)),
Expanded(
child: ListView.separated(
separatorBuilder:
(BuildContext context,
int index) {
return SizedBox(
width: 2,
);
},
shrinkWrap: true,
scrollDirection:
Axis.horizontal,
itemCount: null ==
_continent
? 0
: _continent[index]
.country[
countryIndex]
.language
.length,
itemBuilder: (context,
languageIndex) {
return Row(
mainAxisAlignment:
MainAxisAlignment
.spaceEvenly,
children: <Widget>[
Text(
_continent[index]
.country[
countryIndex]
.language[
languageIndex],
style: TextStyle(
color: Colors
.red)),
]);
}),
),
],
),
)
]);
}),
),
)
])
],
);
}));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.