From 5fe78541b363ed70b1f4cff7a665d1239dc8c3c4 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 3 Jun 2022 16:13:07 +0300 Subject: [PATCH] [DataTable]: Add ability to only select row using checkbox (#105123) --- .../flutter/lib/src/material/data_table.dart | 18 ++++- .../test/material/data_table_test.dart | 78 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/data_table.dart b/packages/flutter/lib/src/material/data_table.dart index 73eb1ef53c..64f5fc089f 100644 --- a/packages/flutter/lib/src/material/data_table.dart +++ b/packages/flutter/lib/src/material/data_table.dart @@ -100,6 +100,7 @@ class DataRow { this.selected = false, this.onSelectChanged, this.onLongPress, + this.selectableOnGestures = true, this.color, required this.cells, }) : assert(cells != null); @@ -113,6 +114,7 @@ class DataRow { this.selected = false, this.onSelectChanged, this.onLongPress, + this.selectableOnGestures = true, this.color, required this.cells, }) : assert(cells != null), @@ -163,6 +165,18 @@ class DataRow { /// Otherwise, the checkbox, if present, will not be checked. final bool selected; + /// Whether the row can be selected by using gestures such as + /// tap or long press. + /// + /// If the value is set to false and [onSelectChanged] is non-null, + /// the row can be only selected by toggling the checkbox. + /// + /// If the value is set to false and [onLongPress] is non-null, + /// [onLongPress] callback won't be called. + /// + /// This value is true by default. + final bool selectableOnGestures; + /// The data for this row. /// /// There must be exactly as many cells as there are columns in the @@ -807,6 +821,7 @@ class DataTable extends StatelessWidget { required GestureTapCancelCallback? onTapCancel, required MaterialStateProperty? overlayColor, required GestureLongPressCallback? onRowLongPress, + required bool selectableOnGestures, }) { final ThemeData themeData = Theme.of(context); final DataTableThemeData dataTableTheme = DataTableTheme.of(context); @@ -852,7 +867,7 @@ class DataTable extends StatelessWidget { overlayColor: overlayColor, child: label, ); - } else if (onSelectChanged != null || onRowLongPress != null) { + } else if (selectableOnGestures && (onSelectChanged != null || onRowLongPress != null)) { label = TableRowInkWell( onTap: onSelectChanged, onLongPress: onRowLongPress, @@ -1031,6 +1046,7 @@ class DataTable extends StatelessWidget { onSelectChanged: row.onSelectChanged == null ? null : () => row.onSelectChanged?.call(!row.selected), overlayColor: row.color ?? effectiveDataRowColor, onRowLongPress: row.onLongPress, + selectableOnGestures: row.selectableOnGestures, ); rowIndex += 1; } diff --git a/packages/flutter/test/material/data_table_test.dart b/packages/flutter/test/material/data_table_test.dart index 89758049f9..034a781e57 100644 --- a/packages/flutter/test/material/data_table_test.dart +++ b/packages/flutter/test/material/data_table_test.dart @@ -293,6 +293,84 @@ void main() { log.clear(); }); + testWidgets('selectableOnGestures disables gestures on row', (WidgetTester tester) async { + final List log = []; + + Widget buildTable({ int? sortColumnIndex, bool sortAscending = true }) { + return DataTable( + sortColumnIndex: sortColumnIndex, + sortAscending: sortAscending, + onSelectAll: (bool? value) { + log.add('select-all: $value'); + }, + columns: [ + const DataColumn( + label: Text('Name'), + tooltip: 'Name', + ), + DataColumn( + label: const Text('Calories'), + tooltip: 'Calories', + numeric: true, + onSort: (int columnIndex, bool ascending) { + log.add('column-sort: $columnIndex $ascending'); + }, + ), + ], + rows: kDesserts.map((Dessert dessert) { + return DataRow( + key: ValueKey(dessert.name), + selectableOnGestures: false, + onSelectChanged: (bool? selected) { + log.add('row-selected: ${dessert.name}'); + }, + onLongPress: () { + log.add('onLongPress: ${dessert.name}'); + }, + cells: [ + DataCell( + Text(dessert.name), + ), + DataCell( + Text('${dessert.calories}'), + showEditIcon: true, + onTap: () { + log.add('cell-tap: ${dessert.calories}'); + }, + onDoubleTap: () { + log.add('cell-doubleTap: ${dessert.calories}'); + }, + onLongPress: () { + log.add('cell-longPress: ${dessert.calories}'); + }, + onTapCancel: () { + log.add('cell-tapCancel: ${dessert.calories}'); + }, + onTapDown: (TapDownDetails details) { + log.add('cell-tapDown: ${dessert.calories}'); + }, + ), + ], + ); + }).toList(), + ); + } + + await tester.pumpWidget(MaterialApp( + home: Material(child: buildTable()), + )); + + await tester.tap(find.text('KitKat')); + expect(log.length, 0); + + await tester.longPress(find.text('KitKat')); + expect(log.length, 0); + + await tester.tap(find.byType(Checkbox).last); + expect(log, ['row-selected: KitKat']); + log.clear(); + }); + testWidgets('DataTable overflow test - header', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp(