Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Know Your Widgets: Scaffold in Flutter

If you’re here then you must’ve just started to work in Flutter and might have come across the Scaffold ‘thing’ almost everytime, like EVERYTIME and I am pretty sure you must’ve come across various ambiguous questions.

class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Drawer Module"),
),
drawer: Drawer(
child: ListView(
children: <Widget>[
new UserAccountsDrawerHeader(
accountName: Text("xyz"),
accountEmail: Text("xyz@gmail.com"),
currentAccountPicture: CircleAvatar(
backgroundColor: Colors.white,
child: Text("xyz"),
),
otherAccountsPictures: <Widget>[
new CircleAvatar(
backgroundColor: Colors.white,
child: Text("A"),
)

],
),

This is just a example of a module showing the root and basic requirement of scaffold in building an app in Flutter.

So, you must be curious to know about the Scaffold widget and it’s properties in flutter. Don’t worry we got your back, this blog will give you the basic knowledge about Scaffold and how to use it while creating your basic module.

Let’s go…..

What is Scaffold?

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 Scaffold
const 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.

Fig 1: Display an AppBar
  • 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.

Fig 2: Text displayed inside the body
  • 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 refer Fig 4 below .

Fig 3: A floatingActionButton with an icon
Fig 4: onPressed callback is called when tapped
  • 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,
Fig 5: floatingActionButtonLocation constants
  • drawer
    A 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("xyz@gmail.com"),
currentAccountPicture: CircleAvatar(
backgroundColor: Colors.white,
child: Text("xyz"),
),
otherAccountsPictures: <Widget>[
CircleAvatar(
backgroundColor: Colors.white,
child: Text("abc"),
)
],
),
ListTile(
title: new Text("All Inboxes"),
leading: new Icon(Icons.mail),
),
Divider(
height: 0.1,
),
ListTile(
title: new Text("Primary"),
leading: new Icon(Icons.inbox),
),
ListTile(
title: new Text("Social"),
leading: new Icon(Icons.people),
),
ListTile(
title: new Text("Promotions"),
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.

Fig 6: drawer
  • 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.

Fig 7: persistentFooterButtons
  • 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;
});
},
),
Fig 8: bottomNavigationBar
  • endDrawer
    A 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.
Fig 9: Shows primary property of scaffold
  • 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.

So, this was all about the various properties of the Scaffold which just gives you an overview of Scaffold. The purpose was to get your familiar with different properties and their usage for more detailed on the Scaffold widget refer to the flutter documentation here.

And for any other documentation related query on flutter click here.
Also check the github repo for Scaffold.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on FacebookGitHubTwitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Leave comment

Your email address will not be published. Required fields are marked with *.