From 56190fb35b7a557458100118813bac3962150817 Mon Sep 17 00:00:00 2001 From: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com> Date: Mon, 3 Feb 2025 15:58:39 -0800 Subject: [PATCH] Add button flag to NavigationDestination (#161568) This PR is to add `Semantics.button` to `NavigationDestination` so that when the switch control on iOS is turned on, it can traverse the unselected destinations. Fixes https://github.com/flutter/flutter/issues/161563 https://github.com/user-attachments/assets/b761aea6-d1d3-468b-b0a5-249fb03e08e3 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --- .../lib/src/material/navigation_bar.dart | 1 + .../test/material/navigation_bar_test.dart | 39 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/material/navigation_bar.dart b/packages/flutter/lib/src/material/navigation_bar.dart index f55b3602e4..d24344bb7a 100644 --- a/packages/flutter/lib/src/material/navigation_bar.dart +++ b/packages/flutter/lib/src/material/navigation_bar.dart @@ -996,6 +996,7 @@ class _NavigationBarDestinationSemantics extends StatelessWidget { return Semantics( selected: destinationInfo.selectedAnimation.isForwardOrCompleted, container: true, + button: true, child: child, ); }, diff --git a/packages/flutter/test/material/navigation_bar_test.dart b/packages/flutter/test/material/navigation_bar_test.dart index 3b21f4695b..244e664b23 100644 --- a/packages/flutter/test/material/navigation_bar_test.dart +++ b/packages/flutter/test/material/navigation_bar_test.dart @@ -460,6 +460,7 @@ void main() { destinations: const [ NavigationDestination(icon: Icon(Icons.ac_unit), label: 'AC'), NavigationDestination(icon: Icon(Icons.access_alarm), label: 'Alarm'), + NavigationDestination(icon: Icon(Icons.abc), label: 'ABC'), ], ), ); @@ -470,10 +471,11 @@ void main() { expect( tester.getSemantics(find.text('AC')), matchesSemantics( - label: 'AC\nTab 1 of 2', + label: 'AC\nTab 1 of 3', textDirection: TextDirection.ltr, isFocusable: true, isSelected: true, + isButton: true, hasTapAction: true, hasFocusAction: true, ), @@ -481,9 +483,21 @@ void main() { expect( tester.getSemantics(find.text('Alarm')), matchesSemantics( - label: 'Alarm\nTab 2 of 2', + label: 'Alarm\nTab 2 of 3', textDirection: TextDirection.ltr, isFocusable: true, + isButton: true, + hasTapAction: true, + hasFocusAction: true, + ), + ); + expect( + tester.getSemantics(find.text('ABC')), + matchesSemantics( + label: 'ABC\nTab 3 of 3', + textDirection: TextDirection.ltr, + isFocusable: true, + isButton: true, hasTapAction: true, hasFocusAction: true, ), @@ -494,9 +508,10 @@ void main() { expect( tester.getSemantics(find.text('AC')), matchesSemantics( - label: 'AC\nTab 1 of 2', + label: 'AC\nTab 1 of 3', textDirection: TextDirection.ltr, isFocusable: true, + isButton: true, hasTapAction: true, hasFocusAction: true, ), @@ -504,10 +519,22 @@ void main() { expect( tester.getSemantics(find.text('Alarm')), matchesSemantics( - label: 'Alarm\nTab 2 of 2', + label: 'Alarm\nTab 2 of 3', textDirection: TextDirection.ltr, isFocusable: true, isSelected: true, + isButton: true, + hasTapAction: true, + hasFocusAction: true, + ), + ); + expect( + tester.getSemantics(find.text('ABC')), + matchesSemantics( + label: 'ABC\nTab 3 of 3', + textDirection: TextDirection.ltr, + isFocusable: true, + isButton: true, hasTapAction: true, hasFocusAction: true, ), @@ -537,6 +564,7 @@ void main() { textDirection: TextDirection.ltr, isFocusable: true, isSelected: true, + isButton: true, hasTapAction: true, hasFocusAction: true, ), @@ -547,6 +575,7 @@ void main() { label: 'Alarm\nTab 2 of 2', textDirection: TextDirection.ltr, isFocusable: true, + isButton: true, hasTapAction: true, hasFocusAction: true, ), @@ -560,6 +589,7 @@ void main() { label: 'AC\nTab 1 of 2', textDirection: TextDirection.ltr, isFocusable: true, + isButton: true, hasTapAction: true, hasFocusAction: true, ), @@ -571,6 +601,7 @@ void main() { textDirection: TextDirection.ltr, isFocusable: true, isSelected: true, + isButton: true, hasTapAction: true, hasFocusAction: true, ),