-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtree_view.dart
193 lines (170 loc) · 5.16 KB
/
tree_view.dart
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import 'package:flutter/material.dart';
import './tree_node.dart';
import './tree_node_data.dart';
class TreeView extends StatefulWidget {
final List<TreeNodeData> data;
final bool lazy;
final Widget icon;
final double offsetLeft;
final double offsetRight;
final int? maxLines;
final bool showFilter;
final String filterPlaceholder;
final bool showActions;
final bool showCheckBox;
final bool contentTappable;
/// Desired behavior:
/// - if I check/uncheck a parent I want all children to be checked/unchecked
/// - if I check/uncheck all children I want the parent to be checked/unchecked
final bool manageParentState;
final Function(TreeNodeData node)? onTap;
final void Function(TreeNodeData node)? onLoad;
final void Function(TreeNodeData node)? onExpand;
final void Function(TreeNodeData node)? onCollapse;
final void Function(bool checked, TreeNodeData node)? onCheck;
final void Function(TreeNodeData node, TreeNodeData parent)? onAppend;
final void Function(TreeNodeData node, TreeNodeData parent)? onRemove;
final TreeNodeData Function(TreeNodeData parent)? append;
final Future<List<TreeNodeData>> Function(TreeNodeData parent)? load;
const TreeView({
Key? key,
required this.data,
this.onTap,
this.onCheck,
this.onLoad,
this.onExpand,
this.onCollapse,
this.onAppend,
this.onRemove,
this.append,
this.load,
this.lazy = false,
this.offsetLeft = 24.0,
this.offsetRight = 0.0,
this.maxLines,
this.showFilter = false,
this.filterPlaceholder = 'Search',
this.showActions = false,
this.showCheckBox = false,
this.contentTappable = false,
this.icon = const Icon(Icons.expand_more, size: 16.0),
this.manageParentState = false,
}) : super(key: key);
@override
State<TreeView> createState() => _TreeViewState();
}
class _TreeViewState extends State<TreeView> {
late TreeNodeData _root;
List<TreeNodeData> _renderList = [];
List<TreeNodeData> _filter(String val, List<TreeNodeData> list) {
List<TreeNodeData> tempNodes = [];
for (int i = 0; i < list.length; i++) {
TreeNodeData tempNode = TreeNodeData.from(list[i]);
if (tempNode.children.isNotEmpty) {
tempNode.children = _filter(val, tempNode.children);
}
if (tempNode.title.contains(RegExp(val, caseSensitive: false)) ||
tempNode.children.isNotEmpty) {
tempNodes.add(tempNode);
}
}
return tempNodes;
}
void _onChange(String val) {
_renderList = widget.data;
if (val.isNotEmpty) {
_renderList = _filter(val, _renderList);
}
setState(() {});
}
void append(TreeNodeData parent) {
parent.children.add(widget.append!(parent));
setState(() {});
}
void _remove(TreeNodeData node, List<TreeNodeData> list) {
for (int i = 0; i < list.length; i++) {
if (node == list[i]) {
list.removeAt(i);
} else {
_remove(node, list[i].children);
}
}
}
void remove(TreeNodeData node) {
_remove(node, _renderList);
setState(() {});
}
Future<bool> load(TreeNodeData node) async {
try {
final data = await widget.load!(node);
node.children = data;
setState(() {});
return true;
} catch (e) {
print(e);
return false;
}
}
@override
void initState() {
super.initState();
_renderList = widget.data;
_root = TreeNodeData(
title: '',
extra: null,
checked: false,
expanded: false,
children: _renderList,
);
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
if (widget.showFilter)
Padding(
padding: const EdgeInsets.only(
left: 18.0,
right: 18.0,
bottom: 12.0,
),
child: TextField(
onChanged: _onChange,
decoration: InputDecoration(
labelText: widget.filterPlaceholder,
)),
),
...List.generate(
_renderList.length,
(int index) {
return TreeNode(
load: load,
remove: remove,
append: append,
parent: _root,
parentState: widget.manageParentState ? this : null,
data: _renderList[index],
icon: widget.icon,
lazy: widget.lazy,
offsetLeft: widget.offsetLeft,
offsetRight: widget.offsetRight,
maxLines: widget.maxLines,
showCheckBox: widget.showCheckBox,
showActions: widget.showActions,
contentTappable: widget.contentTappable,
onTap: widget.onTap ?? (n) {},
onLoad: widget.onLoad ?? (n) {},
onCheck: widget.onCheck ?? (b, n) {},
onExpand: widget.onExpand ?? (n) {},
onRemove: widget.onRemove ?? (n, p) {},
onAppend: widget.onAppend ?? (n, p) {},
onCollapse: widget.onCollapse ?? (n) {},
);
},
)
],
),
);
}
}