From 4d501ab0ff664d7bdee4162c36913ff713ee8084 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Mon, 3 Apr 2023 19:38:14 +0300 Subject: [PATCH] Fix some `MaterialBanner` properties aren't respected when presented by `ScaffoldMessenger` (#124024) Fix some `MaterialBanner` properties aren't respected when presented by `ScaffoldMessenger` --- packages/flutter/lib/src/material/banner.dart | 3 + .../flutter/test/material/banner_test.dart | 101 +++++++----------- 2 files changed, 42 insertions(+), 62 deletions(-) diff --git a/packages/flutter/lib/src/material/banner.dart b/packages/flutter/lib/src/material/banner.dart index 515fee3e91..62f1ceefc6 100644 --- a/packages/flutter/lib/src/material/banner.dart +++ b/packages/flutter/lib/src/material/banner.dart @@ -245,6 +245,9 @@ class MaterialBanner extends StatefulWidget { elevation: elevation, leading: leading, backgroundColor: backgroundColor, + surfaceTintColor: surfaceTintColor, + shadowColor: shadowColor, + dividerColor: dividerColor, padding: padding, margin: margin, leadingPadding: leadingPadding, diff --git a/packages/flutter/test/material/banner_test.dart b/packages/flutter/test/material/banner_test.dart index 0f55080125..e1a8843c8b 100644 --- a/packages/flutter/test/material/banner_test.dart +++ b/packages/flutter/test/material/banner_test.dart @@ -7,71 +7,21 @@ import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { - testWidgets('Custom background color respected', (WidgetTester tester) async { - const Color color = Colors.pink; - await tester.pumpWidget( - MaterialApp( - home: MaterialBanner( - backgroundColor: color, - content: const Text('I am a banner'), - actions: [ - TextButton( - child: const Text('Action'), - onPressed: () { }, - ), - ], - ), - ), - ); - - final Material material = _getMaterialFromBanner(tester); - expect(material.color, color); - }); - - testWidgets('Custom background color respected when presented by ScaffoldMessenger', (WidgetTester tester) async { - const Color color = Colors.pink; - const String contentText = 'Content'; - const Key tapTarget = Key('tap-target'); - await tester.pumpWidget(MaterialApp( - home: Scaffold( - body: Builder( - builder: (BuildContext context) { - return GestureDetector( - key: tapTarget, - onTap: () { - ScaffoldMessenger.of(context).showMaterialBanner(MaterialBanner( - content: const Text(contentText), - backgroundColor: color, - actions: [ - TextButton( - child: const Text('DISMISS'), - onPressed: () => ScaffoldMessenger.of(context).hideCurrentMaterialBanner(), - ), - ], - )); - }, - behavior: HitTestBehavior.opaque, - child: const SizedBox( - height: 100.0, - width: 100.0, - ), - ); - }, - ), - ), - )); - await tester.tap(find.byKey(tapTarget)); - await tester.pumpAndSettle(); - - expect(_getMaterialFromText(tester, contentText).color, color); - }); - - testWidgets('Custom content TextStyle respected', (WidgetTester tester) async { + testWidgets('MaterialBanner properties are respected', (WidgetTester tester) async { const String contentText = 'Content'; + const Color backgroundColor = Colors.pink; + const Color surfaceTintColor = Colors.green; + const Color shadowColor = Colors.blue; + const Color dividerColor = Colors.yellow; const TextStyle contentTextStyle = TextStyle(color: Colors.pink); + await tester.pumpWidget( MaterialApp( home: MaterialBanner( + backgroundColor: backgroundColor, + surfaceTintColor: surfaceTintColor, + shadowColor: shadowColor, + dividerColor: dividerColor, contentTextStyle: contentTextStyle, content: const Text(contentText), actions: [ @@ -84,14 +34,28 @@ void main() { ), ); + final Material material = _getMaterialFromBanner(tester); + expect(material.elevation, 0.0); + expect(material.color, backgroundColor); + expect(material.surfaceTintColor, surfaceTintColor); + expect(material.shadowColor, shadowColor); + final RenderParagraph content = _getTextRenderObjectFromDialog(tester, contentText); expect(content.text.style, contentTextStyle); + + final Divider divider = tester.widget(find.byType(Divider)); + expect(divider.color, dividerColor); }); - testWidgets('Custom content TextStyle respected when presented by ScaffoldMessenger', (WidgetTester tester) async { - const TextStyle contentTextStyle = TextStyle(color: Colors.pink); + testWidgets('MaterialBanner properties are respected when presented by ScaffoldMessenger', (WidgetTester tester) async { const String contentText = 'Content'; const Key tapTarget = Key('tap-target'); + const Color backgroundColor = Colors.pink; + const Color surfaceTintColor = Colors.green; + const Color shadowColor = Colors.blue; + const Color dividerColor = Colors.yellow; + const TextStyle contentTextStyle = TextStyle(color: Colors.pink); + await tester.pumpWidget(MaterialApp( home: Scaffold( body: Builder( @@ -101,6 +65,10 @@ void main() { onTap: () { ScaffoldMessenger.of(context).showMaterialBanner(MaterialBanner( content: const Text(contentText), + backgroundColor: backgroundColor, + surfaceTintColor: surfaceTintColor, + shadowColor: shadowColor, + dividerColor: dividerColor, contentTextStyle: contentTextStyle, actions: [ TextButton( @@ -123,8 +91,17 @@ void main() { await tester.tap(find.byKey(tapTarget)); await tester.pumpAndSettle(); + final Material material = _getMaterialFromText(tester, contentText); + expect(material.elevation, 0.0); + expect(material.color, backgroundColor); + expect(material.surfaceTintColor, surfaceTintColor); + expect(material.shadowColor, shadowColor); + final RenderParagraph content = _getTextRenderObjectFromDialog(tester, contentText); expect(content.text.style, contentTextStyle); + + final Divider divider = tester.widget(find.byType(Divider)); + expect(divider.color, dividerColor); }); testWidgets('Actions laid out below content if more than one action', (WidgetTester tester) async {