The Scaffold widget
We have seen the Scaffold
widget many times, but there are many hidden depths to the Scaffold
widget that can really bring your app to life. In this section, we’ll explore some of the most used features so that you have a starting point for your apps.
Drawer
One of my favorites is the drawer
parameter, which allows you to add a sidebar to your app that can slide in and out from the side of the screen on demand. You have probably seen this on other apps and assumed it would be really complicated to achieve. However, with Flutter, it is surprisingly easy. Let’s walk through an example of adding a drawer to our app.
Firstly, we need to define a global key. This allows us to refer to the scaffold directly, so we add the following line to our class:
final _scaffoldKey = GlobalKey<ScaffoldState>();
We then set the key as the parameter to Scaffold
, thereby linking the key to the widget:
Scaffold( key: _scaffoldKey, ...