Scaffold

Implements the basic material design visual layout structure.

This class provides APIs for showing drawers, snack bars, and bottom sheets.

To display a snackbar or a persistent bottom sheet, obtain the ScaffoldState for the current BuildContext via Scaffold.of and use the ScaffoldState.showSnackBar and ScaffoldState.showBottomSheet functions.

A Scaffold Widget provides a framework which implements the basic material design visual layout structure of the flutter app. It provides APIs for showing drawers, snack bars and bottom sheets. Have a look at its constructor and the properties it has. We are going to overview it’s parameters one by one.

//Constructor of Scaffoldconst Scaffold({
Key key,
this.appBar,
this.body,
this.floatingActionButton,
this.floatingActionButtonLocation,
this.floatingActionButtonAnimator,
this.persistentFooterButtons,
this.drawer,
this.endDrawer,
this.bottomNavigationBar,
this.bottomSheet,
this.backgroundColor,
this.resizeToAvoidBottomPadding = true,
this.primary = true,
})

Scaffold contains various functionality from giving an appbar, a floating button, a drawer, background color, bottom navigation bar, footer buttons,body. Now, let’s explore them individually:

appBar

An appBar is to display at the top of the scaffold it’s one of the primary content of Scaffold without which a scaffold widget is incomplete. It defines what has to be displayed at the top of the screen. appBar uses the AppBar widget properties through Scaffold.appBar. This AppBar widget itself has various properties like title, elevation,brightness etc to name a few.

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Flutter Scaffold'),
    ),

Here, the title property of AppBar uses Text widget to display the text on the app bar.

body

body is the other primary and minimum required property of the scaffold which signifies the area below the app bar and behind the floatingActionButton and drawer. Any widget in the body of scaffold is positioned at the top-left corner by default.

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Flutter Scaffold'),
    ),
    body: Center(
      child: Text("This is Homepage",
        style: TextStyle(
          color: Colors.black,
          fontSize: 40.0,
          fontStyle: FontStyle.italic,
        ),
      ),
    ),

Here, the body property of the Scaffold is used to display a text “This is HomePage”. The Centre widget is used to align the text at the centre of the body and a Text widget is used to give the text along with it’s style property which gives the text a color, fontsize and fontstyle. Don’t worry this blog isn’t about the Centre or Text or TextStyle widgets, they will be covered later.

floatingActionButton

A floatingActionButton is a button displayed floating above body in the bottom right corner. It is a circular icon button that hovers over content to promote a primary action in the application. floatingActionButton uses the FloatingActionButton widget properties through Scaffold.floatingActionButton.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Scaffold'),
      ),
      body: Center(
        child: Text(
          "This is Homepage",
          style: TextStyle(
            color: Colors.black,
            fontSize: 40.0,
            fontStyle: FontStyle.italic,
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
          elevation: 10.0,
          child: Icon(Icons.add),
          onPressed: (){
            print('I am Floating button');
          }
      ),
    );
  }

Here, the elevation property of the FloatingActionButton is used which gives shadow to the button and Icon widget is used to give an icon to the button. The onPressed property provides a callback that is called when the button is tapped, when you tap the button “ I am a Floating Button” gets printed on the console window

floatingActionButtonLocation

By default, the floatingActionButton is positioned at the bottom right corner of the screen so as the name says floatingActionButtonLocation defines the position of the floatingActionButton using the predefined contants like,

centerDocked

floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,

centerFloat

floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,

endDocked

floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,

endFloat

floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,

drawer

drawer is a panel displayed to the side of the body, often hidden on mobile devices. One usually has to swipe left to right or right to left to access the drawer.
It uses the Drawer widget properties which is a material design panel that slides in horizontally from the edge of a Scaffold to show navigation links in an application and this widget is used in scaffold using Scaffold.drawer property.

