Skip to content

Commit

Permalink
Enable explicitChildNodes for the AlertDialog content (#149130)
Browse files Browse the repository at this point in the history
fixes [AlertDialog content semantics merged](flutter/flutter#147574)

### Code sample

<details>
<summary>expand to view the code sample</summary> 

```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: <Widget>[
                const Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('Some text'),
                    Text('More text'),
                  ],
                ),
                Builder(builder: (BuildContext context) {
                  return ElevatedButton(
                    onPressed: () {
                      showDialog<void>(
                        context: context,
                        builder: (BuildContext context) {
                          return AlertDialog(
                            title: const Text('Dialog Title'),
                            content: const Column(
                              children: <Widget>[
                                Text('Some text'),
                                Text('More text'),
                              ],
                            ),
                            actions: <Widget>[
                              TextButton(
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },
                                child: const Text('Close'),
                              ),
                            ],
                          );
                        },
                      );
                    },
                    child: const Text('Open Dialog'),
                  );
                }),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
```

</details>

### 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)
  • Loading branch information
TahaTesser authored May 30, 2024
1 parent d71f4eb commit 6a0e0bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/flutter/lib/src/material/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ class AlertDialog extends StatelessWidget {
style: contentTextStyle ?? dialogTheme.contentTextStyle ?? defaults.contentTextStyle!,
child: Semantics(
container: true,
explicitChildNodes: true,
child: content,
),
),
Expand Down
23 changes: 19 additions & 4 deletions packages/flutter/test/material/dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,12 @@ void main() {
theme: ThemeData(platform: TargetPlatform.iOS),
home: const AlertDialog(
title: Text('title'),
content: Text('content'),
content: Column(
children: <Widget>[
Text('some content'),
Text('more content'),
],
),
actions: <Widget>[ TextButton(onPressed: null, child: Text('action')) ],
),
),
Expand Down Expand Up @@ -1731,11 +1736,21 @@ void main() {
// node 4.
TestSemantics(
id: 6,
label: 'content',
textDirection: TextDirection.ltr,
children: <TestSemantics>[
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>[
SemanticsFlag.isButton,
SemanticsFlag.hasEnabledState,
Expand Down

0 comments on commit 6a0e0bf

Please sign in to comment.