I'm using this example code and it works well. But I don't know any way yet to know the level of the recursive expansion tile "tree".
The function _buildTiles() is recursive and I would like to add a "level" integer parameter to the function parameter list, but if I do that then the line "root.children.map(_buildTiles).toList()" will cause it to no longer compile.
import 'package:flutter/material.dart';
class ExpansionTileExample extends StatelessWidget {
const ExpansionTileExample({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (BuildContext context, int index) => EntryItem(data[index]),
itemCount: data.length,
);
}
}
// One entry in the multilevel list displayed by this app.
class Entry {
const Entry(this.title, [this.children = const <Entry>[]]);
final String title;
final List<Entry> children;
}
// Data to display.
const List<Entry> data = <Entry>[
Entry(
'Chapter A',
<Entry>[
Entry(
'Section A0',
<Entry>[
Entry('Item A0.1'),
Entry('Item A0.2'),
],
),
Entry('Section A1'),
Entry('Section A2'),
],
),
Entry(
'Chapter B',
<Entry>[
Entry('Section B0'),
Entry('Section B1'),
],
),
];
// 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 ListTile(title: Text(root.title));
return ExpansionTile(
key: PageStorageKey<Entry>(root),
title: Text(root.title),
children: root.children.map(_buildTiles).toList(), // <<---- how to add a parameter here to _buildTiles ??
);
}
@override
Widget build(BuildContext context) {
return _buildTiles(entry);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…