From 850d76db7401060ae841a5f933beea837a105712 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Tue, 9 Jul 2024 22:55:47 +0300 Subject: [PATCH] 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
expand to view the code sample ```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 _dialogBuilder(BuildContext context) { return showDialog( 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: [ 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(); }, ), ], ); }, ); } } ```
### Before Screenshot 2024-07-08 at 14 26 39 ### After Screenshot 2024-07-08 at 14 26 28 --- packages/flutter/lib/src/material/dialog.dart | 5 ++++- packages/flutter/test/material/dialog_test.dart | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 09ac978cef..12fe0c1863 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -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, diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index 8d68dfdf5f..c5808e9e7c 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -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); });