Adding onLongPress for DataRow (#87172)

* Adding onLongPress for DataRow

Useful to be able to e.g. start a selection mode of rows or show a drop down menu.

* Test for onLongPress

* Changed parameter
This commit is contained in:
Bonsai11
2021-09-21 20:17:08 +02:00
committed by GitHub
parent 90269c7d77
commit 6d2d9b2f3c
2 changed files with 22 additions and 1 deletions

View File

@@ -94,6 +94,7 @@ class DataRow {
this.key,
this.selected = false,
this.onSelectChanged,
this.onLongPress,
this.color,
required this.cells,
}) : assert(cells != null);
@@ -106,6 +107,7 @@ class DataRow {
int? index,
this.selected = false,
this.onSelectChanged,
this.onLongPress,
this.color,
required this.cells,
}) : assert(cells != null),
@@ -138,6 +140,14 @@ class DataRow {
/// that particular cell.
final ValueChanged<bool?>? onSelectChanged;
/// Called if the row is long-pressed.
///
/// If a [DataCell] in the row has its [DataCell.onTap], [DataCell.onDoubleTap],
/// [DataCell.onLongPress], [DataCell.onTapCancel] or [DataCell.onTapDown] callback defined,
/// that callback behavior overrides the gesture behavior of the row for
/// that particular cell.
final GestureLongPressCallback? onLongPress;
/// Whether the row is selected.
///
/// If [onSelectChanged] is non-null for any row in the table, then
@@ -786,6 +796,7 @@ class DataTable extends StatelessWidget {
required GestureTapDownCallback? onTapDown,
required GestureTapCancelCallback? onTapCancel,
required MaterialStateProperty<Color?>? overlayColor,
required GestureLongPressCallback? onRowLongPress,
}) {
final ThemeData themeData = Theme.of(context);
if (showEditIcon) {
@@ -828,9 +839,10 @@ class DataTable extends StatelessWidget {
overlayColor: overlayColor,
child: label,
);
} else if (onSelectChanged != null) {
} else if (onSelectChanged != null || onRowLongPress != null) {
label = TableRowInkWell(
onTap: onSelectChanged,
onLongPress: onRowLongPress,
overlayColor: overlayColor,
child: label,
);
@@ -996,6 +1008,7 @@ class DataTable extends StatelessWidget {
onTapDown: cell.onTapDown,
onSelectChanged: row.onSelectChanged == null ? null : () => row.onSelectChanged?.call(!row.selected),
overlayColor: row.color ?? effectiveDataRowColor,
onRowLongPress: row.onLongPress,
);
rowIndex += 1;
}

View File

@@ -44,6 +44,9 @@ void main() {
onSelectChanged: (bool? selected) {
log.add('row-selected: ${dessert.name}');
},
onLongPress: () {
log.add('onLongPress: ${dessert.name}');
},
cells: <DataCell>[
DataCell(
Text(dessert.name),
@@ -87,6 +90,11 @@ void main() {
expect(log, <String>['row-selected: Cupcake']);
log.clear();
await tester.longPress(find.text('Cupcake'));
expect(log, <String>['onLongPress: Cupcake']);
log.clear();
await tester.tap(find.text('Calories'));
expect(log, <String>['column-sort: 1 true']);