Flutter Scaffold

 

The Scaffold is a widget in Flutter used to implements the basic material design visual layout structure. It is quick enough to create a general-purpose mobile application and contains almost everything we need to create a functional and responsive Flutter apps. This widget is able to occupy the whole device screen. In other words, we can say that it is mainly responsible for creating a base to the app screen on which the child widgets hold on and render on the screen. It provides many widgets or APIs for showing Drawer, SnackBar, BottomNavigationBar, AppBar, FloatingActionButton, and many more.

The Scaffold class is a shortcut to set up the look and design of our app that allows us not to build the individual visual elements manually. It saves our time to write more code for the look and feel of the app. The following are the constructor and properties of the Scaffold widget class.

  1. const Scaffold({  
  2.   Key key,  
  3.   this.appBar,  
  4.   this.body,  
  5.   this.floatingActionButton,  
  6.   this.floatingActionButtonLocation,  
  7.   this.persistentFooterButtons,  
  8.   this.drawer,  
  9.   this.endDrawer,  
  10.   this.bottomNavigationBar,  
  11.   this.bottomSheet,  
  12.   this.floatingActionButtonAnimator,  
  13.   this.backgroundColor,  
  14.   this.resizeToAvoidBottomPadding = true,  
  15.   this.primary = true,  
  16. })   

Let us understand all of the above properties in detail.

1. appBar: It is a horizontal bar that is mainly displayed at the top of the Scaffold widget. It is the main part of the Scaffold widget and displays at the top of the screen. Without this property, the Scaffold widget is incomplete. It uses the appBar widget that itself contains various properties like elevation, title, brightness, etc. See the below example:

  1. Widget build(BuildContext context)   
  2. {  
  3.   return Scaffold(  
  4.     appBar: AppBar(  
  5.       title: Text('First Flutter Application'),  
  6.     ), )  
  7. }  

In the above code, the title property uses a Text widget for displaying the text on the screen.

