flutter_tableview.dart
11.9 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
library flutter_tableview;
import 'package:flutter/material.dart';
typedef int RowCountAtSection(int section);
typedef Widget ListViewFatherWidgetBuilder(
BuildContext context, Widget canScrollWidget);
typedef Widget SectionHeaderBuilder(BuildContext context, int section);
typedef Widget CellBuilder(BuildContext context, int section, int row);
typedef double CellHeight(BuildContext context, int section, int row);
typedef double SectionHeaderHeight(BuildContext context, int section);
const String ErorrFlagBegin =
'\n\n\n====================FlutterTableView Error========================\n\n\n\n';
const String ErorrFlagEnd =
'\n\n\n\n\n==================================================================\n\n\n\n.';
class FlutterTableView extends StatefulWidget {
FlutterTableView({
required this.sectionCount,
required this.rowCountAtSection,
required this.sectionHeaderBuilder,
required this.cellBuilder,
required this.sectionHeaderHeight,
required this.cellHeight,
this.listViewFatherWidgetBuilder,
this.controller,
this.physics,
this.shrinkWrap = false,
this.padding = const EdgeInsets.all(0.0),
this.cacheExtent = 50.0,
this.backgroundColor = Colors.transparent,
}) : assert(
(sectionCount != null && sectionCount > 0),
'$ErorrFlagBegin sectionCount must > 0 and could not be null. $ErorrFlagEnd',
),
assert(
(rowCountAtSection != null),
'$ErorrFlagBegin function rowCountAtSection could not be null. $ErorrFlagEnd',
),
assert(
(sectionHeaderBuilder != null),
'$ErorrFlagBegin function sectionHeaderBuilder could not be null. $ErorrFlagEnd',
),
assert(
(cellBuilder != null),
'$ErorrFlagBegin function cellBuilder could not be null. $ErorrFlagEnd',
),
assert(
(sectionHeaderHeight != null),
'$ErorrFlagBegin function sectionHeaderHeight could not be null. $ErorrFlagEnd',
),
assert(
(cellHeight != null),
'$ErorrFlagBegin function cellHeight could not be null. $ErorrFlagEnd',
);
@override
_FlutterTableViewState createState() {
return _FlutterTableViewState();
}
/// How many section.
final int sectionCount;
/// How many item in each section.
final RowCountAtSection rowCountAtSection;
/// You can through sectionHeaderBuilder create section header widget.
/// Each section has at most one headWidget.
/// In a special section, if you don't need section header widget, you can return null.
final SectionHeaderBuilder sectionHeaderBuilder;
/// You can through cellBuilder create items.
final CellBuilder cellBuilder;
/// return each item widget height.
final CellHeight cellHeight;
/// return each section header widget height.
final SectionHeaderHeight sectionHeaderHeight;
/// You can wrap a widget for listView
final ListViewFatherWidgetBuilder? listViewFatherWidgetBuilder;
/// see ScrollView controller
final ScrollController? controller;
/// see ScrollView physics
final ScrollPhysics? physics;
/// see ScrollView shrinkWrap
final bool shrinkWrap;
final EdgeInsetsGeometry padding;
/// see ScrollView cacheExtent
final double cacheExtent;
final Color backgroundColor;
}
class _FlutterTableViewState extends State<FlutterTableView> {
////////////////////////////////////////////////////////////////////
// variables
////////////////////////////////////////////////////////////////////
SectionHeaderModel? currentHeaderModel;
int totalItemCount = 0;
List<SectionHeaderModel> sectionHeaderList = [];
List<int> sectionTotalWidgetCountList = [];
ScrollController? scrollController;
ListView? listView;
bool insideSetStateFlag = false;
////////////////////////////////////////////////////////////////////
// init function
////////////////////////////////////////////////////////////////////
void _initBaseData() {
this.totalItemCount = 0;
this.sectionHeaderList.clear();
this.sectionTotalWidgetCountList.clear();
double offsetY = 0;
for (int section = 0; section < widget.sectionCount; section++) {
int rowCount = widget.rowCountAtSection(section);
Widget sectionHeader = widget.sectionHeaderBuilder(context, section);
double sectionHeight;
if (sectionHeader != null) {
sectionHeight = this.widget.sectionHeaderHeight(context, section);
} else {
sectionHeight = 0;
}
double sectionHeaderY = offsetY;
offsetY += sectionHeight;
int sectionWidgetCount = sectionHeader == null ? rowCount : rowCount + 1;
sectionTotalWidgetCountList.add(sectionWidgetCount);
this.totalItemCount += sectionWidgetCount;
for (int row = 0; row < rowCount; row++) {
offsetY += this.widget.cellHeight(context, section, row);
}
SectionHeaderModel model = SectionHeaderModel(
y: sectionHeaderY,
sectionMaxY: offsetY,
height: sectionHeight,
section: section,
headerWidget: sectionHeader,
);
sectionHeaderList.add(model);
}
}
void _initScrollController() {
this.scrollController = this.widget.controller;
if (this.scrollController == null) {
this.scrollController = ScrollController();
}
this.scrollController?.addListener(() {
double offsetY = this.scrollController?.offset ?? 0;
if (offsetY <= 0.0) {
this._updateCurrentSectionHeaderModel(SectionHeaderModel(), 0);
} else {
int section = 0;
for (int i = 0; i < this.sectionHeaderList.length; i++) {
SectionHeaderModel model = this.sectionHeaderList[i];
if (offsetY >= (model?.y ?? 0) && offsetY <= (model?.sectionMaxY ?? 0)) {
section = i;
break;
}
}
SectionHeaderModel model = this.sectionHeaderList[section];
double delta = (model.sectionMaxY ?? 0) - (this.scrollController?.offset ?? 0);
double topOffset;
if (delta >= (model?.height ?? 0)) {
topOffset = 0.0;
} else {
topOffset = delta - (model?.height ?? 0);
}
this._updateCurrentSectionHeaderModel(model, topOffset);
}
});
}
////////////////////////////////////////////////////////////////////
// create listView
////////////////////////////////////////////////////////////////////
void _createListView() {
if(this.insideSetStateFlag == false) {
this._initBaseData();
}
this.insideSetStateFlag = false;
this.listView = ListView.builder(
controller: this.scrollController,
physics: this.widget.physics ?? AlwaysScrollableScrollPhysics(),
shrinkWrap: this.widget.shrinkWrap,
cacheExtent: this.widget.cacheExtent,
itemBuilder: (BuildContext context, int index) {
Widget? itemWidget;
RowSectionModel model = this._getRowSectionModel(index) ?? RowSectionModel(section: 0, row: 0, haveHeaderWidget: false);
double height;
if (model.row == 0 && (model.haveHeaderWidget ?? false)) {
itemWidget = this.sectionHeaderList[model.section ?? 0].headerWidget;
height = this.widget.sectionHeaderHeight(context, model.section ?? 0);
} else {
int row = (model.haveHeaderWidget ?? false) == false ? (model.row ?? 0) : (model.row ?? 0) - 1;
itemWidget = this.widget.cellBuilder(context, model.section ?? 0, row);
height = this.widget.cellHeight(context, model.section ?? 0, row);
}
return ConstrainedBox(
constraints: BoxConstraints(
minHeight: height,
maxHeight: height,
),
child: itemWidget,
);
},
itemCount: this.totalItemCount,
);
}
////////////////////////////////////////////////////////////////////
// tool function
////////////////////////////////////////////////////////////////////
RowSectionModel? _getRowSectionModel(int index) {
int passCount = 0;
for (int section = 0;
section < this.sectionTotalWidgetCountList.length;
section++) {
int currentSectionWidgetCount = this.sectionTotalWidgetCountList[section];
if (index >= passCount && index < passCount + currentSectionWidgetCount) {
int row = index - passCount;
bool haveSectionHeaderWidget =
this.sectionHeaderList[section].headerWidget != null;
RowSectionModel model = RowSectionModel(
section: section,
row: row,
haveHeaderWidget: haveSectionHeaderWidget,
);
return model;
}
passCount += currentSectionWidgetCount;
}
return null;
}
void _updateCurrentSectionHeaderModel(
SectionHeaderModel model, double topOffset) {
bool needSetState = false;
if (model == null) {
if (this.currentHeaderModel != null) {
this.currentHeaderModel = null;
needSetState = true;
}
} else if (this.currentHeaderModel == null) {
this.currentHeaderModel = model;
this.currentHeaderModel?.topOffset = topOffset;
needSetState = true;
} else {
if (model != this.currentHeaderModel) {
this.currentHeaderModel = model;
needSetState = true;
} else if (model.topOffset != topOffset) {
needSetState = true;
}
this.currentHeaderModel?.topOffset = topOffset;
}
if (needSetState == true) {
this.insideSetStateFlag = true;
setState(() {});
}
}
////////////////////////////////////////////////////////////////////
// life cycle
////////////////////////////////////////////////////////////////////
@override
void dispose() {
super.dispose();
if (this.widget.controller == null) {
this.scrollController?.dispose();
}
}
@override
void initState() {
this._initScrollController();
super.initState();
}
@override
Widget build(BuildContext context) {
this._createListView();
Widget? listViewFatherWidget;
if (this.widget.listViewFatherWidgetBuilder != null) {
listViewFatherWidget =
this.widget.listViewFatherWidgetBuilder!(context, this.listView ?? ListView());
}
Widget? listViewWidget = listViewFatherWidget ?? this.listView;
if (this.currentHeaderModel != null &&
this.currentHeaderModel?.headerWidget != null) {
return Container(
padding: this.widget.padding,
color: widget.backgroundColor,
child: Stack(
children: <Widget>[
Container(
color: Colors.transparent,
child: listViewWidget,
),
Positioned(
top: this.currentHeaderModel?.topOffset,
left: 0.0,
right: 0.0,
height: this.currentHeaderModel?.height,
child: Container(
color: Colors.white,
child: this.currentHeaderModel?.headerWidget,
),
),
],
),
);
}
return Container(
padding: this.widget.padding,
color: this.widget.backgroundColor,
child: Stack(
children: [
Container(
color: Colors.transparent,
child: listViewWidget,
),
],
),
);
}
}
////////////////////////////////////////////////////////////////////
// model class
////////////////////////////////////////////////////////////////////
class RowSectionModel {
RowSectionModel({
this.section,
this.row,
this.haveHeaderWidget,
});
final int? section;
final int? row;
final bool? haveHeaderWidget;
}
class SectionHeaderModel {
SectionHeaderModel({
this.y,
this.sectionMaxY,
this.height,
this.section,
this.headerWidget,
});
final double? y;
final double? sectionMaxY;
final double? height;
final int? section;
final Widget? headerWidget;
double? topOffset;
}