Skip to content

Commit 850d76d

Browse files
authored
Fix Material 3 Dialog default background color (flutter#151400)
fixes [[Material 3] Wrong `Dialog` background color from ColorScheme](flutter#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">
1 parent 5103d75 commit 850d76d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

packages/flutter/lib/src/material/dialog.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class Dialog extends StatelessWidget {
8686
/// This sets the [Material.color] on this [Dialog]'s [Material].
8787
///
8888
/// If `null`, [ColorScheme.surfaceContainerHigh] is used in Material 3.
89+
/// Otherwise, defaults to [ThemeData.dialogBackgroundColor].
90+
///
91+
/// If [Dialog.fullscreen] is used, defaults to [ColorScheme.surface].
8992
/// {@endtemplate}
9093
final Color? backgroundColor;
9194

@@ -239,7 +242,7 @@ class Dialog extends StatelessWidget {
239242
child: ConstrainedBox(
240243
constraints: const BoxConstraints(minWidth: 280.0),
241244
child: Material(
242-
color: backgroundColor ?? dialogTheme.backgroundColor ?? Theme.of(context).dialogBackgroundColor,
245+
color: backgroundColor ?? dialogTheme.backgroundColor ?? defaults.backgroundColor,
243246
elevation: elevation ?? dialogTheme.elevation ?? defaults.elevation!,
244247
shadowColor: shadowColor ?? dialogTheme.shadowColor ?? defaults.shadowColor,
245248
surfaceTintColor: surfaceTintColor ?? dialogTheme.surfaceTintColor ?? defaults.surfaceTintColor,

packages/flutter/test/material/dialog_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ void main() {
111111
expect(materialWidget.color, customColor);
112112
});
113113

114-
testWidgets('Dialog background defaults to ColorScheme.surface', (WidgetTester tester) async {
114+
testWidgets('Dialog background defaults to ColorScheme.surfaceContainerHigh', (WidgetTester tester) async {
115115
final ThemeData theme = ThemeData(
116116
colorScheme: ThemeData().colorScheme.copyWith(
117117
surface: Colors.orange,
118118
background: Colors.green,
119+
surfaceContainerHigh: Colors.red,
119120
)
120121
);
121122
const Dialog dialog = Dialog(
@@ -130,7 +131,7 @@ void main() {
130131
await tester.pumpAndSettle();
131132

132133
final Material materialWidget = _getMaterialFromDialog(tester);
133-
expect(materialWidget.color, theme.colorScheme.surface);
134+
expect(materialWidget.color, theme.colorScheme.surfaceContainerHigh);
134135
});
135136

136137
testWidgets('Material2 - Dialog Defaults', (WidgetTester tester) async {
@@ -167,7 +168,7 @@ void main() {
167168
await tester.pumpAndSettle();
168169

169170
final Material material3Widget = _getMaterialFromDialog(tester);
170-
expect(material3Widget.color, material3Theme.colorScheme.surface);
171+
expect(material3Widget.color, material3Theme.colorScheme.surfaceContainerHigh);
171172
expect(material3Widget.shape, _defaultM3DialogShape);
172173
expect(material3Widget.elevation, 6.0);
173174
});

0 commit comments

Comments
 (0)