Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customizable mouse cursor to DataTable #123128

Merged
merged 2 commits into from Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/flutter/lib/src/material/data_table.dart
Expand Up @@ -44,6 +44,7 @@ class DataColumn {
this.tooltip,
this.numeric = false,
this.onSort,
this.mouseCursor,
});

/// The column heading.
Expand Down Expand Up @@ -85,6 +86,13 @@ class DataColumn {
final DataColumnSortCallback? onSort;

bool get _debugInteractive => onSort != null;

/// The cursor for a mouse pointer when it enters or is hovering over the
/// heading row.
///
/// If this is null, then the value of [DataTableThemeData.headingCellCursor]
/// is used. If that's null, then [SystemMouseCursors.click] is used.
TahaTesser marked this conversation as resolved.
Show resolved Hide resolved
final MouseCursor? mouseCursor;
}

/// Row configuration and cell data for a [DataTable].
Expand All @@ -106,6 +114,7 @@ class DataRow {
this.onSelectChanged,
this.onLongPress,
this.color,
this.mouseCursor,
required this.cells,
});

Expand All @@ -119,6 +128,7 @@ class DataRow {
this.onSelectChanged,
this.onLongPress,
this.color,
this.mouseCursor,
required this.cells,
}) : key = ValueKey<int?>(index);

Expand Down Expand Up @@ -205,6 +215,16 @@ class DataRow {
/// <https://material.io/design/interaction/states.html#anatomy>.
final MaterialStateProperty<Color?>? color;

/// The cursor for a mouse pointer when it enters or is hovering over the
/// data row.
///
/// If this is null, then the value of [DataTableThemeData.dataRowCursor]
TahaTesser marked this conversation as resolved.
Show resolved Hide resolved
/// is used. If that's null, then [MaterialStateMouseCursor.clickable] is used.
TahaTesser marked this conversation as resolved.
Show resolved Hide resolved
///
/// See also:
/// * [MaterialStateMouseCursor], which can be used to create a [MouseCursor].
final MaterialStateProperty<MouseCursor?>? mouseCursor;

bool get _debugInteractive => onSelectChanged != null || cells.any((DataCell cell) => cell._debugInteractive);
}

Expand Down Expand Up @@ -738,6 +758,7 @@ class DataTable extends StatelessWidget {
required ValueChanged<bool?>? onCheckboxChanged,
required MaterialStateProperty<Color?>? overlayColor,
required bool tristate,
MouseCursor? rowMouseCursor,
}) {
final ThemeData themeData = Theme.of(context);
final double effectiveHorizontalMargin = horizontalMargin
Expand Down Expand Up @@ -769,6 +790,7 @@ class DataTable extends StatelessWidget {
contents = TableRowInkWell(
onTap: onRowTap,
overlayColor: overlayColor,
mouseCursor: rowMouseCursor,
child: contents,
);
}
Expand All @@ -788,6 +810,7 @@ class DataTable extends StatelessWidget {
required bool sorted,
required bool ascending,
required MaterialStateProperty<Color?>? overlayColor,
required MouseCursor? mouseCursor,
}) {
final ThemeData themeData = Theme.of(context);
final DataTableThemeData dataTableTheme = DataTableTheme.of(context);
Expand Down Expand Up @@ -838,6 +861,7 @@ class DataTable extends StatelessWidget {
label = InkWell(
onTap: onSort,
overlayColor: overlayColor,
mouseCursor: mouseCursor,
child: label,
);
return label;
Expand All @@ -858,6 +882,7 @@ class DataTable extends StatelessWidget {
required GestureTapCancelCallback? onTapCancel,
required MaterialStateProperty<Color?>? overlayColor,
required GestureLongPressCallback? onRowLongPress,
required MouseCursor? mouseCursor,
}) {
final ThemeData themeData = Theme.of(context);
final DataTableThemeData dataTableTheme = DataTableTheme.of(context);
Expand Down Expand Up @@ -905,13 +930,15 @@ class DataTable extends StatelessWidget {
onTapCancel: onTapCancel,
onTapDown: onTapDown,
overlayColor: overlayColor,
mouseCursor: mouseCursor,
child: label,
);
} else if (onSelectChanged != null || onRowLongPress != null) {
label = TableRowInkWell(
onTap: onSelectChanged,
onLongPress: onRowLongPress,
overlayColor: overlayColor,
mouseCursor: mouseCursor,
child: label,
);
}
Expand Down Expand Up @@ -1014,12 +1041,19 @@ class DataTable extends StatelessWidget {
);
rowIndex = 1;
for (final DataRow row in rows) {
final Set<MaterialState> states = <MaterialState>{
if (row.selected)
MaterialState.selected,
if (row.onSelectChanged == null)
MaterialState.disabled,
};
tableRows[rowIndex].children[0] = _buildCheckbox(
context: context,
checked: row.selected,
onRowTap: row.onSelectChanged == null ? null : () => row.onSelectChanged?.call(!row.selected),
onCheckboxChanged: row.onSelectChanged,
overlayColor: row.color ?? effectiveDataRowColor,
rowMouseCursor: row.mouseCursor?.resolve(states) ?? dataTableTheme.dataRowCursor?.resolve(states),
tristate: false,
);
rowIndex += 1;
Expand Down Expand Up @@ -1067,9 +1101,16 @@ class DataTable extends StatelessWidget {
sorted: dataColumnIndex == sortColumnIndex,
ascending: sortAscending,
overlayColor: effectiveHeadingRowColor,
mouseCursor: column.onSort != null ? column.mouseCursor ?? dataTableTheme.headingCellCursor : null,
);
rowIndex = 1;
for (final DataRow row in rows) {
final Set<MaterialState> states = <MaterialState>{
if (row.selected)
MaterialState.selected,
if (row.onSelectChanged == null)
MaterialState.disabled,
};
final DataCell cell = row.cells[dataColumnIndex];
tableRows[rowIndex].children[displayColumnIndex] = _buildDataCell(
context: context,
Expand All @@ -1086,6 +1127,7 @@ class DataTable extends StatelessWidget {
onSelectChanged: row.onSelectChanged == null ? null : () => row.onSelectChanged?.call(!row.selected),
overlayColor: row.color ?? effectiveDataRowColor,
onRowLongPress: row.onLongPress,
mouseCursor: row.mouseCursor?.resolve(states) ?? dataTableTheme.dataRowCursor?.resolve(states),
);
rowIndex += 1;
}
Expand Down Expand Up @@ -1138,6 +1180,7 @@ class TableRowInkWell extends InkResponse {
super.onLongPress,
super.onHighlightChanged,
super.overlayColor,
super.mouseCursor,
}) : super(
containedInkWell: true,
highlightShape: BoxShape.rectangle,
Expand Down
22 changes: 21 additions & 1 deletion packages/flutter/lib/src/material/data_table_theme.dart
Expand Up @@ -55,6 +55,8 @@ class DataTableThemeData with Diagnosticable {
this.columnSpacing,
this.dividerThickness,
this.checkboxHorizontalMargin,
this.headingCellCursor,
this.dataRowCursor,
}) : assert(dataRowMinHeight == null || dataRowMaxHeight == null || dataRowMaxHeight >= dataRowMinHeight),
assert(dataRowHeight == null || (dataRowMinHeight == null && dataRowMaxHeight == null),
'dataRowHeight ($dataRowHeight) must not be set if dataRowMinHeight ($dataRowMinHeight) or dataRowMaxHeight ($dataRowMaxHeight) are set.'),
Expand Down Expand Up @@ -106,6 +108,12 @@ class DataTableThemeData with Diagnosticable {
/// {@macro flutter.material.dataTable.checkboxHorizontalMargin}
final double? checkboxHorizontalMargin;

/// If specified, overrides the default value of [DataColumn.mouseCursor].
final MouseCursor? headingCellCursor;

/// If specified, overrides the default value of [DataRow.mouseCursor].
final MaterialStateProperty<MouseCursor?>? dataRowCursor;

/// Creates a copy of this object but with the given fields replaced with the
/// new values.
DataTableThemeData copyWith({
Expand All @@ -126,6 +134,8 @@ class DataTableThemeData with Diagnosticable {
double? columnSpacing,
double? dividerThickness,
double? checkboxHorizontalMargin,
MouseCursor? headingCellCursor,
MaterialStateProperty<MouseCursor?>? dataRowCursor,
}) {
return DataTableThemeData(
decoration: decoration ?? this.decoration,
Expand All @@ -141,6 +151,8 @@ class DataTableThemeData with Diagnosticable {
columnSpacing: columnSpacing ?? this.columnSpacing,
dividerThickness: dividerThickness ?? this.dividerThickness,
checkboxHorizontalMargin: checkboxHorizontalMargin ?? this.checkboxHorizontalMargin,
headingCellCursor: headingCellCursor ?? this.headingCellCursor,
dataRowCursor: dataRowCursor ?? this.dataRowCursor,
);
}

Expand All @@ -166,6 +178,8 @@ class DataTableThemeData with Diagnosticable {
columnSpacing: lerpDouble(a.columnSpacing, b.columnSpacing, t),
dividerThickness: lerpDouble(a.dividerThickness, b.dividerThickness, t),
checkboxHorizontalMargin: lerpDouble(a.checkboxHorizontalMargin, b.checkboxHorizontalMargin, t),
headingCellCursor: t < 0.5 ? a.headingCellCursor : b.headingCellCursor,
dataRowCursor: t < 0.5 ? a.dataRowCursor : b.dataRowCursor,
);
}

Expand All @@ -183,6 +197,8 @@ class DataTableThemeData with Diagnosticable {
columnSpacing,
dividerThickness,
checkboxHorizontalMargin,
headingCellCursor,
dataRowCursor,
);

@override
Expand All @@ -205,7 +221,9 @@ class DataTableThemeData with Diagnosticable {
&& other.horizontalMargin == horizontalMargin
&& other.columnSpacing == columnSpacing
&& other.dividerThickness == dividerThickness
&& other.checkboxHorizontalMargin == checkboxHorizontalMargin;
&& other.checkboxHorizontalMargin == checkboxHorizontalMargin
&& other.headingCellCursor == headingCellCursor
&& other.dataRowCursor == dataRowCursor;
}

@override
Expand All @@ -223,6 +241,8 @@ class DataTableThemeData with Diagnosticable {
properties.add(DoubleProperty('columnSpacing', columnSpacing, defaultValue: null));
properties.add(DoubleProperty('dividerThickness', dividerThickness, defaultValue: null));
properties.add(DoubleProperty('checkboxHorizontalMargin', checkboxHorizontalMargin, defaultValue: null));
properties.add(DiagnosticsProperty<MouseCursor>('headingCellCursor', headingCellCursor, defaultValue: null));
properties.add(DiagnosticsProperty<MaterialStateProperty<MouseCursor?>>('dataRowCursor', dataRowCursor, defaultValue: null));
TahaTesser marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
145 changes: 145 additions & 0 deletions packages/flutter/test/material/data_table_test.dart
Expand Up @@ -9,6 +9,7 @@ import 'dart:math' as math;

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:vector_math/vector_math_64.dart' show Matrix3;

Expand Down Expand Up @@ -2072,4 +2073,148 @@ void main() {
expect(() => createDataTable(dataRowHeight: 1.0, dataRowMinHeight: 2.0), throwsA(predicate((AssertionError e) =>
e.toString().contains('dataRowHeight == null || (dataRowMinHeight == null && dataRowMaxHeight == null)'))));
});

testWidgets('DataRow cursor resolves MaterialStateMouseCursor correctly', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DataTable(
sortColumnIndex: 0,
columns: <DataColumn>[
DataColumn(
label: const Text('A'),
onSort: (int columnIndex, bool ascending) {},
),
const DataColumn(label: Text('B')),
],
rows: <DataRow>[
DataRow(
mouseCursor: MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return SystemMouseCursors.copy;
}
return SystemMouseCursors.forbidden;
}),
onSelectChanged: (bool? selected) {},
cells: const <DataCell>[
DataCell(Text('Data 1')),
DataCell(Text('Data 2')),
],
),
DataRow(
selected: true,
onSelectChanged: (bool? selected) {},
mouseCursor: MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return SystemMouseCursors.copy;
}
return SystemMouseCursors.forbidden;
}),
cells: const <DataCell>[
DataCell(Text('Data 3')),
DataCell(Text('Data 4')),
],
),
],
),
),
),
);

