-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathtable_builder.dart
More file actions
707 lines (608 loc) · 21.1 KB
/
Copy pathtable_builder.dart
File metadata and controls
707 lines (608 loc) · 21.1 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:collection';
import 'package:collection/collection.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_ui_localizations/firebase_ui_localizations.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'query_builder.dart';
/// {@template firebase_ui.database_table}
/// A [PaginatedDataTable] that is connected to Firestore.
///
/// The parameter [columnLabels] is required and is used to
/// - list the columns.
/// - give them a label.
/// - order the columns.
/// - let [FirebaseDatabaseDataTable] know what are the expected keys in a Firestore document.
///
/// An example usage would be:
///
/// ```dart
/// // A collection of {'name': string, 'age': number}
/// final usersCollection = FirebaseDatabase.instance.ref('users');
///
/// // ...
///
/// FirebaseDatabaseDataTable(
/// query: usersCollection,
/// columnLabels: {
/// 'name': Text('User name'),
/// 'age': Text('age'),
/// },
/// );
/// ```
/// {@endtemplate}
class FirebaseDatabaseDataTable extends StatefulWidget {
/// {@macro firebase_ui.database_table}
const FirebaseDatabaseDataTable({
super.key,
required this.query,
required this.columnLabels,
this.header,
this.onError,
this.canDeleteItems = true,
this.actions,
this.sortColumnIndex,
this.sortAscending = true,
double? dataRowMinHeight,
double? dataRowMaxHeight,
this.headingRowHeight = 56.0,
this.horizontalMargin = 24.0,
this.columnSpacing = 56.0,
this.showCheckboxColumn = true,
this.showFirstLastButtons = false,
this.onPageChanged,
this.rowsPerPage = 10,
this.dragStartBehavior = DragStartBehavior.start,
this.arrowHeadColor,
this.checkboxHorizontalMargin,
}) : assert(
columnLabels is LinkedHashMap,
'only LinkedHashMap are supported as header',
), // using an assert instead of a type because `<A, B>{}` types as `Map` but is an instance of `LinkedHashMap`
dataRowMinHeight = dataRowMinHeight ?? kMinInteractiveDimension,
dataRowMaxHeight = dataRowMaxHeight ?? kMinInteractiveDimension;
/// The firestore query that will be displayed
final Query query;
/// Whether documents can be removed from firestore using the table.
final bool canDeleteItems;
/// The columns and their labels based on the property name in Firestore
final Map<String, Widget> columnLabels;
/// When specified, will be called whenever an interaction with Firestore failed,
/// when as when trying to delete an item without the proper rights.
final void Function(Object error, StackTrace stackTrace)? onError;
/// The table card's optional header.
///
/// This is typically a [Text] widget, but can also be a [Row] of
/// [TextButton]s. To show icon buttons at the top end side of the table with
/// a header, set the [actions] property.
///
/// If items in the table are selectable, then, when the selection is not
/// empty, the header is replaced by a count of the selected items. The
/// [actions] are still visible when items are selected.
final Widget? header;
/// Icon buttons to show at the top end side of the table. The [header] must
/// not be null to show the actions.
///
/// Typically, the exact actions included in this list will vary based on
/// whether any rows are selected or not.
///
/// These should be size 24.0 with default padding (8.0).
final List<Widget>? actions;
/// Invoked when the user switches to another page.
///
/// The value is the index of the first row on the currently displayed page.
final void Function(int page)? onPageChanged;
/// The minimum height of each row (excluding the row that contains column headings).
///
/// This value is optional and defaults to kMinInteractiveDimension if not
/// specified.
final double dataRowMinHeight;
/// The maximum height of each row (excluding the row that contains column headings).
///
/// This value is optional and defaults to kMinInteractiveDimension if not
/// specified.
final double dataRowMaxHeight;
/// The current primary sort key's column.
///
/// See [DataTable.sortColumnIndex].
final int? sortColumnIndex;
/// Whether the column mentioned in [sortColumnIndex], if any, is sorted
/// in ascending order.
///
/// See [DataTable.sortAscending].
final bool sortAscending;
/// The height of the heading row.
///
/// This value is optional and defaults to 56.0 if not specified.
final double headingRowHeight;
/// The horizontal margin between the edges of the table and the content
/// in the first and last cells of each row.
///
/// When a checkbox is displayed, it is also the margin between the checkbox
/// the content in the first data column.
///
/// This value defaults to 24.0 to adhere to the Material Design specifications.
///
/// If [checkboxHorizontalMargin] is null, then [horizontalMargin] is also the
/// margin between the edge of the table and the checkbox, as well as the
/// margin between the checkbox and the content in the first data column.
final double horizontalMargin;
/// The horizontal margin between the contents of each data column.
///
/// This value defaults to 56.0 to adhere to the Material Design specifications.
final double columnSpacing;
/// {@macro flutter.material.dataTable.showCheckboxColumn}
final bool showCheckboxColumn;
/// Flag to display the pagination buttons to go to the first and last pages.
final bool showFirstLastButtons;
/// The number of rows to show on each page.
///
/// Defaults to 10
final int rowsPerPage;
/// {@macro flutter.widgets.scrollable.dragStartBehavior}
final DragStartBehavior dragStartBehavior;
/// Defines the color of the arrow heads in the footer.
final Color? arrowHeadColor;
/// Horizontal margin around the checkbox, if it is displayed.
///
/// If null, then [horizontalMargin] is used as the margin between the edge
/// of the table and the checkbox, as well as the margin between the checkbox
/// and the content in the first data column. This value defaults to 24.0.
final double? checkboxHorizontalMargin;
@override
// ignore: library_private_types_in_public_api
_FirestoreTableState createState() => _FirestoreTableState();
}
class _FirestoreTableState extends State<FirebaseDatabaseDataTable> {
late final source = _Source(
getHeaders: () => widget.columnLabels,
getOnError: () => widget.onError,
selectionEnabled: selectionEnabled,
rowsPerPage: widget.rowsPerPage,
onEditItem: onEditItem,
);
bool get selectionEnabled => widget.canDeleteItems;
@override
Widget build(BuildContext context) {
return FirebaseDatabaseQueryBuilder(
query: widget.query,
builder: (context, snapshot, child) {
source.setFromSnapshot(snapshot);
return AnimatedBuilder(
animation: source,
builder: (context, child) {
final actions = [
...?widget.actions,
if (widget.canDeleteItems && source._selectedRowIds.isNotEmpty)
IconButton(
icon: const Icon(Icons.delete),
onPressed: source.onDeleteSelectedItems,
),
];
return PaginatedDataTable(
source: source,
onSelectAll: selectionEnabled ? source.onSelectAll : null,
onPageChanged: widget.onPageChanged,
showCheckboxColumn: widget.showCheckboxColumn,
arrowHeadColor: widget.arrowHeadColor,
checkboxHorizontalMargin: widget.checkboxHorizontalMargin,
columnSpacing: widget.columnSpacing,
dataRowMaxHeight: widget.dataRowMaxHeight,
dataRowMinHeight: widget.dataRowMinHeight,
dragStartBehavior: widget.dragStartBehavior,
headingRowHeight: widget.headingRowHeight,
horizontalMargin: widget.horizontalMargin,
rowsPerPage: widget.rowsPerPage,
showFirstLastButtons: widget.showFirstLastButtons,
sortAscending: widget.sortAscending,
sortColumnIndex: widget.sortColumnIndex,
header:
actions.isEmpty ? null : (widget.header ?? const SizedBox()),
actions: actions.isEmpty ? null : actions,
columns: [
for (final head in widget.columnLabels.values)
DataColumn(
label: head,
)
],
);
},
);
},
);
}
Future<void> onEditItem(
DataSnapshot snapshot,
Object? value,
String propertyName,
) async {
final result = await showDialog<_Edit?>(
context: context,
builder: (context) {
var formState = _initialFormStateOfValue(value);
return StatefulBuilder(
builder: (context, setState) {
void onTypeChanged(_PropertyType? newType) {
setState(
() {
// Delaying dispose as otherwise the next build
// will throw because it'll call "removeListener"
Future.delayed(
const Duration(milliseconds: 10),
formState.dispose,
);
formState = _initialFormStateForType(newType);
},
);
}
void onFormChange(_FormState newFormState) {
setState(() => formState = newFormState);
}
return Dialog(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 16, horizontal: 10),
child: DropdownButtonHideUnderline(
child: Theme(
data: Theme.of(context).copyWith(
inputDecorationTheme: const InputDecorationTheme(
border: OutlineInputBorder(),
isDense: true,
),
),
// Make sure that the modal is as small as possible, yet
// allow the button bar to fill the width
child: IntrinsicWidth(
child: Column(
// Ensures that switching between type correctly
// applies "autoFocus" to the new inputs
key: ObjectKey(formState.type),
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_PropertyTypeDropdown(
formState: formState,
onTypeChanged: onTypeChanged,
),
const SizedBox(height: 8),
_PropertyTypeForm(
formState: formState,
onFormStateChange: onFormChange,
),
const SizedBox(height: 10),
_EditModalButtonBar(
formState: formState,
ref: snapshot.ref,
),
],
),
),
),
),
),
);
},
);
},
);
if (result == null) return;
await snapshot.ref.update({propertyName: result.newValue});
}
}
/// Takes care of the type-specific form
class _PropertyTypeForm extends StatelessWidget {
const _PropertyTypeForm({
required this.formState,
required this.onFormStateChange,
});
final _FormState formState;
final ValueChanged<_FormState> onFormStateChange;
@override
Widget build(BuildContext context) {
final localizations = FirebaseUILocalizations.labelsOf(context);
final formState = this.formState;
if (formState is _NumberFormState) {
return SizedBox(
width: 200,
child: TextField(
autofocus: true,
controller: formState.controller,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(
RegExp('[0-9]+?.?[0-9]*'),
),
],
decoration: InputDecoration(labelText: localizations.valueLabel),
),
);
} else if (formState is _StringFormState) {
return SizedBox(
width: 200,
child: TextField(
autofocus: true,
controller: formState.controller,
decoration: InputDecoration(labelText: localizations.valueLabel),
),
);
} else if (formState is _BooleanFormState) {
return Checkbox(
onChanged: (_) =>
onFormStateChange(_BooleanFormState(!formState.value)),
value: formState.value,
);
}
return const SizedBox();
}
}
class _EditModalButtonBar extends StatelessWidget {
const _EditModalButtonBar({
required this.formState,
required this.ref,
});
final _FormState formState;
final DatabaseReference ref;
@override
Widget build(BuildContext context) {
final localizations = FirebaseUILocalizations.labelsOf(context);
return OverflowBar(
alignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(localizations.cancelLabel),
),
ElevatedButton(
onPressed: () {
Navigator.pop(context, formState.submit(ref));
},
child: Text(localizations.updateLabel),
),
],
);
}
}
class _PropertyTypeDropdown extends StatelessWidget {
const _PropertyTypeDropdown({
required this.formState,
required this.onTypeChanged,
});
final _FormState? formState;
final ValueChanged<_PropertyType?> onTypeChanged;
@override
Widget build(BuildContext context) {
final localizations = FirebaseUILocalizations.labelsOf(context);
return DropdownButtonFormField<_PropertyType?>(
initialValue: formState?.type,
decoration: InputDecoration(labelText: localizations.typeLabel),
items: [
DropdownMenuItem(
value: _PropertyType.string,
child: Text(localizations.stringLabel),
),
DropdownMenuItem(
value: _PropertyType.number,
child: Text(localizations.numberLabel),
),
DropdownMenuItem(
value: _PropertyType.boolean,
child: Text(localizations.booleanLabel),
),
DropdownMenuItem(child: Text(localizations.nullLabel)),
],
onChanged: onTypeChanged,
);
}
}
_FormState _initialFormStateOfValue(Object? value) {
if (value == null) {
return const _NullFormState();
} else if (value is num) {
return _NumberFormState(value.toString());
} else if (value is bool) {
return _BooleanFormState(value);
} else if (value is String) {
return _StringFormState(value);
} else {
throw UnsupportedError('Unknown type ${value.runtimeType}');
}
}
_FormState _initialFormStateForType(_PropertyType? type) {
switch (type) {
case null:
return const _NullFormState();
case _PropertyType.boolean:
return const _BooleanFormState(true);
case _PropertyType.number:
return _NumberFormState('');
case _PropertyType.string:
return _StringFormState('');
}
}
enum _PropertyType {
number,
boolean,
string,
}
abstract class _FormState {
const _FormState();
_PropertyType? get type;
_Edit submit(DatabaseReference ref) => throw UnimplementedError();
void dispose() {}
}
class _NumberFormState extends _FormState {
_NumberFormState(String text)
: controller = TextEditingController(text: text);
final TextEditingController controller;
@override
_PropertyType get type => _PropertyType.number;
@override
_Edit submit(DatabaseReference ref) => _Edit(num.parse(controller.text));
@override
void dispose() => controller.dispose();
}
class _StringFormState extends _FormState {
_StringFormState(String text)
: controller = TextEditingController(text: text);
final TextEditingController controller;
@override
_PropertyType get type => _PropertyType.string;
@override
_Edit submit(DatabaseReference ref) => _Edit(controller.text);
@override
void dispose() => controller.dispose();
}
class _BooleanFormState extends _FormState {
const _BooleanFormState(this.value);
final bool value;
@override
_PropertyType get type => _PropertyType.boolean;
@override
_Edit submit(DatabaseReference ref) => _Edit(value);
}
class _NullFormState extends _FormState {
const _NullFormState();
@override
_PropertyType? get type => null;
@override
_Edit submit(DatabaseReference ref) => _Edit(null);
}
/// A data holder class to differentiate setting a property to null from
/// not modifying the property at all.
class _Edit {
_Edit(this.newValue);
final Object? newValue;
}
class _Source extends DataTableSource {
_Source({
required this.getHeaders,
required this.getOnError,
required bool selectionEnabled,
required int rowsPerPage,
required this.onEditItem,
}) : _selectionEnabled = selectionEnabled,
_rowsPerpage = rowsPerPage;
final void Function(
DataSnapshot snapshot,
Object? value,
String propertyName,
) onEditItem;
int _rowsPerpage;
int get rowsPerPage => _rowsPerpage;
set rowsPerPage(int value) {
if (value != _rowsPerpage) {
_rowsPerpage = value;
notifyListeners();
}
}
bool _selectionEnabled;
bool get selectionEnabled => _selectionEnabled;
set selectionEnabled(bool value) {
if (value != _selectionEnabled) {
_selectionEnabled = value;
notifyListeners();
}
}
final Map<String, Widget> Function() getHeaders;
final void Function(Object error, StackTrace stackTrace)? Function()
getOnError;
final _selectedRowIds = <String>{};
@override
int get selectedRowCount => _selectedRowIds.length;
@override
bool get isRowCountApproximate =>
_previousSnapshot!.isFetching || _previousSnapshot!.hasMore;
@override
int get rowCount {
// Emitting an extra item during load or before reaching the end
// allows the DataTable to show a spinner during load & let the user
// navigate to next page
if (_previousSnapshot!.isFetching || _previousSnapshot!.hasMore) {
return _previousSnapshot!.docs.length + rowsPerPage;
}
return _previousSnapshot!.docs.length;
}
@override
DataRow? getRow(int index) {
if (index >= _previousSnapshot!.docs.length) {
_previousSnapshot!.fetchMore();
}
if (index >= _previousSnapshot!.docs.length) return null;
final doc = _previousSnapshot!.docs[index];
return DataRow(
selected: _selectedRowIds.contains(doc.key),
onSelectChanged: selectionEnabled
? (selected) {
if (selected == null) return;
if ((selected && _selectedRowIds.add(doc.key!)) ||
(!selected && _selectedRowIds.remove(doc.key))) {
notifyListeners();
}
}
: null,
cells: [
for (final head in getHeaders().keys)
DataCell(
_ValueView(
doc.children.firstWhereOrNull((e) => e.key == head)?.value,
),
onTap: () {
onEditItem(
doc,
doc.children.firstWhereOrNull((e) => e.key == head)?.value,
head,
);
},
),
],
);
}
FirebaseQueryBuilderSnapshot? _previousSnapshot;
void setFromSnapshot(FirebaseQueryBuilderSnapshot snapshot) {
if (snapshot == _previousSnapshot) return;
// Try to preserve the selection status when the snapshot got updated,
// such as when more content got loaded.
final wereAllItemsSelected =
_previousSnapshot?.docs.length == _selectedRowIds.length &&
_previousSnapshot!.docs.isNotEmpty;
_previousSnapshot = snapshot;
if (wereAllItemsSelected) onSelectAll(true);
notifyListeners();
}
void onSelectAll(bool? selected) {
if (selected == null) return;
if (selected) {
_selectedRowIds.addAll(_previousSnapshot!.docs.map((e) => e.key!));
} else {
_selectedRowIds.clear();
}
notifyListeners();
}
void onDeleteSelectedItems() {
for (final doc in _previousSnapshot!.docs) {
if (_selectedRowIds.contains(doc.key)) {
doc.ref.remove().then<void>(
(value) => _selectedRowIds.remove(doc.key),
onError: getOnError(),
);
}
}
}
}
class _ValueView extends StatelessWidget {
const _ValueView(this.value);
final Object? value;
@override
Widget build(BuildContext context) {
final value = this.value;
if (value == null) {
return Text('null', style: Theme.of(context).textTheme.bodySmall);
} else {
return Text(value.toString());
}
}
}