[Material] Parent checkbox in DataTable should deselect all, if children checkboxes are disabled or selected (#67938)
This commit is contained in:
@@ -868,9 +868,10 @@ class DataTable extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
final bool anyRowSelectable = rows.any((DataRow row) => row.onSelectChanged != null);
|
final bool anyRowSelectable = rows.any((DataRow row) => row.onSelectChanged != null);
|
||||||
final bool displayCheckboxColumn = showCheckboxColumn && anyRowSelectable;
|
final bool displayCheckboxColumn = showCheckboxColumn && anyRowSelectable;
|
||||||
final Iterable<DataRow> rowsChecked = displayCheckboxColumn ?
|
final Iterable<DataRow> rowsWithCheckbox = displayCheckboxColumn ?
|
||||||
rows.where((DataRow row) => row.onSelectChanged != null && row.selected) : <DataRow>[];
|
rows.where((DataRow row) => row.onSelectChanged != null) : <DataRow>[];
|
||||||
final bool allChecked = displayCheckboxColumn && rowsChecked.length == rows.length;
|
final Iterable<DataRow> rowsChecked = rowsWithCheckbox.where((DataRow row) => row.selected);
|
||||||
|
final bool allChecked = displayCheckboxColumn && rowsChecked.length == rowsWithCheckbox.length;
|
||||||
final bool anyChecked = displayCheckboxColumn && rowsChecked.isNotEmpty;
|
final bool anyChecked = displayCheckboxColumn && rowsChecked.isNotEmpty;
|
||||||
final bool someChecked = anyChecked && !allChecked;
|
final bool someChecked = anyChecked && !allChecked;
|
||||||
final double effectiveHorizontalMargin = horizontalMargin
|
final double effectiveHorizontalMargin = horizontalMargin
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ void main() {
|
|||||||
testWidgets('DataTable control test - tristate', (WidgetTester tester) async {
|
testWidgets('DataTable control test - tristate', (WidgetTester tester) async {
|
||||||
final List<String> log = <String>[];
|
final List<String> log = <String>[];
|
||||||
const int numItems = 3;
|
const int numItems = 3;
|
||||||
Widget buildTable(List<bool> selected) {
|
Widget buildTable(List<bool> selected, {int? disabledIndex}) {
|
||||||
return DataTable(
|
return DataTable(
|
||||||
onSelectAll: (bool? value) {
|
onSelectAll: (bool? value) {
|
||||||
log.add('select-all: $value');
|
log.add('select-all: $value');
|
||||||
@@ -123,7 +123,7 @@ void main() {
|
|||||||
(int index) => DataRow(
|
(int index) => DataRow(
|
||||||
cells: <DataCell>[DataCell(Text('Row $index'))],
|
cells: <DataCell>[DataCell(Text('Row $index'))],
|
||||||
selected: selected[index],
|
selected: selected[index],
|
||||||
onSelectChanged: (bool? value) {
|
onSelectChanged: index == disabledIndex ? null : (bool? value) {
|
||||||
log.add('row-selected: $index');
|
log.add('row-selected: $index');
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -157,6 +157,21 @@ void main() {
|
|||||||
|
|
||||||
expect(log, <String>['select-all: false']);
|
expect(log, <String>['select-all: false']);
|
||||||
log.clear();
|
log.clear();
|
||||||
|
|
||||||
|
// Tapping the parent checkbox when all rows are selected and one is
|
||||||
|
// disabled, deselects all.
|
||||||
|
await tester.pumpWidget(MaterialApp(
|
||||||
|
home: Material(
|
||||||
|
child: buildTable(
|
||||||
|
<bool>[true, true, false],
|
||||||
|
disabledIndex: 2,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
await tester.tap(find.byType(Checkbox).first);
|
||||||
|
|
||||||
|
expect(log, <String>['select-all: false']);
|
||||||
|
log.clear();
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('DataTable control test - no checkboxes', (WidgetTester tester) async {
|
testWidgets('DataTable control test - no checkboxes', (WidgetTester tester) async {
|
||||||
|
|||||||
Reference in New Issue
Block a user