Welcome to part 2 of my Flutter App development journal. In Part 1, I set up Flutter on my MacBook and documented some of the hurdles I encountered. In Part 2, I am going to build the main screen UI and many of the app’s reusable components.
- Part 1 – Flutter installation and environment setup
- Part 2 – Structuring the app’s main screen UI
- Part 3 – Navigating between app screens with routes
- Part 4 – Refactors and improvements
- Part 5 – Hooking up Firebase to my Flutter project
- Part 6 – Developing the create, read, and update features
- Part 7 – A ton of feature work
- Part 8 – User accounts [someday]
In this post: Putting together the main screen’s UI using ordinary Flutter widgets. Features include: scrollable screen with multiple lists and headers, models for mock data, and displaying mock data.
To view all of the code associated with this step on GitHub, click here.
Creating the boilerplate Flutter app
Running the flutter create app_name
command creates a folder with the app_name you give it and fill it with some starter code (called “boilerplate”) that you’ll either use or delete as you build your own app.
I like to build from the boilerplate app code you get from running flutter create, so I started with that.
Where to run flutter create
Run flutter create app_name
in the folder where you want your codebase to be created. In other words, if you want it to be in /projects, go to projects and run it inside projects.
❌ Note: Don’t do what I initially did, which was create the project folder, enter it, and run flutter create
inside it. That will give you a folder structure like this: /projects/app_name/app_name/ and I assume you don’t want that.
I keep all of my projects inside a folder named projects
, so I created my “Grocery Go” app from that directory:
cd projects
flutter create grocery_go
That will give you /projects/grocery_go/
Planning the main screen UI
My favorite mockup tools all seem to have gone to a subscription model, so I’m just gonna sketch my UI plans real quick on this notecard here…

The “goals” for this UI are:
- Two lists of items, one to represent “Shopping Lists” and one to represent “Stores”.
- Each list has a header
- Neither list should be scrollable on its own (no “scrolling within scrolling”), but the whole screen should scroll up/down
Creating the “Header” reusable component
This is the part that displays the section title, like “Shopping Lists” or “Stores”, in the scrolling list on the main page.
I know I’m going to use that “Header” bar at least twice, so I made it its own component. I also created a /components
directory to hold MainScreenListHeader.
(Perhaps this might be better termed a “widget”, but “component” was ingrained during my years of working as a web developer and it’s the first word I think of when describing reusable pieces of UI).
Anyway, I now have:
grocery_go/lib/components/main_screen_list_header.dart
I use underscores in the filenames and UpperCamelCasing for the class names inside, which is Dart convention.
Inside the file, I create a class called MainScreenListHeader that extends StatelessWidget and give it a constructor that takes this.text, which will serve as the title displayed in the header bar. This component returns a Container with a color, height, and a child Text widget.
import 'package:flutter/material.dart'; class MainScreenListHeader extends StatelessWidget { final String text; MainScreenListHeader({Key key, @required this.text}); @override Widget build(BuildContext context) { return Container( color: Colors.blue[800], height: 30.0, child: Center( child: Text(text, style: TextStyle( color: Colors.white, fontSize: 18, ) ) ) ); } }
I wanted to pick a simple example for the first component, but the next one will be more complicated.
Creating the reusable “ListView” component
The list that contains individual shopping lists (such as “Groceries”, “House stuff”, etc.) and the list of the user’s stores (“Safeway”, “Fred Meyer”, etc.) appear in vertical lists the user can scroll up and down through.
Stores also display their address or location so the user can distinguish the Safeway near their office from the Safeway near their home.
Since I don’t know how many Shopping Lists or Stores a user might have saved, ListView builder seemed like a good widget to start with. I also put this in its own component from the beginning, which I named
grocery_go/lib/components/main_screen_list.dart
import 'package:flutter/material.dart'; import 'package:grocery_go/models/shopping_list.dart'; class MainScreenList extends StatelessWidget { final list; MainScreenList({Key key, @required this.list}); @override Widget build(BuildContext context) { return ListView.builder( shrinkWrap: true, // gives it a size primary: false, // so it doesn't scroll itemCount: list.length, itemBuilder: (BuildContext context, int index) { return ListTile( title: Text(list[index].name), subtitle: Text(list[index] is ShoppingList ? list.length.toString() + ' items' : list[index].address), ); } ); } }
Now all I need to do is pass in list objects from main.dart
, which are declared as two separate lists of ShoppingList and Store objects.
I created two model files:
grocery_go/lib/models/shopping_list.dart
class ShoppingList {
final String name;
ShoppingList({this.name});
}
and
grocery_go/lib/models/store.dart
class Store { final String name; final String address; Store({this.name, this.address}); }
The last piece of the puzzle was to import the components and models in main.dart
and change the body: property of the Scaffold that gets returned to use a LayoutBuilder that returns a SingleChildScrollView of a Column with children that are instances of my MainScreenListHeader and MainScreenList (whew – I’ll explain more after the code).
import 'package:flutter/material.dart';
import './components/main_screen_list.dart';
import './components/main_screen_list_header.dart';
import './models/shopping_list.dart';
import './models/store.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Grocery Go!'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// Some placeholder data just so we can see things working
final List<ShoppingList> shoppingLists = [
ShoppingList(name: "Groceries"),
ShoppingList(name: "House stuff"),
];
final List<Store> stores = [
Store(name: "Safeway", address: "Juanita"),
Store(name: "Safeway", address: "Bellevue"),
Store(name: "Home Depot", address: "Bellevue"),
Store(name: "Fred Meyer", address: "Kirkland"),
Store(name: "Fred Meyer", address: "Bellevue"),
Store(name: "Fred Meyer", address: "Ellensburg")
];
@override
Widget build(BuildContext context) {
const headerShoppingLists = "Shopping Lists";
const headerStores = "Stores";
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints viewportConstraints) {
return SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
MainScreenListHeader(text: headerShoppingLists),
MainScreenList(list: shoppingLists),
MainScreenListHeader(text: headerStores),
MainScreenList(list: stores),
],
),
);
}),
);
}
}
These are the major widgets used in this layout and why I picked them:
Layout Builder – this widget sizes itself to its children at runtime (among other things, but that’s why I picked it). Since I don’t know how many Shopping Lists or Stores the user has, I need to build the layout with that in mind.
SingleChildScrollView – This is a container that scrolls, and you can fill it with whatever you want. I have multiple ListViews inside it but I want them to scroll as a single unit, so I’m going to turn off their own independent scrolling ability and let the SingleChildScrollView handle it instead.
Column – I want my elements to appear in a vertical column, one after another: header, list, header, list, and a Column makes that easy. I had to set mainAxisSize: mainAxisSize.min to get it to work with everything else I have going on.
ListView.builder – I don’t know how many Stores or Shopping lists will be displayed, so I used ListView.builder. There were two important properties I had to set on it:
shrinkWrap: true – I had some trouble getting my ListView to work inside a Column at first. I kept getting a 'hasSize': is not true
error. I found this StackOverflow post helpful, particularly Aziza’s reply.
primary: false – setting primary to false disables scrolling for this particular list. I want the SingleChildScrollView to handle the scrolling instead.
Here it is in Simulator: two ListViews inside a Column inside a SingleChildScrollView so they move as a unit. Hooray!

Adding ‘add new…’ buttons to each list
The next thing this UI needs is a “Add new list…” and “Add new store…” button at the end of each list. Tapping these buttons will take the user to a page where they can create a list or a store.
I changed the MainScreenList widget to take a listType string, which I will use on the “add new” button itself:
class MainScreenList extends StatelessWidget { final list; final String listType; MainScreenList({Key key, @required this.list, @required this.listType}); ...
Also in main_screen_list.dart, I changed itemCount to be the list length plus one more, so that last iteration can be used to draw the button at the end of the list, and the “listType” string that got passed in will used in the title, like so:
... itemCount: list.length + 1, itemBuilder: (BuildContext context, int index) { if (index == list.length) { return ListTile( title: Text("Add new " + listType + "...") ); ...
Back in main.dart, I now pass “shopping list” or “store” to MainScreenList.
children: <Widget>[
MainScreenListHeader(text: headerShoppingLists),
MainScreenList(list: shoppingLists, listType: "shopping list"),
MainScreenListHeader(text: headerStores),
MainScreenList(list: stores, listType: "store"),
Now there is a “Add new shopping list…” string at the end of the shopping lists:

And there is one at the end of the Stores list, too:

You can see these changes in this commit.
And that’s it for Part 2! Here’s a summary of what we did:
- Drew a quick mockup of what the main screen of the app should look like
- Ran flutter create to start the project
- Built some reusable components that are used to build a list of Shopping Lists and a list of Stores
- Populated those components with mock data
- Made the main page scroll as one large list
Up next in Part 3: adding more screens and navigating between them.
Being relatively new to Flutter and mobile dev in general, your explanation for setting `primary: false` on the `ListView.builder` === lightbulb moment.
Excellent write-up. I learned more than what I sought. Thank you!