final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
await gesture.addPointer(location: tester.getCenter(find.text('Data 1')));
await tester.pump();

expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.forbidden);

await gesture.moveTo(tester.getCenter(find.text('Data 3')));
await tester.pump();

expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.copy);
});

testWidgets("DataRow cursor doesn't update checkbox cursor", (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DataTable(
sortColumnIndex: 0,
columns: <DataColumn>[
DataColumn(
label: const Text('A'),
onSort: (int columnIndex, bool ascending) {},
),
const DataColumn(label: Text('B')),
],
rows: <DataRow>[
DataRow(
onSelectChanged: (bool? selected) {},
mouseCursor: const MaterialStatePropertyAll<MouseCursor>(SystemMouseCursors.copy),
cells: const <DataCell>[
DataCell(Text('Data')),
DataCell(Text('Data 2')),
],
),
],
),
),
),
);

final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
await gesture.addPointer(location: tester.getCenter(find.byType(Checkbox).last));
await tester.pump();

// Test that the checkbox cursor is not changed.
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);

await gesture.moveTo(tester.getCenter(find.text('Data')));
await tester.pump();

// Test that cursor is updated for the row.
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.copy);
});

testWidgets('Heading cell cursor can only updated if onSort is non-null', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DataTable(
sortColumnIndex: 0,
columns: <DataColumn>[
DataColumn(
mouseCursor: SystemMouseCursors.copy,
label: const Text('A'),
onSort: (int columnIndex, bool ascending) {},
),
const DataColumn(
mouseCursor: SystemMouseCursors.copy,
label: Text('B'),
),
],
rows: <DataRow>[
DataRow(
onSelectChanged: (bool? selected) {},
cells: const <DataCell>[
DataCell(Text('Data')),
DataCell(Text('Data 2')),
],
),
],
),
),
),
);

final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
await gesture.addPointer(location: tester.getCenter(find.text('A')));
await tester.pump();

expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.copy);

await gesture.moveTo(tester.getCenter(find.text('B')));
await tester.pump();

expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
});
}