Introduction to Flutter

Tram Ho


Flutter is developed by and it uses Google’s UI toolkit for building native compiled applications for mobile platforms, web, and desktop all from a single codebase. It aims at delivering Fast Development with support for hot reloading, Expressive and Flexible User Interfaces and all while delivering Native Performance in all these platforms. The native performance is achieved by compiling your code to native ARM machine code using Dart’s native compilers. This tutorial will guide you into understanding the basics of Flutter and i like to teach by demonstration so as always we will make a working application with a database (Sql Lite) for demonstrating how easy it is to build a multiplatform app that fetches data, save data and update this data using flutter. Ill do my best to avoid anything that may complicate your learning progress so without further ado, let us dive right in.

Download flutter here . Select your platform either Mac or Windows or Ubuntu. Downlaod the zip/file and extract to C: directory so your flutter folder will be located at C:flutter (On Windows). You can also add the flutter to your Variable path by clicking the start icon then go to Edit the system environment variable. Next select Environment Variables > Path > Edit then add new and input C:flutterbin (Note because in my case flutter was pasted into C: so i specify this directory). Click ok and close the pop up.

Next you can verify what version of flutter is installed by opening the command line and entering where flutter (On windows) or which flutter (On Mac). Also if you have an error specifying that dart isnt installed you can follow the instruction to install dart then run flutter doctor to see some info about flutter. Moving on! I will be using Android studio as my IDE because it native supports flutter making it easier to develop our apps. Open android studio and follow the following steps to setup you environment.

More note on Mac Version

To fully set up Mac version. After you paste your flutter in a directory. In my case Desktop (Mac) you can add it to PATH by opening command line and typing: rm -rf <flutter_repo_directory>/Desktop/bin/cache (Please note my specified directory includes Desktop so input yours accordingly). To test if it works close the command line and re open then type flutter doctor this will download necessary files and give you a summary. Also its important to update your XCode to latest version if you want to test on ios devices also. Next follow the instruction to fix remaining errors. In my case i got an error to install cocoapods which i did by typing: sudo gem install cocoapods and also updating xcode by typing: sudo xcode-select --switch /Applications/
After you have successfully run and updated this tools you can run flutter doctor again and this time you should get no error and also will be able to detect attached devices. If you encounter any error please follow this guide again.


Click Configure >> Packages and search flutter. Install it then restart Android Studio.


Click create new Flutter App >> Set the sdk path of your flutter >> Set name of the app (contact_app) and Finish.

Once your apps is done building, Run it to make sure everything is fine.



Now lets begin the fun parts


Inside the lib folder, create a new folder called model and in this a dart class Contact.


Next lets update the pubspec.yaml file to import sqflite library. sqflite is a sql lite library that we can use to save our data. Remove all the unnecessary text so your file appears cleaner and add the sqflite: path: to the dependency as shown below.


Now we need to add another package screens. Here we will add a screen CreateContactScreen.dart. This will have our contacts input such as name, number and so on. But first lets set up our Homescreen.dart which is the starting screen of our app.


In this class we have added an app bar which includes two icons. One is the add icon to open the CreateContactScreen when clicked and the second is a search icon which we will also implement later on. The function navigateToCreateContactScreen will take us to the CreateContactScreen using MaterialPageRoute which we pass the name of our screen to. You have one error because we havent create this screen CreateContactScreen() yet so lets do that but before we do that we have to let the app know we want our HomeScreen to be the launch and start screen of our app so open main.dart class and set it.


A breif explanation of what we have done here. First we cleared the default auto generated code and set the title of the App and theme to color blue (You can change to whatever color you prefer) but the most important part is we set the home: HomeSceen() letting the app knows we want this to be our launch screen. The first and second line are imports only. The first imports the HomeScreen class and the other imports material.dart library which allows us to use a kind of material design elements as provided by google. Enough talk lets proceed. Now we need to fix the error on the HomeScreen by creating another screen in our screens package called CreateContactScreen but first lets create an images directory and add camera.png to it (Note that this directory should be added to the main project folder not lib)

image (camera.png)

We also have to reference this image in the pubspec.yaml file else you wont be able to use it. So add this line to the file right under uses-material-design: true .


Alright now the CreateContactScreen design we want to archeive should look something like this:

Its a simple design and ill explain each part of it as simple as i can.


