cdb_ui/lib/pages/expandable_list.dart
2024-09-23 20:19:30 +02:00

60 lines
1.4 KiB
Dart

import 'package:flutter/material.dart';
class ExpandableListItem {
ExpandableListItem({
required this.body,
required this.header,
this.isExpanded = false,
});
Widget body;
Widget header;
bool isExpanded;
}
class ExpandableList extends StatefulWidget {
final List<ExpandableListItem> entries;
const ExpandableList(this.entries, {super.key});
@override
State<ExpandableList> createState() => _ExpandableListState();
}
class _ExpandableListState extends State<ExpandableList> {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Container(
child: _buildPanel(),
),
);
}
Widget _buildPanel() {
return ExpansionPanelList(
expansionCallback: (int index, bool isExpanded) {
setState(() {
widget.entries[index].isExpanded = isExpanded;
});
},
children: widget.entries.map<ExpansionPanel>((ExpandableListItem item) {
return ExpansionPanel(
headerBuilder: (BuildContext context, bool isExpanded) {
return ListTile(
title: item.header,
onTap: () {
setState(() {
widget.entries.firstWhere((x) => x == item).isExpanded =
!isExpanded;
});
},
);
},
body: item.body,
isExpanded: item.isExpanded,
);
}).toList(),
);
}
}