7.1 C
New York
Monday, December 4, 2023

Working with MaterialBanner in Flutter

In this article, we’ll explore the fundamentals of the MaterialBanner widget in Flutter then walk through a complete example of implementing it in practice. Without any further ado, let’s get started.


MaterialBanner is used to display a brief message ((such as promotions, security warnings, etc.) and can be dismissed by the user. It includes the following mandatory components:Advertisements

  • content: A widget containing the content of the MaterialBanner
  • actions: A list of widgets, usual buttons

A Material Banner is not an overlay or a modal like a Snack Bar or a Dialog. In simple words, it is on the same layer as its parent or siblings.

In general, a MaterialBanner can be shown programmatically by using ScaffoldMessenger like so:

        content: /* the content you want to show in the banner */
        actions: /* a list of widgets here */

The banner will appear right below the app bar and take some space as well as push other content in the body section of your app down.

AdvertisementsTo close a MaterialBanner, you can call:


A MaterialBanner can also be used statically like regular widgets:

appBar: /* Your app bar */
body: Column(
  MaterialBanner(/* ... */),
  // other things
  /* ... */

However, this usage is quite rare.


App Preview


The sample app we’re going to build contains an elevated button in the center of the screen. When this button gets pressed, a material banner will show up. You can close the banner by using its Dismiss button.

If you have a closer look, you’ll notice that the elevated button goes down a little when the banner shows up and returns to the beginning place when the banner disappears.

The Code

The complete code with explanations:

// main.dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: 'KindaCode.com',
        theme: ThemeData(
          primarySwatch: Colors.indigo,
        home: const HomePage());

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  _HomePageState createState() => _HomePageState();

class _HomePageState extends State<HomePage> {
  void _showMaterialBanner() {
        content: const Text('Have a nice day'),
        leading: const Icon(Icons.face),
        contentTextStyle: const TextStyle(
            fontSize: 18, color: Colors.red, fontStyle: FontStyle.italic),
        actions: [
              onPressed: () {
                // dismiss the material banner
              child: const Text('Dismiss'))

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('KindaCode.com')),
      body: Center(
        // this button is used to show the material banner
        child: ElevatedButton(
          onPressed: _showMaterialBanner,
          child: const Text('Show Material Banner'),


That’s it. Congrats if you made it through the entire tutorial. You’ve just successfully shown and hidden a material banner. From now on, you have an extra tool to use in your app as needed. If you’d like to explore more new and exciting things about modern Flutter development, take a look at the following articles:

You can also check out our Flutter category page or Dart category page for the latest tutorials and examples.


Related Articles

Latest Articles