From 6a0e0bfdc5f86c84b3499dfb0943418588f3b6c0 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Thu, 30 May 2024 12:59:22 +0300 Subject: [PATCH] Enable `explicitChildNodes` for the `AlertDialog` content (#149130) fixes [AlertDialog content semantics merged](https://github.com/flutter/flutter/issues/147574) ### Code sample
expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, // showSemanticsDebugger: true, home: Scaffold( body: SafeArea( child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ const Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Some text'), Text('More text'), ], ), Builder(builder: (BuildContext context) { return ElevatedButton( onPressed: () { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: const Text('Dialog Title'), content: const Column( children: [ Text('Some text'), Text('More text'), ], ), actions: [ TextButton( onPressed: () { Navigator.of(context).pop(); }, child: const Text('Close'), ), ], ); }, ); }, child: const Text('Open Dialog'), ); }), ], ), ), ), ), ); } } ```
### Before vs After ![Screenshot 2024-05-27 at 14 59 57](https://github.com/flutter/flutter/assets/48603081/3b231a7a-db71-4dbf-bd4a-dd44a537cae8) ![Screenshot 2024-05-27 at 14 56 04](https://github.com/flutter/flutter/assets/48603081/219c1de5-ad35-49b3-a80f-4f036184e22b) --- packages/flutter/lib/src/material/dialog.dart | 1 + .../flutter/test/material/dialog_test.dart | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index e8049dfdba..09ac978cef 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -812,6 +812,7 @@ class AlertDialog extends StatelessWidget { style: contentTextStyle ?? dialogTheme.contentTextStyle ?? defaults.contentTextStyle!, child: Semantics( container: true, + explicitChildNodes: true, child: content, ), ), diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index 9415e58aae..ebc1af01fc 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -1700,7 +1700,12 @@ void main() { theme: ThemeData(platform: TargetPlatform.iOS), home: const AlertDialog( title: Text('title'), - content: Text('content'), + content: Column( + children: [ + Text('some content'), + Text('more content'), + ], + ), actions: [ TextButton(onPressed: null, child: Text('action')) ], ), ), @@ -1731,11 +1736,21 @@ void main() { // node 4. TestSemantics( id: 6, - label: 'content', - textDirection: TextDirection.ltr, + children: [ + TestSemantics( + id: 7, + label: 'some content', + textDirection: TextDirection.ltr, + ), + TestSemantics( + id: 8, + label: 'more content', + textDirection: TextDirection.ltr, + ), + ], ), TestSemantics( - id: 7, + id: 9, flags: [ SemanticsFlag.isButton, SemanticsFlag.hasEnabledState,