From 60f4f9ffdd72b52a5f808372e06d6001dc50c71d Mon Sep 17 00:00:00 2001 From: Huy Date: Fri, 24 Oct 2025 10:48:25 +0700 Subject: [PATCH] Fix drawer Semantics for mismatched platforms (#177095) - Address a partial issue within https://github.com/flutter/flutter/issues/176566 - Fix https://github.com/flutter/flutter/issues/177005 - In this PR: - proposes using `defaultTargetPlatform` instead of platform from theme for Semantics label in Drawer widget - add some new tests ## 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. - [ ] 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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Signed-off-by: huycozy --- packages/flutter/lib/src/material/drawer.dart | 5 +- .../flutter/test/material/drawer_test.dart | 64 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/drawer.dart b/packages/flutter/lib/src/material/drawer.dart index 8a8b0dc7a1f..ef91e954264 100644 --- a/packages/flutter/lib/src/material/drawer.dart +++ b/packages/flutter/lib/src/material/drawer.dart @@ -14,6 +14,7 @@ /// @docImport 'scaffold.dart'; library; +import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart' show DragStartBehavior; import 'package:flutter/widgets.dart'; @@ -250,7 +251,7 @@ class Drawer extends StatelessWidget { assert(debugCheckHasMaterialLocalizations(context)); final DrawerThemeData drawerTheme = DrawerTheme.of(context); String? label = semanticLabel; - switch (Theme.of(context).platform) { + switch (defaultTargetPlatform) { case TargetPlatform.iOS: case TargetPlatform.macOS: break; @@ -685,7 +686,7 @@ class DrawerControllerState extends State with SingleTickerPro } } else { final bool platformHasBackButton; - switch (Theme.of(context).platform) { + switch (defaultTargetPlatform) { case TargetPlatform.android: platformHasBackButton = true; case TargetPlatform.iOS: diff --git a/packages/flutter/test/material/drawer_test.dart b/packages/flutter/test/material/drawer_test.dart index 81a559e399c..8d77883aa6d 100644 --- a/packages/flutter/test/material/drawer_test.dart +++ b/packages/flutter/test/material/drawer_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; @@ -969,6 +970,69 @@ void main() { }, ); + // Regression test for https://github.com/flutter/flutter/issues/177005 + testWidgets('Drawer semantics for mismatched platforms', (WidgetTester tester) async { + const DefaultMaterialLocalizations localizations = DefaultMaterialLocalizations(); + + Future pumpDrawerWithTheme(TargetPlatform themePlatform) async { + final GlobalKey scaffoldKey = GlobalKey(); + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(platform: themePlatform), + home: Scaffold( + key: scaffoldKey, + drawer: const Drawer(child: Text('Drawer')), + body: Container(), + ), + ), + ); + + scaffoldKey.currentState!.openDrawer(); + await tester.pumpAndSettle(); + + // Test label semantics. + final Finder drawerLabelFinder = find.bySemanticsLabel(localizations.drawerLabel); + if (defaultTargetPlatform == TargetPlatform.iOS || + defaultTargetPlatform == TargetPlatform.macOS) { + expect(drawerLabelFinder, findsNothing); // Apple platforms don't show drawer label. + } else { + expect(drawerLabelFinder, findsOneWidget); // Non-Apple platforms show drawer label. + } + + // Test barrier semantics. + final SemanticsTester semantics = SemanticsTester(tester); + final bool expectBarrierExcluded = defaultTargetPlatform == TargetPlatform.android; + + if (expectBarrierExcluded) { + expect( + semantics, + isNot( + includesNodeWith( + label: localizations.modalBarrierDismissLabel, + actions: [SemanticsAction.tap, SemanticsAction.focus], + ), + ), + ); + } else { + expect( + semantics, + includesNodeWith( + label: localizations.modalBarrierDismissLabel, + actions: [SemanticsAction.tap], + ), + ); + } + + semantics.dispose(); + } + + // Test with theme.platform = Android on different real platforms. + await pumpDrawerWithTheme(TargetPlatform.android); + + // Test with theme.platform = iOS on different real platforms. + await pumpDrawerWithTheme(TargetPlatform.iOS); + }, variant: TargetPlatformVariant.all()); + group('Material 2', () { // These tests are only relevant for Material 2. Once Material 2 // support is deprecated and the APIs are removed, these tests