YouTube Description:

An example of MaterialBanner, new to Flutter in version 2.5. Similar to the SnackBar, the MaterialBanner shows a message but instead of at the bottom like the SnackBar, it's at the top. Learn how to display and hide MaterialBanners.

New in Flutter 2.5 we now have MaterialBanner. MaterialBanner is somewhat similar to the SnackBar, but it shows at the top instead of at the bottom.

 

We will start by building our tutorial boilerplate. It's just going to be a call to a stateless widget that has a MaterialApp, Scaffold and AppBar for now.

import 'package:flutter/material.dart'; void main() => runApp(const BannerApp()); class BannerApp extends StatelessWidget { const BannerApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('MaterialBanner Example')), ), ); } }

 

Now we will add a simple ElevatedButton in the body so we can activate the MaterialBanner. We'll go ahead and center it as well.

appBar: AppBar(title: const Text('MaterialBanner Example')), body: Center( child: ElevatedButton( child: const Text('Show MaterialBanner'), onPressed: () {} ), // ElevatedButton ), // Center ), //Scaffold

That's just going to show a simple button in the middle of the page.

 

Now we will show a MaterialBanner when the button is pressed. In the onPressed for the ElevatedButton we are going to add this:

child: const Text('Show MaterialBanner'), onPressed: () { ScaffoldMessenger.of(context).showMaterialBanner( MaterialBanner( content: const Text('This is a Material Banner'), actions: <Widget>[ TextButton( child: const Text('DISMISS'), onPressed: () {}, ), // TextButton ], // <Widget>[] ), // MaterialBanner ); },

This is the simplest we can make the Material Banner. So we will run this, press our button and you should see the MaterialBanner at the top.

 

Before we do anything cosmetic to it, let's go ahead and add the code to close the MaterialBanner. Inside the TextButton's onPressed we are going to add this:

TextButton( child: const Text('DISMISS'), onPressed: () { ScaffoldMessenger.of(context) .hideCurrentMaterialBanner(); }, ), // TextButton

And after a restart you should be able to dismiss your banner.

But if you push the button twice, you'll notice you actually have two banners available...

So we can fix that pretty easily by just removing any current banner showing before we show a banner like this:

ScaffoldMessenger.of(context) ..removeCurrentMaterialBanner() ..showMaterialBanner( MaterialBanner(

You'll notice that there are two periods before removeCurrentMaterialBanner and before showMaterialBanner. Adding that though will fix our problem with having duplicate banners:

 

You can also close it from another button, you don't have to just use the dismiss button. We will wrap the ElevatedButton in a column:

body: Center( child: Column( children: [ ElevatedButton(

You can do this a few ways, you can just cut the ElevatedButton, add the Column and paste the ElevatedButton back in. Or you can right-mouse click, click on refactor and then click on click "wrap with column".

And then we will add a second ElevatedButton for hiding the MaterialBanner below that button:

), // ElevatedButton ElevatedButton( child: const Text('Hide MaterialBanner'), onPressed: () { }, ), // ElevatedButton

Now just simply put the same code in the onPressed that you have in the dismiss button and it should work correctly:

child: const Text('Hide MaterialBanner'), onPressed: () { ScaffoldMessenger.of(context) .hideCurrentMaterialBanner(); },

 

The problem with the way we currently have it though is that the MaterialApp is built into the ElevatedButton. This could pose problems if you're wanting to call it from other places in your app. We can handle that a few ways.

The first way is moving it outside of the ElevatedButton and assigning it to a variable. So we will cut everything highlighted in red:

..showMaterialBanner( MaterialBanner( content: const Text('This is a Material Banner'), actions: <Widget>[ TextButton( child: const Text('DISMISS'), onPressed: () { ScaffoldMessenger.of(context) .hideCurrentMaterialBanner(); }, ), // TextButton ], // <Widget>[] ), // MaterialBanner );

And then we are going to paste it below the build Widget and set it to a variable:

Widget build(BuildContext context) { var materialBanner = MaterialBanner( content: const Text('This is a Material Banner'), actions: <Widget>[ TextButton( child: const Text('DISMISS'), onPressed: () { ScaffoldMessenger.of(context).hideCurrentMaterialBanner(); }, ), // TextButton ], // <Widget>[] ); // MaterialBanner return Scaffold(

Now inside the ElevatedButton we just call the variable:

child: const Text('Show MaterialBanner'), onPressed: () { ScaffoldMessenger.of(context) ..removeCurrentMaterialBanner() ..showMaterialBanner(materialBanner); },

And you'll see that it still works.

 

But you can actually separate it all together. So we're going to cut everything red again:

Widget build(BuildContext context) { var materialBanner = MaterialBanner( content: const Text('This is a Material Banner'), actions: <Widget>[ TextButton( child: const Text('DISMISS'), onPressed: () { ScaffoldMessenger.of(context).hideCurrentMaterialBanner(); }, ), // TextButton ], //<Widget>[] ); // MaterialBanner return Scaffold(

And then at the bottom of the page we'll add this:

MaterialBanner materialBanner(BuildContext context) { return MaterialBanner( content: const Text('This is a Material Banner'), actions: <Widget>[ TextButton( child: const Text('DISMISS'), onPressed: () { ScaffoldMessenger.of(context).hideCurrentMaterialBanner(); }, ), ], ); }

You'll notice we removed the variable instantiation and are just returning it instead.

Now we just need to add context to the materialBanner call:

child: const Text('Show MaterialBanner'), onPressed: () { ScaffoldMessenger.of(context) ..removeCurrentMaterialBanner() ..showMaterialBanner(materialBanner(context)); },

And after a restart you'll see everything still works as expected.

 

So now let's get into some of the properties we can change with MaterialBanner. We can change the background color:

return MaterialBanner( backgroundColor: Colors.yellowAccent, content: const Text('This is a Material Banner'),

Add a leading icon:

backgroundColor: Colors.yellowAccent, leading: const Icon(Icons.person), content: const Text('This is a Material Banner'),

Add a second button:

actions: <Widget>[ TextButton( child: const Text('MORE INFO'), onPressed: () {}, ), TextButton( child: const Text('DISMISS'),

And a few other things you can see on their official page.

 

One last thing you might want to know is how to pass a message through to it. We will first update the variables the function receives:

MaterialBanner materialBanner(BuildContext context, String message) {

And then set that variable in content like so (you'll notice "const" has been removed):

leading: const Icon(Icons.person), content: Text(message), actions: <Widget>[

And then pass the message down on the call to show it:

..showMaterialBanner( materialBanner(context, 'Passing banner message'));

It's pretty simple.

 

Click here for the full source.

 

More Videos