diff --git a/packages/flutter/lib/src/material/data_table.dart b/packages/flutter/lib/src/material/data_table.dart index 7a796cbe9e..53f0a123c0 100644 --- a/packages/flutter/lib/src/material/data_table.dart +++ b/packages/flutter/lib/src/material/data_table.dart @@ -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? 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? 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; } diff --git a/packages/flutter/test/material/data_table_test.dart b/packages/flutter/test/material/data_table_test.dart index 01c43560e1..6f526f782b 100644 --- a/packages/flutter/test/material/data_table_test.dart +++ b/packages/flutter/test/material/data_table_test.dart @@ -44,6 +44,9 @@ void main() { onSelectChanged: (bool? selected) { log.add('row-selected: ${dessert.name}'); }, + onLongPress: () { + log.add('onLongPress: ${dessert.name}'); + }, cells: [ DataCell( Text(dessert.name), @@ -87,6 +90,11 @@ void main() { expect(log, ['row-selected: Cupcake']); log.clear(); + await tester.longPress(find.text('Cupcake')); + + expect(log, ['onLongPress: Cupcake']); + log.clear(); + await tester.tap(find.text('Calories')); expect(log, ['column-sort: 1 true']);