2. body: It is the other primary and required property of this widget, which will display the main content in the Scaffold. It signifies the place below the appBar and behind the floatingActionButton & drawer. The widgets inside the body are positioned at the top-left of the available space by default. See the below code:

  1. Widget build(BuildContext context) {   
  2. return Scaffold(   
  3.     appBar: AppBar(   
  4.     title: Text('First Flutter Application'),   
  5.     ),   
  6.     body: Center(   
  7.     child: Text("Welcome to Javatpoint",   
  8.         style: TextStyle( color: Colors.black, fontSize: 30.0,   
  9.         ),   
  10.          ),   
  11.     ),  
  12. }  

In the above code, we have displayed a text "Welcome to Javatpoint!!" in the body attribute. This text is aligned in the center of the page by using the Center widget. Here, we have also styled the text by using the TextStyle widget, such as color, font size, etc.

3. drawer: It is a slider panel that is displayed at the side of the body. Usually, it is hidden on the mobile devices, but the user can swipe it left to right or right to left to access the drawer menu. It uses the Drawer widget properties slides in a horizontal direction from the Scaffold edge to show navigation links in the application. An appropriate icon for the drawer is set automatically in an appBar property. The gesture is also set automatically to open the drawer. See the following code.

  1. drawer: Drawer(   
  2.         child: ListView(   
  3.         children: const <Widget>[   
  4.         DrawerHeader(   
  5.             decoration: BoxDecoration(   
  6.             color: Colors.red,   
  7.             ),   
  8.             child: Text(   
  9.             'Welcome to Javatpoint',   
  10.             style: TextStyle(   
  11.                 color: Colors.green,   
  12.                 fontSize: 30,   
  13.             ),   
  14.               ),   
  15.         ),   
  16.         ListTile(   
  17.             title: Text('1'),   
  18.         ),   
  19.         ListTile(  
  20.             title: new Text("All Mail Inboxes"),  
  21.             leading: new Icon(Icons.mail),  
  22.         ),  
  23.         Divider(  
  24.             height: 0.2,  
  25.         ),  
  26.         ListTile(  
  27.             title: new Text("Primary"),  
  28.         ),  
  29.         ListTile(  
  30.             title: new Text("Social"),  
  31.         ),  
  32.         ListTile(  
  33.             title: new Text("Promotions"),  
  34.         ),   
  35.         ],   
  36.          ),   
  37.   ),  

In the above code, we use the drawer property of Scaffold for creating a drawer. We have also used some other widgets to make it attractive. In the ListView widget, we have divided the panel into two parts, Header and Menu. The DrawerHeader property modifies the panel header that also contains an icon or details according to the application. Again, we have used ListTile to add the list items in the menu.

4. floatingActionButton: It is a button displayed at the bottom right corner and floating above the body. It is a circular icon button that floats over the content of a screen at a fixed place to promote a primary action in the application. While scrolling the page, its position cannot be changed. It uses the FloatingActionButton widget properties using Scaffold.floatingActionButton. See the below code:

  1. Widget build(BuildContext context) {   
  2.          return Scaffold(   
  3.     appBar: AppBar(title: Text('First Flutter Application')),   
  4.     body: Center(   
  5.         child: Text("Welcome to Javatpoint!!"),   
  6.     ),   
  7.     floatingActionButton: FloatingActionButton(   
  8.         elevation: 8.0,   
  9.         child: Icon(Icons.add),   
  10.         onPressed: (){   
  11.            print('I am Floating Action Button');  
  12.         }   
  13.     );   
  14. }  

In the above code, we have used the elevation property that gives a shadow effect to the button. We have also used the Icon widget to give an icon to the button using preloaded Flutter SDK icons. The onPressed() property will be called when the user taps the button, and the statements "I am Floating Action Button" will be printed on the console.

5. backgroundColor: This property is used to set the background color of the whole Scaffold widget.

  1. backgroundColor: Colors.yellow,  

6. primary: It is used to tell whether the Scaffold will be displayed at the top of the screen or not. Its default value is true that means the height of the appBar extended by the height of the screen's status bar.

  1. primary: true/false,  

7. persistentFooterButton: It is a list of buttons that are displayed at the bottom of the Scaffold widget. These property items are always visible; even we have scroll the body of the Scaffold. It is always wrapped in a ButtonBar widget. They are rendered below the body but above the bottomNavigationBar.

  1. persistentFooterButtons: <Widget>[  
  2.   RaisedButton(  
  3.     onPressed: () {},  
  4.     color: Colors.blue,  
  5.     child: Icon(  
  6.       Icons.add,  
  7.       color: Colors.white,  
  8.     ),  
  9.   ),  
  10.   RaisedButton(  
  11.     onPressed: () {},  
  12.     color: Colors.green,  
  13.     child: Icon(  
  14.       Icons.clear,  
  15.       color: Colors.white,  
  16.     ),  
  17.   ),  
  18. ],  

In the above code, we have used the RaisedButton that displays at the bottom of the Scaffold. We can also use the FlatButton instead of the RaisedButton.

8. bottomNavigationBar: This property is like a menu that displays a navigation bar at the bottom of the Scaffold. It can be seen in most of the mobile applications. This property allows the developer to add multiple icons or texts in the bar as items. It should be rendered below the body and persistentFooterButtons. See the below code:

  1. bottomNavigationBar: BottomNavigationBar(  
  2.   currentIndex: 0,  
  3.   fixedColor: Colors.grey,  
  4.   items: [  
  5.     BottomNavigationBarItem(  
  6.       title: Text("Home"),  
  7.       icon: Icon(Icons.home),  
  8.     ),  
  9.     BottomNavigationBarItem(  
  10.       title: Text("Search"),  
  11.       icon: Icon(Icons.search),  
  12.     ),  
  13.     BottomNavigationBarItem(  
  14.       title: Text("User Profile"),  
  15.       icon: Icon(Icons.account_circle),  
  16.     ),  
  17.   ],  
  18.   onTap: (int itemIndex){  
  19.     setState(() {  
  20.       _currentIndex = itemIndex;  
  21.     });  
  22.   },  
  23. ),  

In the above code, we have used the BottomNavigationBar widget to display the menu bar. The fixedColor property is used for the color of the active icon. The BottomNavigationBarItems widget is used to add items in the bar containing text and icon as its child property. We had also used onTap(int itemIndex) function to perform an action when we tapped on the items, which works according to their index position.

9. endDrawer: It is similar to a drawer property, but they are displayed at the right side of the screen by default. It can be swiped right to left or left to right.

10. resizeToAvoidBottomInset: If it is true, the body and the Scaffold's floating widgets should adjust their size themselves to avoid the onscreen keyboard. The bottom property defines the onscreen keyboard height.

11. floatingActionButtonLocation: By default, it is positioned at the bottom right corner of the screen. It is used to determine the position of the floatingActionButton. It contains many predefined constants, such as centerDocked, centerFloat, endDocked, endFloat, etc.

That's all about the Scaffold's various properties that give us an overview of the Scaffold widget. Its main purpose is to get familiar with different properties and their usage in Flutter application. If we want to learn it in more detail, refer to the flutter documentation here.

Let us see the example where we have tried to use most of the Scaffold properties to understand this widget quickly and easily.

In this example, we are going to see a Scaffold widget with an AppBar, BottomAppBar, FloatingActionButton, floatingActionButtonLocation, and drawer properties.

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. /// This Widget is the main application widget.  
  6. class MyApp extends StatelessWidget {  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       home: MyStatefulWidget(),  
  11.     );  
  12.   }  
  13. }  
  14.   
  15. class MyStatefulWidget extends StatefulWidget {  
  16.   MyStatefulWidget({Key key}) : super(key: key);  
  17.   
  18.   @override  
  19.   _MyStatefulWidgetState createState() => _MyStatefulWidgetState();  
  20. }  
  21.   
  22. class _MyStatefulWidgetState extends State<MyStatefulWidget> {  
  23.   int _count = 0;  
  24.   
  25.   Widget build(BuildContext context) {  
  26.     return Scaffold(  
  27.       appBar: AppBar(  
  28.         title: Text('Flutter Scaffold Example'),  
  29.       ),  
  30.       body: Center(  
  31.         child: Text('We have pressed the button $_count times.'),  
  32.       ),  
  33.       bottomNavigationBar: BottomAppBar(  
  34.         shape: const CircularNotchedRectangle(),  
  35.         child: Container(  
  36.           height: 50.0,  
  37.         ),  
  38.       ),  
  39.       floatingActionButton: FloatingActionButton(  
  40.         onPressed: () => setState(() {  
  41.           _count++;  
  42.         }),  
  43.         tooltip: 'Increment Counter',  
  44.         child: Icon(Icons.add),  
  45.       ),  
  46.       floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,  
  47.       drawer: Drawer(  
  48.         elevation: 20.0,  
  49.         child: Column(  
  50.           children: <Widget>[  
  51.             UserAccountsDrawerHeader(  
  52.               accountName: Text("javatpoint"),  
  53.               accountEmail: Text("javatpoint@gmail.com"),  
  54.               currentAccountPicture: CircleAvatar(  
  55.                 backgroundColor: Colors.yellow,  
  56.                 child: Text("abc"),  
  57.               ),  
  58.             ),  
  59.             ListTile(  
  60.               title: new Text("Inbox"),  
  61.               leading: new Icon(Icons.mail),  
  62.             ),  
  63.             Divider( height: 0.1,),  
  64.             ListTile(  
  65.               title: new Text("Primary"),  
  66.               leading: new Icon(Icons.inbox),  
  67.             ),  
  68.             ListTile(  
  69.               title: new Text("Social"),  
  70.               leading: new Icon(Icons.people),  
  71.             ),  
  72.             ListTile(  
  73.               title: new Text("Promotions"),  
  74.               leading: new Icon(Icons.local_offer),  
  75.             )  
  76.           ],  
  77.         ),  
  78.       ),  
  79.     );  
  80.   }  
  81. }  

Output:

When we run this project in the IDE, we will see the UI as the following screenshot.

Flutter Scaffold

If we click on the three lines that can be seen in the top left corner of the screen, we will see the drawer. The drawer can be swiped right to left or left to right. See the below image.

Flutter Scaffold


Post a Comment

Previous Post Next Post