我正在尝试在扩展图块中显示我的json数组数据。我能够在扩展图块中显示数据,但是当我按扩展图块时,它什么也没显示。按下扩展磁贴时无显示。
JSON:
{
“name”: “kris”,
“age”: 13,
“class”: “13A”,
}
Expansion_tile.dart:
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
var userData = [];
class ActiveUser extends StatefulWidget {
@override
ActiveUserState createState() => new ActiveUserState();
}
class ActiveLevelState extends State<ActiveUser> {
final String url = "http://35.186.145.243:8080/user?user_id=user3";
List data;
@override
void initState() {
super.initState();
this.getJsonData();
}
Future<String> getJsonData() async {
var response = await http.get(
Uri.encodeFull(url),
headers: {"Accept": "application/json"},
);
var extractdata = json.decode(response.body);
data = extractdata['user'];
userData = data;
setState(
() {
var extractdata = json.decode(response.body);
data = extractdata['user'];
},
);
return "Success";
}
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (BuildContext context, int index) => Card(
child: SingleChildScrollView(
child: StuffInTiles(userData[index]['name']),
),
),
itemCount: userData.length,
);
}
}
class StuffInTiles extends StatelessWidget {
final String elementList;
StuffInTiles(this.elementList);
@override
Widget build(BuildContext context) {
return _buildTiles();
}
Widget _buildTiles() {
if (userData.length == 0)
return ListTile(
title: Text("Test"),
);
return ExpansionTile(
title: Text(elementList),
children: map(_buildTiles).toList(),
);
}
}
我知道我应该在扩展图块中的子项下写一些内容,以便在按下时显示内容,但是我不确定如何显示子项,因为尝试时会给我错误。
答案 0 :(得分:1)
我已经知道答案了!只需在扩展图块的children属性下调用_getChildren方法即可:
List<dynamic> _getChildren() {
List<Widget> children = [];
levelsData.forEach((element) {
children.add(GestureDetector(
onTap: () => print("Tapped"),
child: ListTile(
dense: true,
title: Text("@" + element['name'].toString()),
subtitle: Text(element['age'].toString()),
),
));
});
return children;
}
return ExpansionTile(
title: Text(elementList),
children: _getChildren(),
);