Flutter可扩展的面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import 'package:flutter/material.dart';
class ExpansionTileSample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('ExpansionTile'),
),
body: new ListView.builder(
itemBuilder: (BuildContext context, int index) => new EntryItem(data[index]),
itemCount: data.length,
),
),
);
}
}

// One entry in the multilevel list displayed by this app.
class Entry {
Entry(this.title, [this.children = const <Entry>[]]);
final String title;
final List<Entry> children;
}

// The entire multilevel list displayed by this app.
final List<Entry> data = <Entry>[
new Entry('Chapter A',
<Entry>[
new Entry('Section A0',
<Entry>[
new Entry('Item A0.1'),
new Entry('Item A0.2'),
new Entry('Item A0.3'),
],
),
new Entry('Section A1'),
new Entry('Section A2'),
],
),
];

// Displays one Entry. If the entry has children then it's displayed
// with an ExpansionTile.
class EntryItem extends StatelessWidget {
const EntryItem(this.entry);

final Entry entry;

Widget _buildTiles(Entry root) {
if (root.children.isEmpty)
return new ListTile(title: new Text(root.title));
return new ExpansionTile(
key: new PageStorageKey<Entry>(root),
title: new Text(root.title),
children: root.children.map(_buildTiles).toList(),
);
}

@override
Widget build(BuildContext context) {
return _buildTiles(entry);
}
}

void main() {
runApp(new ExpansionTileSample());
}