So lets get to it. Here are the important parts of this class and what they do:

  • Line 1 to 3: Our imports. We imported the Model (Contact.dart) class and other libraries we need.
  • Line 5: We extended the StatefulWidget class. Do not worry much about what StatefulWidget is but understand that most design interfaces in Flutter are treated as widgets and in this case we are using StatefulWidget because it is a widget that describes part of the user interface by building a constellation of other widgets (widget within widget) that describe the user interface more concretely. So we use Stateful widgets when the part of the user interface can change dynamically. Also Since the fields we are using now are editables, the Widget must be a StatefulWidget, so we have created our StatefulWidgets with their respective States (nameTextController, phoneTextController….). Moving on!
  • Line 15 to end: Defintely where all the actions happens. Because we will be using this screen to add new Contact and also update a contact we can archieve this by extending Sate framework. In the initState we will recieve an instance of the Contact class which we will check if not null and set the present widgets values (In case of update only. You can see we also set the title to this will be fully integrated when we finish the database). Name will be set, gender and all others.

Next we are disposing our controllers in the dispose method. This is Called when an object is removed from the tree permanently. This is to release any resources retained by any of our objects/controllers. Now lets move on to the build (Line 45+). Here one can customize the Appbar, body and add child widgets to the interface. Each widget is put in a seperate padding wrapper so you can easily find them. there is the Image where we set the assets as our camera.png, then there are mutiple TextFields for Name, Address, Phone and so on. You can also see we set the keyboardType specifically for each TextField and pass the controllers to their respective owners while aslo using material icons to set custom icons for the Fields.

The last Widgets are the radio buttons which are placed in a row and assigned value 0 and 1 respectively. 0 for Male and 1 for female. We also added an onChanged events that will be fired when a radio button is checked where the value of the radio button will be passed to the variable _radioGenderValue

The floatingActionButton we added when clicked will call a method which we will create later to save our Contact in the database. I know this is a lot to digest but dont worry about the blabering, at least not all of it. Only try to understand the essential part of the class. What does what, Which function is responsible for what and thats all.

With this class done we can test the app. Connect the device and click on run and you should be directed to the HomeScreen. Click on the + icon and the CreateContactScreen should open.

HomeScreen and CreateContactScreen Android

HomeScreen and CreateContactScreen Ios

Lets call this the end of Part1 and now we move on to Part2.

Part 2

In this part we will integrate the sqlite database in our app. We already added the sql library to the dependency so now we will create the databaseHelper classes and such. Inside the lib directory add a new package db and in here add a class DatabaseHelper


Using the sql lite query: "CREATE TABLE contacts(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, avatar TEXT, name TEXT, address TEXT, phone TEXT, gender INTEGER)"); we have created a table contacts and added an auto generated id for each row with collums to hold all the contact data from avatar to gender. The method insertContact() will be used to add a contact to the database while we retrieve all the contacts as a List using the retrieveContacts() method. To delete we will also call the deleteContact() method which accepts the id as a parameter to locate a contact and finally the updateContact() will you guessed it, update a contact.

Next open the CreateContactScreen.dart and add this function:

Finally call this method in the onPressed of the floating button to save. Note that we also check if an object contact exist and if so we will update and not create a new contact. This contact will be passed when user selects a contact from HomeScreen as we will be using the same screen (CreateContactScreen) for creating new contact and also updating a contact.

We will have to update our HomeScreen so that all our contacts are loaded and displayed automatically when we open the screen as a normal contact app should. We will make use of the retrieveContacts() in the DatabaseHelper class to get all our contacts then pass to a List which is then displayed in the body of our HomeScreen.dart. We also have to update the HomeScreen to extend a StatefulWidget rather than the previous StatelessWidget. There will be a class ContactListView which will return our custom view layout. Below is the full updated HomeScreen.dart.


Next add a class ContactsListView which we have already passed as the body to out HomeScreen body in line 38.


We fetch the contacts saved from the database using the method _fetchContacts Using name as title and the phone as subtitle then we set a delete icon as trailing and added a onPressed() event so whenever user click the delete icon it calls _deleteContact().
Also we navigate to the CreateContact screen when the user selects a contact except this time we pass the selected contacts to the instance. This will allow us to update the contact rather than create a new one. Refer to the button save clicked to understand where and how this happens.

Run the App (Note that the Contacts will not auto upload when you return to the HomeScreen. We will fix this later. You can quit the app after saving a contact then reopen to see you saved contact for now).

iOS Test

Android Test

In the next part we will add the add image feature using image picker library for flutter and share feature with anything else i can come up with while also improving the app. Until then……..Happy Coding!!!

Share the news now

Source : Viblo