@@ -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
23202487RenderParagraph _getTextRenderObject (WidgetTester tester, String text) {
0 commit comments