Unverified Commit 5fe78541 authored by Taha Tesser's avatar Taha Tesser Committed by GitHub

[DataTable]: Add ability to only select row using checkbox (#105123)

parent 032dc542
......@@ -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<Color?>? 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;
}
......
......@@ -293,6 +293,84 @@ void main() {
log.clear();
});
testWidgets('selectableOnGestures disables gestures on row', (WidgetTester tester) async {
final List<String> log = <String>[];
Widget buildTable({ int? sortColumnIndex, bool sortAscending = true }) {
return DataTable(
sortColumnIndex: sortColumnIndex,
sortAscending: sortAscending,
onSelectAll: (bool? value) {
log.add('select-all: $value');
},
columns: <DataColumn>[
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<DataRow>((Dessert dessert) {
return DataRow(
key: ValueKey<String>(dessert.name),
selectableOnGestures: false,
onSelectChanged: (bool? selected) {
log.add('row-selected: ${dessert.name}');
},
onLongPress: () {
log.add('onLongPress: ${dessert.name}');
},
cells: <DataCell>[
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, <String>['row-selected: KitKat']);
log.clear();
});
testWidgets('DataTable overflow test - header', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
......
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