我能够从REST / JSON填充ListView和ExpansionTiles,这很好,但是我希望最初不填充expandtile,并且仅在单击它们时才单独填充(可能使用onExpansionChanged :)
我有以下内容,除了填充ListView之后,它几乎可以按我的要求工作,然后,由于每个ExpansionTile内都装有FutureBuilder,因此它立即(自动)填充了扩展图块。
如何更改此设置,以便在单独单击/展开ExpansionTiles之前不会填充它们?
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'ExpansionTile Test',
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<http.Response> _responseFuture;
@override
void initState() {
super.initState();
_responseFuture = http.get('https://jsonplaceholder.typicode.com/users');
print("Getting Main List");
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('ExpansionTile Test'),
),
body: new FutureBuilder(
future: _responseFuture,
builder: (BuildContext context, AsyncSnapshot<http.Response> response) {
if (!response.hasData) {
return const Center(
child: const Text('Loading...'),
);
} else if (response.data.statusCode != 200) {
return const Center(
child: const Text('Error loading data'),
);
} else {
List<dynamic> jsonn = json.decode(response.data.body);
return new MyExpansionTileList(jsonn);
}
},
),
);
}
}
class MyExpansionTileList extends StatelessWidget {
final List<dynamic> elementList;
MyExpansionTileList(this.elementList);
List<Widget> _getChildren() {
List<Widget> children = [];
elementList.forEach((element) {
children.add(
new MyExpansionTile(element['id'], element['name']),
);
});
return children;
}
@override
Widget build(BuildContext context) {
return new ListView(
children: _getChildren(),
);
}
}
class MyExpansionTile extends StatefulWidget {
final int id;
final String title;
MyExpansionTile(this.id, this.title);
@override
State createState() => new MyExpansionTileState();
}
class MyExpansionTileState extends State<MyExpansionTile> {
PageStorageKey _key;
Future<http.Response> _responseFuture;
@override
void initState() {
super.initState();
_responseFuture = http.get('https://jsonplaceholder.typicode.com/users');
print("Getting Expansion Item # ${widget.id}");
}
@override
Widget build(BuildContext context) {
_key = new PageStorageKey('${widget.id}');
return new ExpansionTile(
key: _key,
title: new Text(widget.title),
children: <Widget>[
new FutureBuilder(
future: _responseFuture,
builder:
(BuildContext context, AsyncSnapshot<http.Response> response) {
if (!response.hasData) {
return const Center(
child: const Text('Loading...'),
);
} else if (response.data.statusCode != 200) {
return const Center(
child: const Text('Error loading data'),
);
} else {
List<dynamic> json_data = json.decode(response.data.body);
List<Widget> reasonList = [];
json_data.forEach((element) {
reasonList.add(new ListTile(
dense: true,
title: new Text(element['email']),
));
});
return new Column(children: reasonList);
}
},
)
],
);
}
}
答案 0 :(得分:2)
您可以使用Completer代替Future,但是仍然可以使用FutureBuilder,这是示例代码
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'ExpansionTile Test',
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<http.Response> _responseFuture;
@override
void initState() {
super.initState();
_responseFuture = http.get('https://jsonplaceholder.typicode.com/users');
print("Getting Main List");
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('ExpansionTile Test'),
),
body: new FutureBuilder(
future: _responseFuture,
builder: (BuildContext context, AsyncSnapshot<http.Response> response) {
if (!response.hasData) {
return const Center(
child: const Text('Loading...'),
);
} else if (response.data.statusCode != 200) {
return const Center(
child: const Text('Error loading data'),
);
} else {
List<dynamic> jsonn = json.decode(response.data.body);
return new MyExpansionTileList(jsonn);
}
},
),
);
}
}
class MyExpansionTileList extends StatelessWidget {
final List<dynamic> elementList;
MyExpansionTileList(this.elementList);
List<Widget> _getChildren() {
List<Widget> children = [];
elementList.forEach((element) {
children.add(
new MyExpansionTile(element['id'], element['name']),
);
});
return children;
}
@override
Widget build(BuildContext context) {
return new ListView(
children: _getChildren(),
);
}
}
class MyExpansionTile extends StatefulWidget {
final int id;
final String title;
MyExpansionTile(this.id, this.title);
@override
State createState() => new MyExpansionTileState();
}
class MyExpansionTileState extends State<MyExpansionTile> {
PageStorageKey _key;
Completer<http.Response> _responseCompleter = new Completer();
@override
Widget build(BuildContext context) {
_key = new PageStorageKey('${widget.id}');
return new ExpansionTile(
key: _key,
title: new Text(widget.title),
onExpansionChanged: (bool isExpanding) {
if (!_responseCompleter.isCompleted) {
_responseCompleter.complete(http.get('https://jsonplaceholder.typicode.com/users'));
print("Getting Expansion Item # ${widget.id}");
}
},
children: <Widget>[
new FutureBuilder(
future: _responseCompleter.future,
builder:
(BuildContext context, AsyncSnapshot<http.Response> response) {
if (!response.hasData) {
return const Center(
child: const Text('Loading...'),
);
} else if (response.data.statusCode != 200) {
return const Center(
child: const Text('Error loading data'),
);
} else {
List<dynamic> json_data = json.decode(response.data.body);
List<Widget> reasonList = [];
json_data.forEach((element) {
reasonList.add(new ListTile(
dense: true,
title: new Text(element['email']),
));
});
return new Column(children: reasonList);
}
},
)
],
);
}
}