Financial Literacy and Mental Health

Apparently there is a whole field called “financial therapy” which focuses on the psychology aspects of money. I took a Zoom workshop from The Financial Diet and wanted to share my notes. There isn’t…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Write a Simple Navigation Drawer App in Flutter

Let’s dive into implementing our simple Navigation Drawer and see how simple this thing is in Flutter, and to prove “Flutter is a breeze”

The aim of this tutorial is to build a simple app with Navigation Drawer, screenshot of what we will be building is shown below.

I will be using Android studio IDE, feel free to use any IDE of your choice as the implementation is the same cross IDE.

After a successful installation, if your are using Android studio, go to file and you will see New, under it, there is New Flutter Project, select it and configure the rest as prompted by the IDE.

We will write most of our code into lib directory, so locate main.dart inside the lib directory and replace the code with the below snippet, don’t worry i will walk you through the code in a moment.

Every Flutter app needs an entry file called main.dart which must hold a function called main that also returns runApp(MyApp())

Inside that we have debugShowCheckedModeBanner: false, that helps us remove the dubug banner that comes as a default from flutter SDK, after that we simply have home attribute that holds what we want to see first when we start our app, that is our first page: first route. below that we define our routes, it takes a string which is an endpoint and a widget that returns our destination page.

For now, those pages are not yet created so ignore the error you will see in that regards, because that will be the next step of action.

Create three different files in the lib directories such as homepage.dart news.dart and profile.dart your working directory should look like this

project directory

By default, your newly created files will be empty, paste the below snippet into homepage.dart file.

By now, our homepage is ready where our drawer implementation resides.😲Relax, let me walk you through what we have in homepage lets pay much attention here, this is where the real business sits. Let’s go guys😋

HomePage extends StatelessWidget because we don’t have to change state,

we declared our appbar and drawer attribute in Scaffold, this is possible because Scaffold class comes with these attributes and lots more, but our focus here is just on appbar and drawer.

We could have used just pushNamed that also navigates to other page as well, but we did not want to leave the current page and push another on it, this approach messes up when we click on the back button, it preserves all the pages and still pushes on to the existing stack, what the heck!🤥 we don’t want to do this.

This method takes two arguments context of the current page and the string endpoint that defines the destination as declared in the main.dart file

Wow, we have gone this far 🤑 The last one on the scaffold is body is the part that holds everything we need to implement int the main page of the Scaffold. We centralized the Text shown on the homepage using widget Center widget.

Lastly before we go, don’t forget we passed title to the profile and news pages from the main.dart as the constructor, that is the easy way of passing data to other widget. In the profile file similar in news file too, we wrote a constructor for the class so that we can pass data down from main file. This is pretty simple as we are already familiar with OOP concepts in programming.

The next thing is to put something in profile.dart and news.dart so that we can identify them when clicking on the drawer items, paste below code inside profile.dart file.

And finally, in our news.dart the two files almost have the same thing, paste the below code in there

If everything is fine, boom save and run the app, you will se your beautiful app with navigation drawer

Implementing Navigation Drawer in your app makes it user friendly and easy to access all features at one place. It gives this feature to the app to display contents to the users.

Feel free to play around with the app, do your own tweak and keep coding in Flutter. 😀😇

If you have any suggestion, or found any mistakes or improvements do drop a comment below, and don’t forget to clap as many as you can, and share with friends. Thanks.

Add a comment

Related posts:

How to Pick the Right Real Estate Consultant

Real estate consultants provide the analysis and research needed to understand a location’s real estate market. For major metropolitan areas like New York City and opportunity zones like Oakland…

Global Paper Cup Machine Market was Worth USD XX in 2019 and is Estimated to Reach USD XX by 2025

The Global Paper Cup Machine Market Research Report 2020 is an extensive and latest study on the global Paper Cup Machine industry that highlights promising growth opportunities, challenges, and…

Why You Never Want Your Fiction to Read Like a Research Paper

I wrote at least ten research papers during my five years of graduate school, one that was recently published in a book of essays I believe is a super compelling reads. I’ve also read research papers…