Skip to content

Commit a1e18a5

Browse files
committed
Add option to customise DataTable sorting icon (flutter#91801)
1 parent 79d77a1 commit a1e18a5

2 files changed

Lines changed: 180 additions & 2 deletions

File tree

‎packages/flutter/lib/src/material/data_table.dart‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ class DataTable extends StatelessWidget {
484484
this.checkboxHorizontalMargin,
485485
this.border,
486486
this.clipBehavior = Clip.none,
487+
this.sortIconWidget,
487488
}) : assert(columns.isNotEmpty),
488489
assert(
489490
sortColumnIndex == null || (sortColumnIndex >= 0 && sortColumnIndex < columns.length),
@@ -538,6 +539,11 @@ class DataTable extends StatelessWidget {
538539
/// Ascending order is represented by an upwards-facing arrow.
539540
final bool sortAscending;
540541

542+
/// The widget to use as the sort icon.
543+
///
544+
/// If null, a default arrow icon will be used.
545+
final Widget? sortIconWidget;
546+
541547
/// Invoked when the user selects or unselects every row, using the
542548
/// checkbox in the heading row.
543549
///
@@ -876,6 +882,7 @@ class DataTable extends StatelessWidget {
876882
required MaterialStateProperty<Color?>? overlayColor,
877883
required MouseCursor? mouseCursor,
878884
required MainAxisAlignment headingRowAlignment,
885+
required Widget? sortIconWidget,
879886
}) {
880887
final ThemeData themeData = Theme.of(context);
881888
final DataTableThemeData dataTableTheme = DataTableTheme.of(context);
@@ -893,6 +900,7 @@ class DataTable extends StatelessWidget {
893900
visible: sorted,
894901
up: sorted ? ascending : null,
895902
duration: _sortArrowAnimationDuration,
903+
icon: sortIconWidget,
896904
),
897905
const SizedBox(width: _sortArrowPadding),
898906
],
@@ -1201,6 +1209,7 @@ class DataTable extends StatelessWidget {
12011209
column.headingRowAlignment ??
12021210
dataTableTheme.headingRowAlignment ??
12031211
MainAxisAlignment.start,
1212+
sortIconWidget: sortIconWidget,
12041213
);
12051214
rowIndex = 1;
12061215
for (final DataRow row in rows) {
@@ -1318,14 +1327,16 @@ class TableRowInkWell extends InkResponse {
13181327
}
13191328

13201329
class _SortArrow extends StatefulWidget {
1321-
const _SortArrow({required this.visible, required this.up, required this.duration});
1330+
const _SortArrow({required this.visible, required this.up, required this.duration, required this.icon});
13221331

13231332
final bool visible;
13241333

13251334
final bool? up;
13261335

13271336
final Duration duration;
13281337

1338+
final Widget? icon;
1339+
13291340
@override
13301341
_SortArrowState createState() => _SortArrowState();
13311342
}
@@ -1424,7 +1435,7 @@ class _SortArrowState extends State<_SortArrow> with TickerProviderStateMixin {
14241435
transform: Matrix4.rotationZ(_orientationOffset + _orientationAnimation.value)
14251436
..setTranslationRaw(0.0, _arrowIconBaselineOffset, 0.0),
14261437
alignment: Alignment.center,
1427-
child: const Icon(Icons.arrow_upward, size: _arrowIconSize),
1438+
child: widget.icon ?? const Icon(Icons.arrow_upward, size: _arrowIconSize),
14281439
),
14291440
);
14301441
}

