Skip to content

Instantly share code, notes, and snippets.

@deshario
Last active March 3, 2020 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deshario/9981b58b870a019d60ec889cea0c73f4 to your computer and use it in GitHub Desktop.
Save deshario/9981b58b870a019d60ec889cea0c73f4 to your computer and use it in GitHub Desktop.
ListView with Nested childrens
import 'package:flutter/material.dart';
import 'package:store/model/Entry.dart';
class PListView extends StatelessWidget {
final List<Entry> dataEntry;
final ValueChanged<String> onSelect;
PListView({
Key key,
@required this.dataEntry,
@required this.onSelect,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scrollbar(
child: ListView.builder(
itemCount: dataEntry.length,
itemBuilder: (BuildContext context, int index) => EntryItem(dataEntry[index],onSelect),
),
);
}
}
class EntryItem extends StatefulWidget {
final Entry entry;
final ValueChanged<String> onItemClicked;
const EntryItem(this.entry, this.onItemClicked);
@override
EntryItemState createState() => EntryItemState();
}
class EntryItemState extends State<EntryItem> {
Widget buildTiles(Entry root, double depth) {
if (root.children.isEmpty) {
double mPadding = depth == 0 ? 0 : depth;
return ListTile(
title: Padding(padding: EdgeInsets.only(left: mPadding.toDouble()),child: Text('${root.title}')),
onTap: () => widget.onItemClicked(root.getKey()),
trailing: root.amount == 0 ? Text('') : Text('${root.getAmount()}'),
);
}else{
ExpansionTile elementList = ExpansionTile(
key: PageStorageKey<Entry>(root),
title: Padding(padding: EdgeInsets.only(left: depth.toDouble()),child: Text('${root.title}')),
children: root.children.map((entry) => buildTiles(entry, depth + 18.toDouble())).toList(),
);
return elementList;
}
}
@override
Widget build(BuildContext context) {
return buildTiles(widget.entry, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment