Unverified Commit 6d2d9b2f authored by Bonsai11's avatar Bonsai11 Committed by GitHub

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
parent 90269c7d
...@@ -94,6 +94,7 @@ class DataRow { ...@@ -94,6 +94,7 @@ class DataRow {
this.key, this.key,
this.selected = false, this.selected = false,
this.onSelectChanged, this.onSelectChanged,
this.onLongPress,
this.color, this.color,
required this.cells, required this.cells,
}) : assert(cells != null); }) : assert(cells != null);
...@@ -106,6 +107,7 @@ class DataRow { ...@@ -106,6 +107,7 @@ class DataRow {
int? index, int? index,
this.selected = false, this.selected = false,
this.onSelectChanged, this.onSelectChanged,
this.onLongPress,
this.color, this.color,
required this.cells, required this.cells,
}) : assert(cells != null), }) : assert(cells != null),
...@@ -138,6 +140,14 @@ class DataRow { ...@@ -138,6 +140,14 @@ class DataRow {
/// that particular cell. /// that particular cell.
final ValueChanged<bool?>? onSelectChanged; 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. /// Whether the row is selected.
/// ///
/// If [onSelectChanged] is non-null for any row in the table, then /// If [onSelectChanged] is non-null for any row in the table, then
...@@ -786,6 +796,7 @@ class DataTable extends StatelessWidget { ...@@ -786,6 +796,7 @@ class DataTable extends StatelessWidget {
required GestureTapDownCallback? onTapDown, required GestureTapDownCallback? onTapDown,
required GestureTapCancelCallback? onTapCancel, required GestureTapCancelCallback? onTapCancel,
required MaterialStateProperty<Color?>? overlayColor, required MaterialStateProperty<Color?>? overlayColor,
required GestureLongPressCallback? onRowLongPress,
}) { }) {
final ThemeData themeData = Theme.of(context); final ThemeData themeData = Theme.of(context);
if (showEditIcon) { if (showEditIcon) {
...@@ -828,9 +839,10 @@ class DataTable extends StatelessWidget { ...@@ -828,9 +839,10 @@ class DataTable extends StatelessWidget {
overlayColor: overlayColor, overlayColor: overlayColor,
child: label, child: label,
); );
} else if (onSelectChanged != null) { } else if (onSelectChanged != null || onRowLongPress != null) {
label = TableRowInkWell( label = TableRowInkWell(
onTap: onSelectChanged, onTap: onSelectChanged,
onLongPress: onRowLongPress,
overlayColor: overlayColor, overlayColor: overlayColor,
child: label, child: label,
); );
...@@ -996,6 +1008,7 @@ class DataTable extends StatelessWidget { ...@@ -996,6 +1008,7 @@ class DataTable extends StatelessWidget {
onTapDown: cell.onTapDown, onTapDown: cell.onTapDown,
onSelectChanged: row.onSelectChanged == null ? null : () => row.onSelectChanged?.call(!row.selected), onSelectChanged: row.onSelectChanged == null ? null : () => row.onSelectChanged?.call(!row.selected),
overlayColor: row.color ?? effectiveDataRowColor, overlayColor: row.color ?? effectiveDataRowColor,
onRowLongPress: row.onLongPress,
); );
rowIndex += 1; rowIndex += 1;
} }
......
...@@ -44,6 +44,9 @@ void main() { ...@@ -44,6 +44,9 @@ void main() {
onSelectChanged: (bool? selected) { onSelectChanged: (bool? selected) {
log.add('row-selected: ${dessert.name}'); log.add('row-selected: ${dessert.name}');
}, },
onLongPress: () {
log.add('onLongPress: ${dessert.name}');
},
cells: <DataCell>[ cells: <DataCell>[
DataCell( DataCell(
Text(dessert.name), Text(dessert.name),
...@@ -87,6 +90,11 @@ void main() { ...@@ -87,6 +90,11 @@ void main() {
expect(log, <String>['row-selected: Cupcake']); expect(log, <String>['row-selected: Cupcake']);
log.clear(); log.clear();
await tester.longPress(find.text('Cupcake'));
expect(log, <String>['onLongPress: Cupcake']);
log.clear();
await tester.tap(find.text('Calories')); await tester.tap(find.text('Calories'));
expect(log, <String>['column-sort: 1 true']); expect(log, <String>['column-sort: 1 true']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment