iOS

Building a Flutter App with Complex UI


Hello there! Welcome to the second tutorial of our Flutter series. In the last tutorial, you learned the basics of building a Flutter app. So if you have not gone through it, please take a pit stop here, visit it first before proceeding with this tutorial.

What will you learn?

In today’s tutorial, we will build an app with complex UI with the Flutter framework. We will explore quite a lot of components. By going through the tutorial, you will learn to implement:

  • Textfields with Validation
  • Centralized App Theme
  • Carousel Slider
  • List View
  • Complex List View
  • Basic Form
  • Scoped Model

If any of the above raises your eyebrow, then you are here at the right place! Tighten your seatbelt as this tutorial will take you on a ride to learn all these concepts, by building an app called Moments.

Moments App

Moments App is a simple app that has the following screens:

  • Login Screen
  • Gallery Screen
  • Menu Screen
  • Form Screen
  • List Screen
  • Rich List Screen

You will build the screens sequentially, learn the different ways of wiring up views. And, this is how the final app will look like:

Building a Flutter App with Complex UI 1

Let’s get started!

Getting Started

Please proceed to download the starter project and spend about 10-15 minutes to look through the code. The app has 4 main folders:

  • Model
  • Pages
  • Scoped Models
  • Utils

With 2 additional files main.dart and app.dart. Most of the functions are marked with // TODO, so you can implement its logic to build the components.

Login Screen

Let’s first take a look at how the completed Login Screen is:

flutter-app-login-screen

It has the following UI components:

  • Image
  • Title Text
  • TextField Title Text
  • TextField
  • Button

Proceed to login.dart. Here you will first implement the login textfield, start by adding this ladder of codes:

In this code, you returned a Theme Widget with a child of TextFormField. You should take a look at theme.dart to see how the theme data is set before proceeding further. Using a centralised theme, any view that requires a text style can easily reference by calling Theme.of(context).textTheme.[styleType].

You should take a look at these codes to understand how this view is built as they will be repeatedly used in this tutorial. This is how the TextField is created:

  • Theme
    • ThemeData
    • TextFormField
      • InputDecoration
        • OutlineInputBorder
        • IconButton

The TextFormField has an OutlineInputBorder and a Clear IconButton, wrapped in Theme. It has a mobileNumberController which handles input text, and a validator for validation checks. onFieldSubmitted is triggered when the textInputAction is activated. To find out more about TextFormField, read the documentation here.

Moving on to the Enter button, a custom button has been created for you in buttons.dart. Here, you will just need to initialise a MomentsButton like this and since the button needs to be centered, wrap it with Center:

The final step to complete this page is to layer everything into the body properties in build function:

  • Scaffold
    • SafeArea
      • ListView
        • Column
          • Image
          • Text
        • Text
        • Login TextField
        • Enter Button

You are starting to see that the best way to arrange views vertically is to use Column. SafeArea here is used to handle any view changes caused by the keyboard.

ListView automatically helps you handle any overflowing views and it includes a scrolling mechanism. SizedBox is used to specify fixed spaces between views.

With the Login Screen completed, many classes of views and components were introduced, and you will see them in action more as you build the next few pages.

Now let’s run the app, try to key in a wrong code 1111 and then a correct code 1234, and then move on to the Gallery Screen!

You will not dive deep into how Scoped Models work in this app. You can open user.dart to see the login logic, and explore this crash course. In a nutshell, Scoped Model is used to manage variables that will live in the lifetime of the app.

Gallery Screen

Next, we will build the Gallery screen. You will use Carousel Slider to build an image slider, and again, add a button to move to the next screen. Proceed to gallery.dart to get started.

Firstly, if you look at our assets folder, you notice there are already pre-loaded images labelled pic1.jpg etc. These are also declared in an array of strings call imgList.

Using the pre-created map function, you map them each into a Container so the output result is actually a List of Containers:

With the List of Containers ready, you can now load them into the Carousel like this in _CarouselWithIndicatorState class:

Again, Column is used to layout 2 views vertically:

  • View #1 – Carousel
  • View #2 – Row of Carousel Indicators

If you can using Visual Studio Code and using a MAC machine, you can whole CMD and hover on CarouselSlider to view more information about this class.

The MomentsButton can be created just like the previous one you have built:

Having a custom class helps minimise repeated codes.

The last step for this page is to link everything up in build, add these codes after _buildMomentsButton:

PreferredSize is used to give AppBar a fixed size. Using MainAxisAlignment.start positions its children views in a top-down approach.

Run the app and you should see a nice carousel slide show :-).

flutter-carousel

Menu Screen

You are now down to just 3 more main screens to build, with a Menu Container Screen. In gallery.dart, you ended with a MomentsButton which navigates user to MainPage.

flutter menu screen

Open up main.dart in pages folder, and in this page, there will be 3 menu buttons at the top, which when each one is being tapped, it will switch the child view to its respective view.

Let’s start by implementing buildMenuItems:

Wow! That’s really a lot of code. It does look intimidating, but actually, they are really simple. Logically, every MenuButton is made up of:

  1. Image
  2. Title
  3. MenuItem Type (Enum)

At the end, you get the complete view likes this:

  • Container
    • InkWell (Allows Tap)
      • Column
        • Image
        • Text

This is one way you actually build a customised button. InkWell brings in the ability to implement onTap event. The function also uses isSelectedMenuItem to render a bordered or non-bordered view. At the end of this function, it returns the columns as a List.

Next, implement getPage which returns the selected page object:

And, finally, implement the Column view which has a Row that contains all the customised buttons:

In the Row here, MainAxisAlignment.spaceEvenly helps to spread the views evenly as the screen size changes. In the next layer, a simple Welcome message is shown. Followed by the child view which is rendered by getPage.

Run the app now and you should be able to tap on each of the button, see them being selected! Nothing much happens at the child view yet as they are not implemented. That is up next!

Form Screen

Next, we’ll implement the form screen. Open form.dart. In most of the apps you are using now, it’s common to implement a page with a form to submit information. Here’s a quick way to implement a form with radio buttons, checkbox and also a counter.

Name Text Field

The textfield is completely similar to the one you have implemented in the login screen, with the exception that here a ListTile is used with leading and title used to specify the location of the views:

I will not go into further explanation here as bulks of the code are the same. You could challenge yourself by placing them in a centralised class like how I did it for MomentsButton, give it a custom initiliser and call it to reduce code.

Guests Counter

The counter section contains a label text, a - button, a counter text and a + button:

As you have understood how a ListTile works, the leading view is the label, where as the title view contains a Rowwhich contains:

  • IconButton (-)
  • Text
  • IconButton (+)

Section Header

In every section, we are also giving it a nice header:

Gender Radio Option

This is radio button type option, so go ahead and add this:

In Visual Studio Code, you can actually use OPTION+SHIFT+F to perform an auto-indentation.

Up to this point, I believe you are already a ListTile expert. Here we introduced Radio.

Transport Checkbox Option

The last option type is Checkbox:

Nothing really special here, we used InkWell to improve the tap area of the Checkbox.

Alert Dialog

For the alert dialog, you may have already seen how an AlertDialog is created. Here’s a simple dialog you should add to validate user’s input:

Lastly, the Submit Button which you are implemented it for the third time:

Let the views assemble! Take a look at renderForm and you can see how we can render the form and order of each section easily! Here, the build function is completed for you so go ahead and run the app!

Try submitting the form without a name, you should be prompted with the AlertDialog you added.

If you are unable to see your page rendered, try to delete the app and perform a fresh installation. Sometimes, flutter may unknowingly cache it.

List Screen

Displaying a List of items is also a common feature of every app that we use. Headover to item_list.dart and here, you will use ListView to build a list of data:

The data has been pre-loaded from scoped-models/item_list.dart. The builder method in ListView automatically helps you iterate through the data List, so you can implement the rendering logic in itemBuilder. The build function has been implemented for you. Go ahead and run the app, you should see a nice scrolling view in List tab 🙂

Building a Flutter App with Complex UI 2

Rich List Screen

The last and final screen is the RichList screen. I personally named it as RichList because it has an image and a customised look. Headover to rich_list.dart and you should already see everything implemented for you. But, you are seeing an empty screen because each card view is not implemented yet.

Now go to rich_list_item_card.dart and here is where all the wires are missing. You will be giving each image a circular frame, so go ahead and add these in itemImage:

Every image is also accompanied by a Card with description on the right:

And finally, let’s combine both into build function:

While Column and Row help you to layer the views vertically down and horizontally across, Stack helps you to layer your views on top of each other. Here, the image overlaps the Card View by putting it in the second postion. 

Run the app and you should see the completed RichList!

Building a Flutter App with Complex UI 3

Moving Forward

You can download the finished project here. You have learnt a lot about creating more complex UIs in this tutorial using Flutter. Flutter is natively developed by Google so it will generally have better performance on Android devices. However, given its natively-compiled capability, it actually runs smoothly on most iOS devices as well.

Flutter is still at its infant stage, and it surely has a very bright future. It can help you create beautiful and complex UIs with lesser code. I encourage you to use its documentation diligently to build your own flavor of apps.

Thank you for following through this tutorial!

iOS
Building a QR Code Generator with Core Image Filters
iOS
Enhance Your Simple Table App With Property List
iOS
Documenting Your Objective-C and Swift Code in Xcode with HeaderDoc and Doxygen
There are currently no comments.

Shares