Open In App

Flutter – Banner Widget

Banner widget comes built-in with flutter API. It is somewhat similar to the debug banner that we are used to seeing on the top-right corner on a flutter app in debug mode. It enables us to show a message or text on top of any other widget. Below we will see its implementation with the help of an example and all its properties.

Constructor of Banner class:

const Banner(
{Key key,
Widget child,
@required String message,
TextDirection textDirection,
@required BannerLocation location,
TextDirection layoutDirection,
Color color: _kColor,
TextStyle textStyle: _kTextStyle}
)

Properties of Banner Widget:

Example:






import 'package:flutter/material.dart';
 
//Material design library
void main() {
  runApp(
    //widget tree starts here
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('GeeksforGeeks'),
          backgroundColor: Colors.greenAccent[400],
          centerTitle: true,
        ), //AppBar
        body: Center(
          child: Container(
            margin: const EdgeInsets.all(10.0),
            child: ClipRect(
              /** Banner Widget **/
              child: Banner(
                message: "20% off !!",
                location: BannerLocation.bottomStart,
                color: Colors.red,
                child: Container(
                  color: Colors.green[100],
                  height: 300,
                  child: Padding(
                    padding: const EdgeInsets.fromLTRB(10, 20, 10, 20),
                    child: Column(
                      children: <Widget>[
                        Image.network(
                            'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190806131525/forkPython.jpg'), //Image.network
                        const SizedBox(height: 10),
                        const Text(
                          'GeeksforGeeks',
                          style: TextStyle(
                              color: Colors.green,
                              fontSize: 40,
                              fontWeight: FontWeight.bold), //TextStyle
                        ),
                        const SizedBox(
                          height: 5,
                        ), //SizedBox
                        const Text(
                          'Fork Python Course',
                          style: TextStyle(
                              color: Colors.green,
                              fontSize: 20,
                              fontWeight: FontWeight.bold), //TextStyle
                        ), //Text
                        const SizedBox(height: 20),
 
                        // RaiseButton is deprecated and should not be used. Use ElevatedButton instead.
 
                        // RaisedButton(
                        // color: Colors.greenAccent[400],
                        // onPressed: () {},
                        // child: const Text('Register'),
                        // ) //RaisedButton
                      ], //<Widget>[]
                    ), //Column
                  ), //Padding
                ), //Container
              ), //Banner
            ), //ClipRect
          ), //container
        ), //Center
      ), //Scaffold
    ), //MaterialApp
  );
}

Output:



Explanation: In this flutter application the Banner widget is child of ClipRect class which clips off the part that exceeds the box. The message in the banner is holding the text ‘20% off !!’, the color is red and the location is set to bottomStart. The rest of the part is held by the child property of the Banner widget.  The hierarchy from Banner widget to Column is Banner > Container > Padding > Column. Inside the Column widget we have a list of widgets taken as the object the children property. The first element in the column is an image from internet then we have two Text widgets and a RaisedButton all separated by a SizedBox widget. 


Article Tags :