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.

<!-- Links -->
[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 <huy@nevercode.io>
This commit is contained in:
Huy 2025-10-24 10:48:25 +07:00 committed by GitHub
parent 2b20cfe3f5
commit 60f4f9ffdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 2 deletions

View File

@ -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<DrawerController> with SingleTickerPro
}
} else {
final bool platformHasBackButton;
switch (Theme.of(context).platform) {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
platformHasBackButton = true;
case TargetPlatform.iOS:

View File

@ -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<void> pumpDrawerWithTheme(TargetPlatform themePlatform) async {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
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>[SemanticsAction.tap, SemanticsAction.focus],
),
),
);
} else {
expect(
semantics,
includesNodeWith(
label: localizations.modalBarrierDismissLabel,
actions: <SemanticsAction>[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