Skip to content

Commit 40a00b9

Browse files
committed
added option to customize sorting icon(flutter#91801)
1 parent 4bf06a3 commit 40a00b9

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ class DataTable extends StatelessWidget {
397397
required this.rows,
398398
this.checkboxHorizontalMargin,
399399
this.border,
400+
this.sortIcon,
400401
}) : assert(columns != null),
401402
assert(columns.isNotEmpty),
402403
assert(sortColumnIndex == null || (sortColumnIndex >= 0 && sortColumnIndex < columns.length)),
@@ -623,6 +624,9 @@ class DataTable extends StatelessWidget {
623624
/// The style to use when painting the boundary and interior divisions of the table.
624625
final TableBorder? border;
625626

627+
///
628+
final Icon? sortIcon;
629+
626630
// Set by the constructor to the index of the only Column that is
627631
// non-numeric, if there is exactly one, otherwise null.
628632
final int? _onlyTextColumn;
@@ -748,6 +752,7 @@ class DataTable extends StatelessWidget {
748752
visible: sorted,
749753
up: sorted ? ascending : null,
750754
duration: _sortArrowAnimationDuration,
755+
sortIcon: sortIcon,
751756
),
752757
const SizedBox(width: _sortArrowPadding),
753758
],
@@ -1121,6 +1126,7 @@ class _SortArrow extends StatefulWidget {
11211126
required this.visible,
11221127
required this.up,
11231128
required this.duration,
1129+
this.sortIcon,
11241130
});
11251131

11261132
final bool visible;
@@ -1129,6 +1135,8 @@ class _SortArrow extends StatefulWidget {
11291135

11301136
final Duration duration;
11311137

1138+
final Icon? sortIcon;
1139+
11321140
@override
11331141
_SortArrowState createState() => _SortArrowState();
11341142
}
@@ -1230,7 +1238,7 @@ class _SortArrowState extends State<_SortArrow> with TickerProviderStateMixin {
12301238
transform: Matrix4.rotationZ(_orientationOffset + _orientationAnimation.value)
12311239
..setTranslationRaw(0.0, _arrowIconBaselineOffset, 0.0),
12321240
alignment: Alignment.center,
1233-
child: const Icon(
1241+
child: widget.sortIcon ?? const Icon(
12341242
Icons.arrow_upward,
12351243
size: _arrowIconSize,
12361244
),

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,4 +1893,55 @@ void main() {
18931893
// Go without crashes.
18941894

18951895
});
1896+
1897+
testWidgets('DataTable sorting icon test', (WidgetTester tester) async {
1898+
const Icon sortIcon = Icon(Icons.arrow_drop_up_rounded, size: 16.0);
1899+
1900+
Widget buildTable({bool sortAscending = true, Icon? sortIcon}) {
1901+
return DataTable(
1902+
sortIcon: sortIcon,
1903+
sortColumnIndex: 0,
1904+
sortAscending: sortAscending,
1905+
columns: <DataColumn>[
1906+
DataColumn(
1907+
label: const Text('Name'),
1908+
tooltip: 'Name',
1909+
onSort: (int columnIndex, bool ascending) {},
1910+
),
1911+
],
1912+
rows: kDesserts.map<DataRow>((Dessert dessert) {
1913+
return DataRow(
1914+
cells: <DataCell>[
1915+
DataCell(
1916+
Text(dessert.name),
1917+
),
1918+
],
1919+
);
1920+
}).toList(),
1921+
);
1922+
}
1923+
1924+
await tester.pumpWidget(MaterialApp(
1925+
home: Material(child: Scaffold(body: buildTable(sortIcon: sortIcon))),
1926+
));
1927+
1928+
Finder iconFinder = find.byWidget(sortIcon);
1929+
expect(iconFinder, findsOneWidget);
1930+
1931+
final Icon icon = tester.widget(iconFinder);
1932+
expect(icon, equals(sortIcon));
1933+
1934+
DataTable dataTable = tester.widget(find.byType(DataTable));
1935+
expect(dataTable.sortIcon, equals(sortIcon));
1936+
1937+
await tester.pumpWidget(MaterialApp(
1938+
home: Material(child: Scaffold(body: buildTable())),
1939+
));
1940+
1941+
iconFinder = find.byWidget(sortIcon);
1942+
expect(iconFinder, findsNothing);
1943+
1944+
dataTable = tester.widget(find.byType(DataTable));
1945+
expect(dataTable.sortIcon, equals(null));
1946+
});
18961947
}

0 commit comments

Comments
 (0)