drawer: Drawer(
  elevation: 16.0,
  child: Column(
    children: <Widget>[
      UserAccountsDrawerHeader(
        accountName: Text("xyz"),
        accountEmail: Text("[email protected]"),
        currentAccountPicture: CircleAvatar(
          backgroundColor: Colors.white,
          child: Text("xyz"),
        ),
        otherAccountsPictures: <Widget>[
          CircleAvatar(
            backgroundColor: Colors.white,
            child: Text("abc"),
          )
        ],
      ),
      ListTile(
        title: new Text("Home"),
        leading: new Icon(Icons.mail),
      ),
      Divider(
        height: 0.1,
      ),
      ListTile(
        title: new Text("Profile"),
        leading: new Icon(Icons.inbox),
      ),
      ListTile(
        title: new Text("About us"),
        leading: new Icon(Icons.people),
      ),
      ListTile(
        title: new Text("Contact Us"),
        leading: new Icon(Icons.local_offer),
      )
    ],
  ),
),

Here, the drawer property of scaffold is used to create a drawer. We used various other widget inside this to make a little attractive drawer but don’t worry about all the other widgets, as we’ll be covering in the series.

persistentFooterButtons

persistentFooterButtons is a list of buttons that are displayed at the bottom of the scaffold. These buttons are persistently visible, even if the body of the scaffold scrolls. They will be wrapped in a ButtonBar and are rendered above the bottomNavigationBar but below the body.

persistentFooterButtons: <Widget>[
  RaisedButton(
    elevation: 10.0,
    onPressed: () {},
    color: Colors.green,
    child: Icon(
      Icons.add,
      color: Colors.white,
    ),
  ),
  RaisedButton(
    elevation: 10.0,
    onPressed: () {},
    color: Colors.blueGrey,
    child: Icon(
      Icons.clear,
      color: Colors.white,
    ),
  ),
],

Here, persistentFooterButtons take a list of widgets or buttons. In this we have used the RaisedButton if you want you can use the FlatButton instead.

bottomNavigationBar

bottomNavigationBar is used to display a navigation bar at the bottom of the scaffold. It is rendered below the persistentFooterButtons and the body.

bottomNavigationBar: BottomNavigationBar(
  currentIndex: 0,
  fixedColor: Colors.teal,
  items: [
    BottomNavigationBarItem(
      title: Text("Home"),
      icon: Icon(Icons.home),
    ),
    BottomNavigationBarItem(
      title: Text("Search"),
      icon: Icon(Icons.search),
    ),
    BottomNavigationBarItem(
      title: Text("Add"),
      icon: Icon(Icons.add_box),
    ),
  ],
  onTap: (int index){
    setState(() {
      _currentIndex = index;
    });
  },
),

endDrawer

enddrawer is like a drawer property but in enddrawer by default the drawer is display at the right side of the screen.

primary

Whether the scaffold is being displayed at the top of the screen. If true then the height of the appBar will be extended by the height of the screen’s status bar, i.e. the top padding for. The default value of this property is true.

backgroundColor

This property sets the background color of the scaffold.

backgroundColor: Colors.white,

resizeToAvoidBottomInset

If this property is true the body and the scaffold’s floating widgets should size themselves to avoid the onscreen keyboard whose height is defined by the bottom property.
For example, if there is an onscreen keyboard displayed above the scaffold, the body can be resized to avoid overlapping the keyboard, which prevents widgets inside the body from being obscured by the keyboard. Defaults to true.

12 Replies to “Scaffold”

  1. great publish, very informative. I’m wondering why the opposite experts of this
    sector do not realize this. You must continue your writing.
    I’m confident, you’ve a great readers’ base already!

  2. Excellent post. I was checking constantly this blog and I am impressed!
    Very helpful info specially the last part 🙂 I care
    for such info a lot. I was seeking this particular info for a very long time.

    Thank you and best of luck.

  3. Link exchange is nothing else but it is just placing the other person’s blog link on your page at appropriate place and other person will also do same for you.

Leave a Reply

Your email address will not be published.