From c9751c9292b2b83ad46ed6976298e82a458200c1 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Mon, 20 Sep 2021 12:48:05 -0700 Subject: [PATCH] Fixes dialog title announce twice (#90075) --- packages/flutter/lib/src/material/dialog.dart | 28 ++++++----- .../flutter/test/material/dialog_test.dart | 50 ------------------- 2 files changed, 15 insertions(+), 63 deletions(-) diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index c95f60a586..8d0f3bbc9c 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -499,7 +499,9 @@ class AlertDialog extends StatelessWidget { child: DefaultTextStyle( style: titleTextStyle ?? dialogTheme.titleTextStyle ?? theme.textTheme.headline6!, child: Semantics( - namesRoute: label == null, + // For iOS platform, the focus always lands on the title. + // Set nameRoute to false to avoid title being announce twice. + namesRoute: label == null && theme.platform != TargetPlatform.iOS, container: true, child: title, ), @@ -843,17 +845,15 @@ class SimpleDialog extends StatelessWidget { final ThemeData theme = Theme.of(context); String? label = semanticLabel; - if (title == null) { - switch (theme.platform) { - case TargetPlatform.macOS: - case TargetPlatform.iOS: - break; - case TargetPlatform.android: - case TargetPlatform.fuchsia: - case TargetPlatform.linux: - case TargetPlatform.windows: - label = semanticLabel ?? MaterialLocalizations.of(context).dialogLabel; - } + switch (theme.platform) { + case TargetPlatform.macOS: + case TargetPlatform.iOS: + break; + case TargetPlatform.android: + case TargetPlatform.fuchsia: + case TargetPlatform.linux: + case TargetPlatform.windows: + label ??= MaterialLocalizations.of(context).dialogLabel; } // The paddingScaleFactor is used to adjust the padding of Dialog @@ -874,7 +874,9 @@ class SimpleDialog extends StatelessWidget { child: DefaultTextStyle( style: titleTextStyle ?? DialogTheme.of(context).titleTextStyle ?? theme.textTheme.headline6!, child: Semantics( - namesRoute: label == null, + // For iOS platform, the focus always lands on the title. + // Set nameRoute to false to avoid title being announce twice. + namesRoute: label == null && theme.platform != TargetPlatform.iOS, container: true, child: title, ), diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index 411c3c95a3..cd2f4f5271 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -1319,54 +1319,6 @@ void main() { ); }); - testWidgets('AlertDialog widget contains route semantics from title for iOS', (WidgetTester tester) async { - final SemanticsTester semantics = SemanticsTester(tester); - - await tester.pumpWidget( - MaterialApp( - theme: ThemeData(platform: TargetPlatform.iOS), - home: Material( - child: Builder( - builder: (BuildContext context) { - return Center( - child: ElevatedButton( - child: const Text('X'), - onPressed: () { - showDialog( - context: context, - builder: (BuildContext context) { - return const AlertDialog( - title: Text('Title'), - content: Text('Y'), - actions: [], - ); - }, - ); - }, - ), - ); - }, - ), - ), - ), - ); - - expect(semantics, isNot(includesNodeWith( - label: 'Title', - flags: [SemanticsFlag.namesRoute], - ))); - - await tester.tap(find.text('X')); - await tester.pumpAndSettle(); - - expect(semantics, includesNodeWith( - label: 'Title', - flags: [SemanticsFlag.namesRoute], - )); - - semantics.dispose(); - }); - // Regression test for https://github.com/flutter/flutter/issues/78229. testWidgets('AlertDialog has correct semantics for content in iOS', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester); @@ -1400,7 +1352,6 @@ void main() { children: [ TestSemantics( id: 5, - flags: [SemanticsFlag.namesRoute], label: 'title', textDirection: TextDirection.ltr, ), @@ -1569,7 +1520,6 @@ void main() { // node 4. TestSemantics( id: 5, - flags: [SemanticsFlag.namesRoute], label: 'title', textDirection: TextDirection.ltr, ),