‎packages/flutter/test/material/data_table_test.dart‎

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,173 @@ void main() {
23152315

23162316
semantics.dispose();
23172317
});
2318+
2319+
testWidgets('DataTable sortIconWidget displays custom icon', (WidgetTester tester) async {
2320+
const Widget customSortIcon = Icon(Icons.star, key: Key('custom-sort-icon'));
2321+
2322+
Widget buildTable() {
2323+
return DataTable(
2324+
sortColumnIndex: 0,
2325+
sortAscending: true,
2326+
sortIconWidget: customSortIcon,
2327+
columns: <DataColumn>[
2328+
DataColumn(
2329+
label: const Text('Name'),
2330+
onSort: (int columnIndex, bool ascending) {},
2331+
),
2332+
],
2333+
rows: kDesserts.map<DataRow>((Dessert dessert) {
2334+
return DataRow(cells: <DataCell>[DataCell(Text(dessert.name))]);
2335+
}).toList(),
2336+
);
2337+
}
2338+
2339+
await tester.pumpWidget(MaterialApp(home: Material(child: buildTable())));
2340+
2341+
// Verify that the custom sort icon is displayed
2342+
expect(find.byKey(const Key('custom-sort-icon')), findsOneWidget);
2343+
2344+
// Verify that the default arrow icon is not displayed
2345+
expect(find.widgetWithIcon(Transform, Icons.arrow_upward), findsNothing);
2346+
});
2347+
2348+
testWidgets('DataTable sortIconWidget receives proper rotation for descending sort', (WidgetTester tester) async {
2349+
const Widget customSortIcon = Icon(Icons.star, key: Key('custom-sort-icon'));
2350+
2351+
Widget buildTable({bool sortAscending = true}) {
2352+
return DataTable(
2353+
sortColumnIndex: 0,
2354+
sortAscending: sortAscending,
2355+
sortIconWidget: customSortIcon,
2356+
columns: <DataColumn>[
2357+
DataColumn(
2358+
label: const Text('Name'),
2359+
onSort: (int columnIndex, bool ascending) {},
2360+
),
2361+
],
2362+
rows: kDesserts.map<DataRow>((Dessert dessert) {
2363+
return DataRow(cells: <DataCell>[DataCell(Text(dessert.name))]);
2364+
}).toList(),
2365+
);
2366+
}
2367+
2368+
// Test ascending sort (no rotation)
2369+
await tester.pumpWidget(MaterialApp(home: Material(child: buildTable())));
2370+
2371+
final Finder customIconFinder = find.descendant(
2372+
of: find.byType(DataTable),
2373+
matching: find.byKey(const Key('custom-sort-icon')),
2374+
);
2375+
2376+
// Find the Transform widget that wraps the custom icon
2377+
final Finder transformFinder = find.ancestor(
2378+
of: customIconFinder,
2379+
matching: find.byType(Transform),
2380+
);
2381+
2382+
Transform transformOfCustomIcon = tester.widget<Transform>(transformFinder);
2383+
expect(transformOfCustomIcon.transform.getRotation(), equals(Matrix3.identity()));
2384+
2385+
// Test descending sort (180-degree rotation)
2386+
await tester.pumpWidget(MaterialApp(home: Material(child: buildTable(sortAscending: false))));
2387+
await tester.pumpAndSettle();
2388+
2389+
transformOfCustomIcon = tester.widget<Transform>(transformFinder);
2390+
expect(transformOfCustomIcon.transform.getRotation(), equals(Matrix3.rotationZ(math.pi)));
2391+
});
2392+
2393+
testWidgets('DataTable falls back to default icon when sortIconWidget is null', (WidgetTester tester) async {
2394+
Widget buildTable() {
2395+
return DataTable(
2396+
sortColumnIndex: 0,
2397+
sortAscending: true,
2398+
sortIconWidget: null, // Explicitly set to null
2399+
columns: <DataColumn>[
2400+
DataColumn(
2401+
label: const Text('Name'),
2402+
onSort: (int columnIndex, bool ascending) {},
2403+
),
2404+
],
2405+
rows: kDesserts.map<DataRow>((Dessert dessert) {
2406+
return DataRow(cells: <DataCell>[DataCell(Text(dessert.name))]);
2407+
}).toList(),
2408+
);
2409+
}
2410+
2411+
await tester.pumpWidget(MaterialApp(home: Material(child: buildTable())));
2412+
2413+
// Verify that the default arrow icon is displayed
2414+
final Finder iconFinder = find.descendant(
2415+
of: find.byType(DataTable),
2416+
matching: find.widgetWithIcon(Transform, Icons.arrow_upward),
2417+
);
2418+
expect(iconFinder, findsOneWidget);
2419+
});
2420+
2421+
testWidgets('DataTable sortIconWidget works with different custom widgets', (WidgetTester tester) async {
2422+
const Widget customIcon1 = Icon(Icons.keyboard_arrow_up, key: Key('custom-icon-1'));
2423+
const Widget customIcon2 = Text('↑', key: Key('custom-text-icon'));
2424+
2425+
Widget buildTable({Widget? sortIcon}) {
2426+
return DataTable(
2427+
sortColumnIndex: 0,
2428+
sortAscending: true,
2429+
sortIconWidget: sortIcon,
2430+
columns: <DataColumn>[
2431+
DataColumn(
2432+
label: const Text('Name'),
2433+
onSort: (int columnIndex, bool ascending) {},
2434+
),
2435+
],
2436+
rows: kDesserts.map<DataRow>((Dessert dessert) {
2437+
return DataRow(cells: <DataCell>[DataCell(Text(dessert.name))]);
2438+
}).toList(),
2439+
);
2440+
}
2441+
2442+
// Test with custom Icon widget
2443+
await tester.pumpWidget(MaterialApp(home: Material(child: buildTable(sortIcon: customIcon1))));
2444+
expect(find.byKey(const Key('custom-icon-1')), findsOneWidget);
2445+
expect(find.byKey(const Key('custom-text-icon')), findsNothing);
2446+
2447+
// Test with custom Text widget
2448+
await tester.pumpWidget(MaterialApp(home: Material(child: buildTable(sortIcon: customIcon2))));
2449+
expect(find.byKey(const Key('custom-text-icon')), findsOneWidget);
2450+
expect(find.byKey(const Key('custom-icon-1')), findsNothing);
2451+
});
2452+
2453+
testWidgets('DataTable sortIconWidget only appears on sortable columns', (WidgetTester tester) async {
2454+
const Widget customSortIcon = Icon(Icons.star, key: Key('custom-sort-icon'));
2455+
2456+
Widget buildTable() {
2457+
return DataTable(
2458+
sortColumnIndex: 1, // Second column is sortable
2459+
sortAscending: true,
2460+
sortIconWidget: customSortIcon,
2461+
columns: <DataColumn>[
2462+
const DataColumn(
2463+
label: Text('Name'),
2464+
// No onSort callback - not sortable
2465+
),
2466+
DataColumn(
2467+
label: const Text('Calories'),
2468+
onSort: (int columnIndex, bool ascending) {},
2469+
),
2470+
],
2471+
rows: kDesserts.map<DataRow>((Dessert dessert) {
2472+
return DataRow(cells: <DataCell>[
2473+
DataCell(Text(dessert.name)),
2474+
DataCell(Text('${dessert.calories}')),
2475+
]);
2476+
}).toList(),
2477+
);
2478+
}
2479+
2480+
await tester.pumpWidget(MaterialApp(home: Material(child: buildTable())));
2481+
2482+
// Should find exactly one custom sort icon (only on the sortable column)
2483+
expect(find.byKey(const Key('custom-sort-icon')), findsOneWidget);
2484+
});
23182485
}
23192486

23202487
RenderParagraph _getTextRenderObject(WidgetTester tester, String text) {

0 commit comments

Comments
 (0)