Fix Material 3 Dialog default background color (#151400)

fixes [[Material 3] Wrong `Dialog` background color from ColorScheme](https://github.com/flutter/flutter/issues/148849
)

### Code sample

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

```dart
import 'package:flutter/material.dart';

/// Flutter code sample for [showDialog].

void main() => runApp(const ShowDialogExampleApp());

class ShowDialogExampleApp extends StatelessWidget {
  const ShowDialogExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const DialogExample(),
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: Colors.green,
          surfaceContainerHigh: Colors.amber,
        ),
      ),
    );
  }
}

class DialogExample extends StatelessWidget {
  const DialogExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('showDialog Sample')),
      body: Center(
        child: OutlinedButton(
          onPressed: () => _dialogBuilder(context),
          child: const Text('Open Dialog'),
        ),
      ),
    );
  }

  Future<void> _dialogBuilder(BuildContext context) {
    return showDialog<void>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Basic dialog title'),
          content: const Text(
            'A dialog is a type of modal window that\n'
            'appears in front of app content to\n'
            'provide critical information, or prompt\n'
            'for a decision to be made.',
          ),
          actions: <Widget>[
            TextButton(
              style: TextButton.styleFrom(
                textStyle: Theme.of(context).textTheme.labelLarge,
              ),
              child: const Text('Disable'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              style: TextButton.styleFrom(
                textStyle: Theme.of(context).textTheme.labelLarge,
              ),
              child: const Text('Enable'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}
```

</details>

### Before

<img width="674" alt="Screenshot 2024-07-08 at 14 26 39" src="https://github.com/flutter/flutter/assets/48603081/a95160f5-947e-4a6e-a3a5-82c94980c744">

### After

<img width="674" alt="Screenshot 2024-07-08 at 14 26 28" src="https://github.com/flutter/flutter/assets/48603081/fa912519-2a79-4fd5-a695-6e18542f0005">
This commit is contained in:
Taha Tesser
2024-07-09 22:55:47 +03:00
committed by GitHub
parent 5103d75743
commit 850d76db74
2 changed files with 8 additions and 4 deletions

View File

@@ -86,6 +86,9 @@ class Dialog extends StatelessWidget {
/// This sets the [Material.color] on this [Dialog]'s [Material].
///
/// If `null`, [ColorScheme.surfaceContainerHigh] is used in Material 3.
/// Otherwise, defaults to [ThemeData.dialogBackgroundColor].
///
/// If [Dialog.fullscreen] is used, defaults to [ColorScheme.surface].
/// {@endtemplate}
final Color? backgroundColor;
@@ -239,7 +242,7 @@ class Dialog extends StatelessWidget {
child: ConstrainedBox(
constraints: const BoxConstraints(minWidth: 280.0),
child: Material(
color: backgroundColor ?? dialogTheme.backgroundColor ?? Theme.of(context).dialogBackgroundColor,
color: backgroundColor ?? dialogTheme.backgroundColor ?? defaults.backgroundColor,
elevation: elevation ?? dialogTheme.elevation ?? defaults.elevation!,
shadowColor: shadowColor ?? dialogTheme.shadowColor ?? defaults.shadowColor,
surfaceTintColor: surfaceTintColor ?? dialogTheme.surfaceTintColor ?? defaults.surfaceTintColor,

View File

@@ -111,11 +111,12 @@ void main() {
expect(materialWidget.color, customColor);
});
testWidgets('Dialog background defaults to ColorScheme.surface', (WidgetTester tester) async {
testWidgets('Dialog background defaults to ColorScheme.surfaceContainerHigh', (WidgetTester tester) async {
final ThemeData theme = ThemeData(
colorScheme: ThemeData().colorScheme.copyWith(
surface: Colors.orange,
background: Colors.green,
surfaceContainerHigh: Colors.red,
)
);
const Dialog dialog = Dialog(
@@ -130,7 +131,7 @@ void main() {
await tester.pumpAndSettle();
final Material materialWidget = _getMaterialFromDialog(tester);
expect(materialWidget.color, theme.colorScheme.surface);
expect(materialWidget.color, theme.colorScheme.surfaceContainerHigh);
});
testWidgets('Material2 - Dialog Defaults', (WidgetTester tester) async {
@@ -167,7 +168,7 @@ void main() {
await tester.pumpAndSettle();
final Material material3Widget = _getMaterialFromDialog(tester);
expect(material3Widget.color, material3Theme.colorScheme.surface);
expect(material3Widget.color, material3Theme.colorScheme.surfaceContainerHigh);
expect(material3Widget.shape, _defaultM3DialogShape);
expect(material3Widget.elevation, 6.0);
});