In this tutorial, I will tell you how to use the Scaffold and more in the flutter.
Before Start
The Scaffold is so important to use in the flutter and I use it so frequently. So we must know how to use it in the right way. We have used this for all of our pages. It is a widget that used to contain our other widget. So let's look at a sample use.
We have used the appBar, the body is a Widget, you can use whatever widget you want. We just use the Container to show a Text. So it will show as follows.
Constructor
So before we start our tutorial we should know the constructor of the Scaffold.
This will show a button at the bottom of the screen. You can custom your own Widget, but usually, you can use the FloatingActionButton. Let's look at the code as follows.
If you want the button to show in the bottom-center, you can change the floatingActionButtonLocation to FloatingActionButtonLocation.centerFloat.It is easy, other location will retain to you as an exercise.
The floatingActionButtonAnimator will change the animation of this button. This is about the animation, I will tell you in the animation part in the next time.
persistentFooterButtons
If you set this parameter, it will show these widgets at the bottom of the Scaffold. You need to know that this parameter is a List. Let's look at the code as below.
The persistentFooterButtons part will show below the floatingButtonpart.
drawer
This parameter will draw a drawer in the left of the screen. When you click the menu on the left, I will show a drawer to you. Usually, we put a Drawer widget here, if you want to put other widgets here, it doesn't matter, but you should custom your own drawer. So let's look at the constructor of the Drawer to see how to use it.
We know the elevation controls the shadow of the widget, the child is a common widget. The semanticLabel is told before. It is just a string to describe the widget, often with voice. So let's use it.
Widget_drawer() =>Drawer( child:Column( children:<Widget>[AppBar( title:Text("Articles"), automaticallyImplyLeading:false, backgroundColor:RED_LIGHT, elevation:0, ),Container( alignment:Alignment.centerLeft, padding:EdgeInsets.only(left:20), constraints:BoxConstraints.expand(height:80), child:Text("This is my first article.", style:TextStyle(fontSize:20), ), ),Divider( height:0, color:TEXT_BLACK, ),Container( alignment:Alignment.centerLeft, padding:EdgeInsets.only(left:20), constraints:BoxConstraints.expand(height:80), child:Text("This is my second article.", style:TextStyle(fontSize:20), ), ),Divider( height:0, color:TEXT_BLACK, ),Container( alignment:Alignment.centerLeft, padding:EdgeInsets.only(left:20), constraints:BoxConstraints.expand(height:80), child:Text("This is my third article.", style:TextStyle(fontSize:20), ), ),Divider( height:0, color:TEXT_BLACK, ),Container( alignment:Alignment.centerLeft, padding:EdgeInsets.only(left:20), constraints:BoxConstraints.expand(height:80), child:Text("This is my forth article.", style:TextStyle(fontSize:20), ), ),Divider( height:0, color:TEXT_BLACK, ),Container( alignment:Alignment.centerLeft, padding:EdgeInsets.only(left:20), constraints:BoxConstraints.expand(height:80), child:Text("This is my fifth article.", style:TextStyle(fontSize:20), ), ),Divider( height:0, color:TEXT_BLACK, ),Container( alignment:Alignment.centerLeft, padding:EdgeInsets.only(left:20), constraints:BoxConstraints.expand(height:80), child:Text("This is my sixth article.", style:TextStyle(fontSize:20), ), ),Divider( height:0, color:TEXT_BLACK, ),Container( alignment:Alignment.centerLeft, padding:EdgeInsets.only(left:20), constraints:BoxConstraints.expand(height:80), child:Text("This is my seventh article.", style:TextStyle(fontSize:20), ), ),Divider( height:0, color:TEXT_BLACK, ), ], ), );
We click the menu widget to show the drawer, but if we want to show by our custom button, what can we do? there are two choice to do.
It will show as below, when you click the plus and forward. it will show the drawer.
The endDrawer is the same but with different direction and location. So I do not give the example.
bottomNavigationBar
This is very common in our real app.It shows that it is a widget, but usually, we use the BottomNavigationBar. It will show them at the bottom of the screen to switch different pages.
This is a dialog shown at the bottom of the screen, but above the bottomNavigationBar and persistentFooterButtons. You can use BottomSheet directly as follows.
The backgroundColor is easy. Just change its background color. The primary parameter will make the padding-top of the Scaffold with the status bar to be zero. When set the primary = false, the screen will scroll up. Let's see the code and see the effect.
backgroundColor:PURPLE,primary:false
Conclusion
Today, we have learned the Scaffold, this is a very important widget. There are some widgets, such as the Drawer,BottomNavigationBar,BottomSheet,FloatingActionButton,AppBar,persistentFooterButtons to construct our Scaffold.Hope you have learned how to use them.