Unverified Commit c9751c92 authored by chunhtai's avatar chunhtai Committed by GitHub

Fixes dialog title announce twice (#90075)

parent 5b7d671f
...@@ -499,7 +499,9 @@ class AlertDialog extends StatelessWidget { ...@@ -499,7 +499,9 @@ class AlertDialog extends StatelessWidget {
child: DefaultTextStyle( child: DefaultTextStyle(
style: titleTextStyle ?? dialogTheme.titleTextStyle ?? theme.textTheme.headline6!, style: titleTextStyle ?? dialogTheme.titleTextStyle ?? theme.textTheme.headline6!,
child: Semantics( 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, container: true,
child: title, child: title,
), ),
...@@ -843,17 +845,15 @@ class SimpleDialog extends StatelessWidget { ...@@ -843,17 +845,15 @@ class SimpleDialog extends StatelessWidget {
final ThemeData theme = Theme.of(context); final ThemeData theme = Theme.of(context);
String? label = semanticLabel; String? label = semanticLabel;
if (title == null) { switch (theme.platform) {
switch (theme.platform) { case TargetPlatform.macOS:
case TargetPlatform.macOS: case TargetPlatform.iOS:
case TargetPlatform.iOS: break;
break; case TargetPlatform.android:
case TargetPlatform.android: case TargetPlatform.fuchsia:
case TargetPlatform.fuchsia: case TargetPlatform.linux:
case TargetPlatform.linux: case TargetPlatform.windows:
case TargetPlatform.windows: label ??= MaterialLocalizations.of(context).dialogLabel;
label = semanticLabel ?? MaterialLocalizations.of(context).dialogLabel;
}
} }
// The paddingScaleFactor is used to adjust the padding of Dialog // The paddingScaleFactor is used to adjust the padding of Dialog
...@@ -874,7 +874,9 @@ class SimpleDialog extends StatelessWidget { ...@@ -874,7 +874,9 @@ class SimpleDialog extends StatelessWidget {
child: DefaultTextStyle( child: DefaultTextStyle(
style: titleTextStyle ?? DialogTheme.of(context).titleTextStyle ?? theme.textTheme.headline6!, style: titleTextStyle ?? DialogTheme.of(context).titleTextStyle ?? theme.textTheme.headline6!,
child: Semantics( 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, container: true,
child: title, child: title,
), ),
......
...@@ -1319,54 +1319,6 @@ void main() { ...@@ -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<void>(
context: context,
builder: (BuildContext context) {
return const AlertDialog(
title: Text('Title'),
content: Text('Y'),
actions: <Widget>[],
);
},
);
},
),
);
},
),
),
),
);
expect(semantics, isNot(includesNodeWith(
label: 'Title',
flags: <SemanticsFlag>[SemanticsFlag.namesRoute],
)));
await tester.tap(find.text('X'));
await tester.pumpAndSettle();
expect(semantics, includesNodeWith(
label: 'Title',
flags: <SemanticsFlag>[SemanticsFlag.namesRoute],
));
semantics.dispose();
});
// Regression test for https://github.com/flutter/flutter/issues/78229. // Regression test for https://github.com/flutter/flutter/issues/78229.
testWidgets('AlertDialog has correct semantics for content in iOS', (WidgetTester tester) async { testWidgets('AlertDialog has correct semantics for content in iOS', (WidgetTester tester) async {
final SemanticsTester semantics = SemanticsTester(tester); final SemanticsTester semantics = SemanticsTester(tester);
...@@ -1400,7 +1352,6 @@ void main() { ...@@ -1400,7 +1352,6 @@ void main() {
children: <TestSemantics>[ children: <TestSemantics>[
TestSemantics( TestSemantics(
id: 5, id: 5,
flags: <SemanticsFlag>[SemanticsFlag.namesRoute],
label: 'title', label: 'title',
textDirection: TextDirection.ltr, textDirection: TextDirection.ltr,
), ),
...@@ -1569,7 +1520,6 @@ void main() { ...@@ -1569,7 +1520,6 @@ void main() {
// node 4. // node 4.
TestSemantics( TestSemantics(
id: 5, id: 5,
flags: <SemanticsFlag>[SemanticsFlag.namesRoute],
label: 'title', label: 'title',
textDirection: TextDirection.ltr, textDirection: TextDirection.ltr,
), ),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment