Unverified Commit efe0c5eb authored by Aayan's avatar Aayan Committed by GitHub

Integrate MaterialBanner with the ScaffoldMessenger API (#81706)

parent 734df6f5
......@@ -64,7 +64,47 @@ void main() {
),
));
final Container container = _getContainerFromBanner(tester);
final Container container = _getContainerFromText(tester, contentText);
final RenderParagraph content = _getTextRenderObjectFromDialog(tester, contentText);
expect(container.color, const Color(0xffffffff));
// Default value for ThemeData.typography is Typography.material2014()
expect(content.text.style, Typography.material2014().englishLike.bodyText2!.merge(Typography.material2014().black.bodyText2));
});
testWidgets('Passing no MaterialBannerThemeData returns defaults when presented by ScaffoldMessenger', (WidgetTester tester) async {
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),
actions: <Widget>[
TextButton(
child: const Text('Action'),
onPressed: () { },
),
],
));
},
behavior: HitTestBehavior.opaque,
child: const SizedBox(
height: 100.0,
width: 100.0,
),
);
},
),
),
));
await tester.tap(find.byKey(tapTarget));
await tester.pumpAndSettle();
final Container container = _getContainerFromText(tester, contentText);
final RenderParagraph content = _getTextRenderObjectFromDialog(tester, contentText);
expect(container.color, const Color(0xffffffff));
// Default value for ThemeData.typography is Typography.material2014()
......@@ -90,7 +130,57 @@ void main() {
),
));
final Container container = _getContainerFromBanner(tester);
final Container container = _getContainerFromText(tester, contentText);
final RenderParagraph content = _getTextRenderObjectFromDialog(tester, contentText);
expect(container.color, bannerTheme.backgroundColor);
expect(content.text.style, bannerTheme.contentTextStyle);
final Offset contentTopLeft = tester.getTopLeft(_textFinder(contentText));
final Offset containerTopLeft = tester.getTopLeft(_containerFinder());
final Offset leadingTopLeft = tester.getTopLeft(find.byIcon(Icons.ac_unit));
expect(contentTopLeft.dy - containerTopLeft.dy, 24);
expect(contentTopLeft.dx - containerTopLeft.dx, 41);
expect(leadingTopLeft.dy - containerTopLeft.dy, 19);
expect(leadingTopLeft.dx - containerTopLeft.dx, 11);
});
testWidgets('MaterialBanner uses values from MaterialBannerThemeData when presented by ScaffoldMessenger', (WidgetTester tester) async {
final MaterialBannerThemeData bannerTheme = _bannerTheme();
const String contentText = 'Content';
const Key tapTarget = Key('tap-target');
await tester.pumpWidget(MaterialApp(
theme: ThemeData(bannerTheme: bannerTheme),
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return GestureDetector(
key: tapTarget,
onTap: () {
ScaffoldMessenger.of(context).showMaterialBanner(MaterialBanner(
leading: const Icon(Icons.ac_unit),
content: const Text(contentText),
actions: <Widget>[
TextButton(
child: const Text('Action'),
onPressed: () { },
),
],
));
},
behavior: HitTestBehavior.opaque,
child: const SizedBox(
height: 100.0,
width: 100.0,
),
);
},
),
),
));
await tester.tap(find.byKey(tapTarget));
await tester.pumpAndSettle();
final Container container = _getContainerFromText(tester, contentText);
final RenderParagraph content = _getTextRenderObjectFromDialog(tester, contentText);
expect(container.color, bannerTheme.backgroundColor);
expect(content.text.style, bannerTheme.contentTextStyle);
......@@ -129,7 +219,63 @@ void main() {
),
));
final Container container = _getContainerFromBanner(tester);
final Container container = _getContainerFromText(tester, contentText);
final RenderParagraph content = _getTextRenderObjectFromDialog(tester, contentText);
expect(container.color, backgroundColor);
expect(content.text.style, textStyle);
final Offset contentTopLeft = tester.getTopLeft(_textFinder(contentText));
final Offset containerTopLeft = tester.getTopLeft(_containerFinder());
final Offset leadingTopLeft = tester.getTopLeft(find.byIcon(Icons.ac_unit));
expect(contentTopLeft.dy - containerTopLeft.dy, 29);
expect(contentTopLeft.dx - containerTopLeft.dx, 58);
expect(leadingTopLeft.dy - containerTopLeft.dy, 24);
expect(leadingTopLeft.dx - containerTopLeft.dx, 22);
});
testWidgets('MaterialBanner widget properties take priority over theme when presented by ScaffoldMessenger', (WidgetTester tester) async {
const Color backgroundColor = Colors.purple;
const TextStyle textStyle = TextStyle(color: Colors.green);
final MaterialBannerThemeData bannerTheme = _bannerTheme();
const String contentText = 'Content';
const Key tapTarget = Key('tap-target');
await tester.pumpWidget(MaterialApp(
theme: ThemeData(bannerTheme: bannerTheme),
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return GestureDetector(
key: tapTarget,
onTap: () {
ScaffoldMessenger.of(context).showMaterialBanner(MaterialBanner(
backgroundColor: backgroundColor,
leading: const Icon(Icons.ac_unit),
contentTextStyle: textStyle,
content: const Text(contentText),
padding: const EdgeInsets.all(10),
leadingPadding: const EdgeInsets.all(12),
actions: <Widget>[
TextButton(
child: const Text('Action'),
onPressed: () { },
),
],
));
},
behavior: HitTestBehavior.opaque,
child: const SizedBox(
height: 100.0,
width: 100.0,
),
);
},
),
),
));
await tester.tap(find.byKey(tapTarget));
await tester.pumpAndSettle();
final Container container = _getContainerFromText(tester, contentText);
final RenderParagraph content = _getTextRenderObjectFromDialog(tester, contentText);
expect(container.color, backgroundColor);
expect(content.text.style, textStyle);
......@@ -145,11 +291,12 @@ void main() {
testWidgets('MaterialBanner uses color scheme when necessary', (WidgetTester tester) async {
final ColorScheme colorScheme = const ColorScheme.light().copyWith(surface: Colors.purple);
const String contentText = 'Content';
await tester.pumpWidget(MaterialApp(
theme: ThemeData(colorScheme: colorScheme),
home: Scaffold(
body: MaterialBanner(
content: const Text('Content'),
content: const Text(contentText),
actions: <Widget>[
TextButton(
child: const Text('Action'),
......@@ -160,7 +307,46 @@ void main() {
),
));
final Container container = _getContainerFromBanner(tester);
final Container container = _getContainerFromText(tester, contentText);
expect(container.color, colorScheme.surface);
});
testWidgets('MaterialBanner uses color scheme when necessary when presented by ScaffoldMessenger', (WidgetTester tester) async {
final ColorScheme colorScheme = const ColorScheme.light().copyWith(surface: Colors.purple);
const String contentText = 'Content';
const Key tapTarget = Key('tap-target');
await tester.pumpWidget(MaterialApp(
theme: ThemeData(colorScheme: colorScheme),
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return GestureDetector(
key: tapTarget,
onTap: () {
ScaffoldMessenger.of(context).showMaterialBanner(MaterialBanner(
content: const Text(contentText),
actions: <Widget>[
TextButton(
child: const Text('Action'),
onPressed: () { },
),
],
));
},
behavior: HitTestBehavior.opaque,
child: const SizedBox(
height: 100.0,
width: 100.0,
),
);
},
),
),
));
await tester.tap(find.byKey(tapTarget));
await tester.pumpAndSettle();
final Container container = _getContainerFromText(tester, contentText);
expect(container.color, colorScheme.surface);
});
}
......@@ -174,8 +360,8 @@ MaterialBannerThemeData _bannerTheme() {
);
}
Container _getContainerFromBanner(WidgetTester tester) {
return tester.widget<Container>(_containerFinder());
Container _getContainerFromText(WidgetTester tester, String text) {
return tester.widget<Container>(find.widgetWithText(Container, text).first);
}
Finder _containerFinder() {
......
......@@ -2282,6 +2282,63 @@ void main() {
await expectLater(find.byType(MaterialApp), matchesGoldenFile('snack_bar.goldenTest.workWithBottomSheet.png'));
});
testWidgets('ScaffoldMessenger does not duplicate a SnackBar when presenting a MaterialBanner.', (WidgetTester tester) async {
const Key materialBannerTapTarget = Key('materialbanner-tap-target');
const Key snackBarTapTarget = Key('snackbar-tap-target');
const String snackBarText = 'SnackBar';
const String materialBannerText = 'MaterialBanner';
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return Column(
children: <Widget>[
GestureDetector(
key: snackBarTapTarget,
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text(snackBarText),
));
},
behavior: HitTestBehavior.opaque,
child: const SizedBox(
height: 100.0,
width: 100.0,
),
),
GestureDetector(
key: materialBannerTapTarget,
onTap: () {
ScaffoldMessenger.of(context).showMaterialBanner(MaterialBanner(
content: const Text(materialBannerText),
actions: <Widget>[
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(snackBarTapTarget));
await tester.tap(find.byKey(materialBannerTapTarget));
await tester.pumpAndSettle();
expect(find.text(snackBarText), findsOneWidget);
expect(find.text(materialBannerText), findsOneWidget);
});
testWidgets('ScaffoldMessenger presents SnackBars to only the root Scaffold when Scaffolds are nested.', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment