sandwich_shop

Worksheet 2 — Stateless and Stateful Widgets

Table of contents

Getting help

To get support with this worksheet, follow the Discord guide and post your questions there. You can also attend your timetabled practical sessions and ask a member of teaching staff for guidance.

Getting started

Continue from Worksheet 1

You can continue directly with the Sandwich Shop project you completed in Worksheet 1. Open the project folder in VS Code with File > Open Folder. You do not need to clone the repository again.

Clone the Sandwich Shop repository

If you do not have the project from Worksheet 1, clone the Sandwich Shop repository. Open a terminal and move to a folder that is not synchronised to cloud storage, such as your Downloads folder:

cd ~/Downloads
git clone https://github.com/manighahrmani/sandwich_shop
cd sandwich_shop

The cd command changes the current folder in the terminal. The first command moves to Downloads, and the final command moves into the cloned sandwich_shop folder. Open that folder in VS Code with File > Open Folder.

You can also clone the repository without typing terminal commands. Open the Source Control panel, select Clone Repository, enter https://github.com/manighahrmani/sandwich_shop, choose where to save it, then open the cloned folder, as shown below:

Cloning the repository from the Source Control panel in VS Code

Clean your working tree

GitHub repositories can have different branches. Think of branches as different versions of the project. This repository opens on the main branch, which contains the worksheets rather than the Flutter application. For this worksheet, you need branch 1, which contains the Sandwich Shop app as it should look after Worksheet 1.

Before switching branches, open the Source Control panel with Ctrl + Shift + G on Windows or ⌃ + Shift + G on macOS and check that there are no uncommitted changes. If you have no uncommitted changes, your Source Control panel should look like this:

Source Control panel with no uncommitted changes in VS Code

If you have uncommitted changes, commit any work that you want to keep. If you do not want to keep a change, discard it from the Source Control panel, as shown below:

Discarding uncommitted changes from the Source Control panel in VS Code

Switch to branch 1

You can switch branches from the status bar at the bottom of VS Code. Click the current branch name, then select branch 1, as shown below:

Selecting branch 1 from the branch menu in the VS Code status bar

Alternatively, open the integrated terminal from the Command Palette with Ctrl + Shift + P on Windows or ⌘ + Shift + P on macOS, run Terminal: Create New Integrated Terminal, then run:

git checkout 1

The checkout command replaces the files in your working folder with the files from branch 1. After it finishes, VS Code should show the Flutter project folders, including lib and web.

Check the status bar now says 1 before continuing. If Git refuses to switch branches, return to the Source Control panel and commit or discard your uncommitted changes first.

Import the Material Design library

Open lib/main.dart and ensure that you have the correct import statement for Material Design components:

import 'package:flutter/material.dart';

The top of your file should look like this:

Import Material Design library

Material Design is a design system from Google. The package:flutter/material.dart library gives you access to its pre-built User Interface (UI) components, called widgets. We will use these widgets to build our user interface.

Use Copilot to explore this library further. For example, you can ask it: “What kind of widgets are available in the material.dart library? List a few examples and explain them briefly.”

For completeness, below are some general categories of fundamental building blocks in Flutter:

Clean the default code

Locate the main() function in lib/main.dart, which is the entry point of your app. You can do this with the “Go to Symbol” feature in VS Code by pressing Ctrl + Shift + O on Windows or ⌘ + Shift + O on macOS and selecting main. Let’s clear out the default MyApp class and other related classes from the app.

Comment out or remove this line for now. You can comment a line in VS Code by selecting it with your mouse and pressing Ctrl + / on Windows or ⌘ + / on macOS. The (uncommented) code in your lib/main.dart file should now look like this:

import 'package:flutter/material.dart';

void main() {}

Make sure your file looks like this now:

Cleaned main.dart file

Commit your changes (1)

Now is a good time to commit your changes. In VS Code, go to the Source Control panel. You can open this from View > Source Control. Alternatively, you can open the Command Palette by pressing Ctrl + Shift + P on Windows or ⌘ + Shift + P on macOS and enter Source Control, which will open the Source Control panel. You should see main.dart listed under changes. Type a descriptive commit message, such as Set Up the Project, and click the Commit button, followed by Sync Changes.

Alternatively, open the terminal using Ctrl + ` on Windows or ⌘ + ` on macOS and run these commands:

git add lib/main.dart
git commit -m "Set Up the Project"
git push

Define the main app widget: App

We will now define the main widget for our application.

Add the following class definition to lib/main.dart, below the main() function, on a new line after the closing curly brace (}):

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

After you’ve added the App class, optionally fold the rest of the classes and ignore them as shown in this screenshot:

Folded classes screenshot

Hopefully you are familiar with the syntax (refer to the Dart worksheets in Worksheet 1 for a refresher). We are defining a subclass of StatelessWidget class called App. App is going to be the stateless widget that serves as the main entry point for our application’s UI.

If a class extends StatelessWidget, it means its state and properties can’t change once it’s built. All widgets must have a build method, which describes the widget’s part of the user interface (what it should look like and how it should behave).

For now, the build method returns an empty Container, which is like a blank div tag in HTML. To understand this code better, select the entire class and ask Copilot the following questions by pressing Ctrl + I on Windows or ⌘ + I on macOS:

Use the App widget

Next, update the main() function to run our new App widget:

void main() {
  runApp(const App());
}

Check that your main() function now looks like this:

Main function screenshot

The runApp() function takes our App widget and makes it the root of the widget tree, displaying it on the screen. If you run the app now, you will just see a blank screen.

Commit your changes (2)

Commit your work with a meaningful message, such as Define the Main App Widget: App.

Define the UI inside App

Let’s give our App widget some structure and content.

Redefine the build method

Modify the build method within the App class as follows:

@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Sandwich Shop App',
    home: Scaffold(
      appBar: AppBar(title: const Text('Sandwich Counter')),
      body: const Center(
        child: Text('Welcome to the Sandwich Shop!'),
      ),
    ),
  );
}

Your App widget should now look like this:

App widget screenshot

Here, we’ve created a “widget tree”. MaterialApp is the root (parent), providing core app functionality. Scaffold provides the basic screen layout, including an AppBar (the top bar) and a body. The body contains a Center widget, which in turn holds our Text widget.

As before, for a deeper understanding of this structure, use Copilot to explain each widget’s role.

Run the application

Make sure you have a device selected (e.g., Chrome or Edge from the bottom status bar). You can also open the Command Palette by pressing Ctrl + Shift + P on Windows or ⌘ + Shift + P on macOS and type “Flutter: Select Device” to choose a device.

With a device selected run the app by pressing F5 (depending on your keyboard, you may have to press Fn + F5). You should see an application with an app bar titled “Sandwich Counter” and “Welcome to the Sandwich Shop!” centred on the screen.

Welcome to the Sandwich Shop

Commit your changes (3)

Commit your work with a message like Define the UI inside App.

Create the custom OrderItemDisplay widget

You can create your own reusable widgets by combining existing ones. We will create a custom widget to display a single sandwich order.

Define the OrderItemDisplay widget

Add the definition for OrderItemDisplay in lib/main.dart, placing it below the App class:

class OrderItemDisplay extends StatelessWidget {
  final String itemType;
  final int quantity;

  OrderItemDisplay(this.quantity, this.itemType, {super.key});

  @override
  Widget build(BuildContext context) {
    return Text('This is a placeholder for OrderItemDisplay');
  }
}

You may need to auto-fix any warnings by hovering over the squiggly lines and clicking on the lightbulb icon or pressing Ctrl + . on Windows or ⌘ + . on macOS. See the screenshot below:

Auto-fix warnings screenshot

This StatelessWidget has two final instance variables, itemType and quantity, which are set by its constructor (whenever an instance of OrderItemDisplay is created, the value for itemType and quantity must be provided but key is optional hence it is written in curly braces). final means they cannot be changed after the widget is created.

Running the app at this stage won’t show any visual changes yet, as we haven’t actually used the OrderItemDisplay widget in our App.

Commit your changes (4)

Commit your new widget with the message Define OrderItemDisplay custom widget.

Implement the build method of OrderItemDisplay

Now, let’s update the build method of OrderItemDisplay to show the sandwich type and quantity with emojis. We want it to display something like: “5 Footlong sandwich(es): 🥪🥪🥪🥪🥪”. (Hopefully you are familiar with the syntax used for string interpolation in Dart, if not, refer to the worksheet on Strings in Dart linked in Worksheet 1.)

Update the build method inside your OrderItemDisplay so it looks like this:

@override
Widget build(BuildContext context) {
  return Text('$quantity $itemType sandwich(es): ${'🥪' * quantity}');
}

Inside the quotes, $quantity and $itemType insert the values of those fields into the text. A longer expression needs curly braces, so ${'🥪' * quantity} works out the value of '🥪' * quantity first and then inserts it. Multiplying a string by a whole number repeats it, so '🥪' * 3 gives '🥪🥪🥪' and '🥪' * 0 gives an empty string.

Commit your changes (5)

Commit this update with a message like Implement dynamic text in OrderItemDisplay.

Use OrderItemDisplay in App

Now we can use our new custom widget inside the main App.

Replace the placeholder in App to use OrderItemDisplay

Find the build method of the App class with the shortcut (Ctrl + Shift + F on Windows or ⌘ + Shift + F on macOS) as shown below:

Find build method screenshot

Inside there, locate the Center widget within the Scaffold’s body. Select the Center widget’s child (the Text widget) as shown below:

Select Center widget screenshot

Then replace the placeholder Text widget in the body of the Scaffold with an instance of our new OrderItemDisplay widget. Your code must match the one shown below:

@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Sandwich Shop App',
    home: Scaffold(
      appBar: AppBar(title: const Text('Sandwich Counter')),
      body: const Center(
        child: OrderItemDisplay(5, 'Footlong'),
      ),
    ),
  );
}

Run the application again to see OrderItemDisplay in action

Run the app. You should now see “5 Footlong sandwich(es): 🥪🥪🥪🥪🥪” displayed in the centre of the screen.

Sandwich Counter

Commit your changes (6)

Commit your final changes for this section with the message Use OrderItemDisplay in App.

At this stage, your code should look like our code as shown on the GitHub repository.

Inspecting the Scaffold with the Widget Inspector

The Widget Inspector is a tool that comes with the Flutter SDK and lets you examine the widget tree of your running app, see the properties of each widget, and understand how your UI is structured.

Open the Widget Inspector

Make sure your app is still running (if not, press F5 to start it). Open the Command Palette with Ctrl + Shift + P on Windows or ⌘ + Shift + P on macOS, type Flutter: Open DevTools and press Enter. It will then ask you where you want to open DevTools. Choose either your web browser or inside VS Code (in its Widget Inspector page) as shown below:

Open DevTools in VS Code

For a short walkthrough of this tool, watch this YouTube video on the Widget Inspector and skim its official documentation. Make sure you watch the whole video and try using the Widget Inspector yourself on your running app.

The Flutter Widget Inspector open in VS Code

Read the widget tree of your app

In the Widget Inspector, enable “Select Widget Mode” (the mouse pointer icon at the top of the inspector) and then click on the sandwich emojis in your running app. The inspector highlights that widget and expands the tree to show it. You should see something similar to this:

The widget tree with the sandwich text selected

Take a moment to trace the tree from the top down. At the root you have the MaterialApp which provides the overall app, then the Scaffold which provides the page layout, then the AppBar holding the Text title “Sandwich Counter”, then the Center which centres its child, and finally your own OrderItemDisplay which contains a Text. Click on each of these in turn and watch the browser running the app to see how each widget is placed on the screen. When a widget is selected, the panels on the right show its size, padding and constraints. Notice how the Center takes up the whole body but its child only takes the space it needs.

The widget tree with the app bar selected

As you explore, keep the Scaffold documentation and the AppBar documentation open so you can connect what you see in the inspector to the properties in your code.

Experiment with the parts of the Scaffold

Now let’s change a few parts of the Scaffold and use the inspector to see the effect. Keep hot reload enabled (the lightning bolt icon while the app runs, or press r in the terminal) so your changes appear instantly. Make one change at a time in your App widget’s build method.

Start with the AppBar. Change its title from 'Sandwich Counter' to something else, such as 'My Sandwich Shop', and give it a background colour at the same time:

appBar: AppBar(
  title: const Text('My Sandwich Shop'),
  backgroundColor: Colors.orange,
),

Save the file (Ctrl + S on Windows or ⌘ + S on macOS), then select the AppBar in the inspector and confirm the new title and colour appear in the running app and in the tree.

Both title and backgroundColor are properties of the AppBar widget. You can view all the properties of a widget in VS Code by hovering your mouse over its name in the code. In the popup below, scroll down to find Color? backgroundColor, which shows that backgroundColor accepts a nullable Color value:

The AppBar properties shown on hover in VS Code

Next, add a floatingActionButton to the Scaffold. Put your cursor just after the body property, add a comma, then start typing floating. VS Code’s IntelliSense (the completion dropdown that lists types, methods and properties) should suggest floatingActionButton. Select it and press Enter to add it to your code:

The floatingActionButton autocomplete suggestion

There are two kinds of suggestion in VS Code. IntelliSense is the dropdown you just used: it reads your code and offers real types, methods and properties, and it needs a language extension (the Dart and Flutter extensions provide it here). Ghost text is the greyed-out AI suggestion that predicts whole lines as you type; it comes from GitHub Copilot, which needs you signed into VS Code with a Copilot subscription (free through GitHub Education). For now, please use IntelliSense only.

As the value of the floatingActionButton property, start typing Floating and select FloatingActionButton from the suggestions. Leave its onPressed property as an empty function (() {}). The onPressed property specifies the callback function that runs when the button is pressed, and () {} is a function that takes no parameters (()) and has an empty body ({}), so pressing the button does nothing for now. Your code should look like this:

The FloatingActionButton with an empty onPressed callback

Lastly, add a child property inside the FloatingActionButton. Type Icons followed by a dot (.) to see the list of available icons, then find the add icon and press Enter to select it:

Selecting the add icon for the FloatingActionButton child

Your completed Scaffold should now include the floatingActionButton:

body: const Center(
  child: OrderItemDisplay(5, 'Footlong'),
),
floatingActionButton: FloatingActionButton(
  onPressed: () {},
  child: const Icon(Icons.add),
),

A round button now appears in the bottom right corner. Select it in the inspector and notice that it sits in the tree separately from the body.

As a final test, temporarily delete the Center so the body is just the OrderItemDisplay, and observe how the text jumps to the top left of the body:

The OrderItemDisplay positioned at the top left with no Center

Put the Center back afterwards (you can undo with Ctrl + Z on Windows or ⌘ + Z on macOS). Once you are done, return your App widget to the version at the end of Use OrderItemDisplay in App, with a plain AppBar, a Center and no floating action button.

Commit your changes (7)

Commit any tidy-up with a message like Restore Center after inspecting the Scaffold.

Making the app interactive with Stateful widgets

So far every widget we have written has been a StatelessWidget. That is fine for a fixed display, but a real app needs to respond to the user. In this section we will let the user add and remove sandwiches by pressing buttons, so the number of 🥪 emojis on the screen goes up and down.

Stateless versus Stateful

State is simply data that can change while the app is running. A StatelessWidget is immutable, which means that once it is built its properties cannot change. It is like a photograph: a snapshot of the user interface at one moment. Our OrderItemDisplay is stateless because it only ever shows the values passed into its constructor. A StatefulWidget, on the other hand, can hold data that changes over time and rebuilds itself to show the new data.

The kind of state we use here is called ephemeral state, which is data that lives inside a single widget, such as the current number of sandwiches in the order. In a later worksheet we will meet app state, which is shared across many widgets, such as the login information of a user.

If you would like a short explanation before coding, read the StatelessWidget documentation and the StatefulWidget documentation, and watch the Flutter team’s short videos on StatelessWidget and StatefulWidget. Optionally, check out Flutter’s guide to adding interactivity.

Add the Add and Remove buttons

Let’s add two buttons below the sandwich display. Jump to your App widget in main.dart (use Ctrl + Shift + O on Windows or ⌘ + Shift + O on macOS, then search for App). Select the current content of the body property of the Scaffold (it should be set to Center(child: OrderItemDisplay(...))).

Delete it and start typing Column, press enter to select a Column widget from the IntelliSense dropdown (the completion list that shows types, methods and properties). Inside the brackets, type main and select mainAxisAlignment from the dropdown, as shown in the image below:

Main Axis Alignment

This property, used in Row and Column widgets, determines how the children are aligned along the main axis. In a Column, the main axis runs vertically, and in a Row, it runs horizontally. The values of mainAxisAlignment are set to MainAxisAlignment constants, such as MainAxisAlignment.start, MainAxisAlignment.center, and MainAxisAlignment.end. Set it to MainAxisAlignment.center as shown in the image below:

Main Axis Alignment Center

You should get comfortable with IntelliSense and always select what you need from the dropdown instead of typing it out, as it helps you write code quickly. Next, add a children property to the Column. Inside its square brackets ([]), place two children: an instance of OrderItemDisplay followed by a Row widget. The children are items in a list, so separate them with a comma.

Column with Children

Inside the Row, add another list of children ([]) to hold the buttons. Each button is an instance of ElevatedButton, and they should be separated by a comma. Your code should look like this:

Row with Buttons

An ElevatedButton typically contains a Text widget for its label. We want two of them, “Add” and “Remove”. Each also takes an onPressed callback that handles user interactions; for now we simply print a message to the terminal when the button is pressed. Update your code so it looks like this:

Elevated Buttons

Ignore the warning about calling print in production code for now. Your completed body should look like this:

Column with Row and Buttons

body: Center(
  child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      const OrderItemDisplay(5, 'Footlong'),
      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
            onPressed: () => print('Add button pressed!'),
            child: const Text('Add'),
          ),
          ElevatedButton(
            onPressed: () => print('Remove button pressed!'),
            child: const Text('Remove'),
          ),
        ],
      ),
    ],
  ),
),

To fix any indentation, open the Command Palette and run Format Document, or run Preferences: Open Settings (UI) and enable “format on save” so this happens automatically.

The important property of an ElevatedButton is onPressed. It takes a function that runs when the button is tapped, which is called an event handler or a callback. Our callbacks are arrow functions that print a message to the terminal rather than changing the UI. Run the app with flutter run and click the buttons; the messages should appear in the terminal.

The two buttons currently sit right next to each other. To add a gap between them, place a SizedBox with a fixed width between the two ElevatedButton widgets in the Row:

ElevatedButton(
  onPressed: () => print('Add button pressed!'),
  child: const Text('Add'),
),
const SizedBox(width: 16),
ElevatedButton(
  onPressed: () => print('Remove button pressed!'),
  child: const Text('Remove'),
),

A SizedBox is an empty box of a fixed size, so it is a simple way to space widgets apart. Try changing the width to see the gap grow and shrink. You can remove this SizedBox again afterwards, as the rest of the worksheet does not depend on it.

Commit your changes (8)

Commit your work with a message like Add Add and Remove buttons.

Define the OrderScreen stateful widget

The quantity in OrderItemDisplay(5, 'Footlong') is hardcoded, so it can never change. To make it interactive we need a widget that can hold a changing value. That is what a StatefulWidget is for.

Use the fold buttons to fold App and OrderItemDisplay. Put your cursor between them and type class. Select the boilerplate for a new class and replace its name ClassName with OrderScreen.

Folding App and OrderItemDisplay

After the name of the class (OrderScreen), add the extends StatefulWidget part to indicate that this is a subclass of the StatefulWidget class (Simply put, we want to say that OrderScreen is a widget that will have mutable state).

Next, write a maxQuantity instance variable for the OrderScreen widget. This variable, which should be an int, will hold the maximum quantity of the order item that can be selected. It should be marked as final because it will not change after the widget is created.

Write a constructor for the OrderScreen widget too that takes an optional maxQuantity parameter (inside curly braces {}). Give this parameter a default value of 10 ({this.maxQuantity = 10}). Constructors for widgets also need an optional parameter super.key to pass the key to the parent class (you don’t need to worry about this for now).

Your code should look like this:

OrderScreen widget boilerplate

Fix the blue squiggly lines (info warnings) by adding the const keyword. The red squiggly lines (errors) indicate that every StatefulWidget must have a createState method. Feel free to click on the Explain button to have Copilot explain the error for you:

OrderScreen createState error

Hover your mouse over the error to see the suggested fix (or use Ctrl + . on Windows or ⌘ + . on macOS to apply it). Your code should look like this now:

OrderScreen createState fixed

Every StatefulWidget must implement the createState method that returns a State object. It is basically a way to tell Flutter which state class is associated with this widget. And yes, we need a separate State class.

So put your mouse outside of the curly braces of the OrderScreen class and type class. Call it _OrderScreenState (this is a private state class, hence the _ prefix) and make it extend State<OrderScreen>. Add a private integer instance variable called _quantity (again, the _ indicates that it is private member and should not be accessed directly from outside the class). Our _quantity variable will be in there to hold the current quantity of the order item. Your code should look like this:

OrderScreen state class boilerplate

You should see an error indicating that the build method is missing in the _OrderScreenState class. This is expected because every State class must implement the build method to describe how to display the widget. Use the Quick Fix options to generate the build method, so your code should look like this:

OrderScreen build method fixed

Remove the to do comment and the throw UnimplementedError(); line from the createState method of OrderScreen and replace it with return _OrderScreenState(); (we want to return an instance of the state class). And remove the throw UnimplementedError(); line from the build method of _OrderScreenState as well and replace it with a Placeholder widget (or your actual UI later). This is what your code should look like now:

class OrderScreen extends StatefulWidget {
  final int maxQuantity;

  const OrderScreen({super.key, this.maxQuantity = 10});

  @override
  State<OrderScreen> createState() {
    return _OrderScreenState();
  }
}

class _OrderScreenState extends State<OrderScreen> {
  int _quantity = 0;

  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}

You may see a warning that _quantity is unused or could be final. Ignore it for now.

This looks unusual because we have two classes for one widget, but this is how Flutter separates the immutable configuration from the mutable state. The OrderScreen class is the StatefulWidget and holds the configuration that does not change, such as maxQuantity, and it has a createState() method. The _OrderScreenState class is the State and holds the data that does change (_quantity) along with the build method. The underscore in _OrderScreenState and _quantity makes them private to the file.

To understand this structure, select it in your editor and ask Copilot (Ctrl + I on Windows or ⌘ + I on macOS) questions such as what the difference is between a StatefulWidget and a State object, why the build method lives inside the State class rather than the StatefulWidget class, and what the underscore prefix on _OrderScreenState and _quantity means in Dart.

Commit your changes (9)

Commit your work with a message like Define OrderScreen stateful widget.

Build the UI for OrderScreen

Now let’s move the UI into _OrderScreenState. Replace the Placeholder() in its build method with the Scaffold we built earlier (copy and paste the code inside the build method of the App widget), the code is shown below for completeness. This should look familiar:

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Sandwich Counter'),
    ),
    body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          OrderItemDisplay(
            _quantity,
            'Footlong',
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () => print('Add button pressed!'),
                child: const Text('Add'),
              ),
              ElevatedButton(
                onPressed: () => print('Remove button pressed!'),
                child: const Text('Remove'),
              ),
            ],
          ),
        ],
      ),
    ),
  );
}

Your code should look like this now:

OrderScreen UI: Screenshot of OrderScreen showing the scaffold with the app bar and buttons

Notice that we now pass _quantity (which starts at 0) into OrderItemDisplay instead of the hardcoded 5. The State object can read its own variables directly.

Use OrderScreen inside App

Update the App widget to use OrderScreen as its home. In VS Code, you can click on the opening round bracket of the Scaffold in the App widget to select the whole widget and see where it ends (see the screenshot below, all of this needs to be replaced with OrderScreen(maxQuantity: 5).

Select Scaffold in VS Code: Screenshot showing how to select the entire Scaffold widget in VS Code

Your App widget should now look like this:

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Sandwich Shop App',
      home: OrderScreen(maxQuantity: 5),
    );
  }
}

maxQuantity is an optional named parameter with a default of 10, so we could also write OrderScreen(). Here we set it to 5.

Run the app. You should see “0 Footlong sandwich(es):” with no emojis yet, and two buttons that print to the terminal but do not change the display. That is the problem we fix next. This is what your app should look like now:

Screenshot of OrderScreen showing zero sandwiches and the two buttons

Commit your changes (10)

Commit your work with a message like Use OrderScreen in App.

Add and remove emojis with setState

The last step is to make the buttons actually change _quantity. Put your cursor inside the curly braces of the _OrderScreenState class, you may want to fold the build method to make it easier to see. Type in fun and VS Code will suggest function snippets and the boilerplate for a method will appear as shown below:

Function snippet in VS Code: Screenshot showing the function snippet for adding a new method in VS Code

Change name to _increaseQuantity for the first method and make a second method called _decreaseQuantity right below it. Remove the params from both methods as we don’t want our methods to take any arguments. Keep void as it is (our methods will not return any value). Then put your cursor inside the curly braces of increaseQuantity and write if to start an if-statement as show below:

If snippet in VS Code: Screenshot showing the if-statement snippet for adding a new method in VS Code

Update the code so that you check that _quantity is less than widget.maxQuantity in _increaseQuantity and greater than 0 in _decreaseQuantity. If these conditions are met, call setState method of the _OrderScreenState class to update the UI. Update the code so it matches what is shown below:

void _increaseQuantity() {
  if (_quantity < widget.maxQuantity) {
    setState(() => _quantity++);
  }
}

void _decreaseQuantity() {
  if (_quantity > 0) {
    setState(() => _quantity--);
  }
}

There are two things to notice here. The first is widget.maxQuantity, which lets the State read the maxQuantity from its OrderScreen. The State reaches its widget through the built-in widget property (see the State.widget documentation). To verify this, hold Ctrl on Windows or ⌘ on macOS and click on maxQuantity in the if-statement of _increaseQuantity to jump to its declaration in the OrderScreen class:

Max quantity declaration: Screenshot showing the maxQuantity declaration in the OrderScreen class

The second, and the most important, is setState(). You call it to tell Flutter that a value has changed, and Flutter then runs build() again and redraws the UI with the new _quantity. If you wrote _quantity++ without wrapping it in setState(), the number would change in memory but the screen would not update.

_quantity++ adds one to _quantity, and _quantity-- subtracts one; they are short for _quantity = _quantity + 1 and _quantity = _quantity - 1. The code we pass to setState is a small function with no name. () => _quantity++ is the short arrow form of a function that runs a single statement. The longer form below does exactly the same thing, and we use this longer form in later worksheets because it can hold more than one statement:

setState(() {
  _quantity++;
});

Now connect the buttons to these methods. Replace the two onPressed callbacks in the build method of _OrderScreenState so they call the new functions instead of printing:

ElevatedButton(
  onPressed: _increaseQuantity,
  child: const Text('Add'),
),
ElevatedButton(
  onPressed: _decreaseQuantity,
  child: const Text('Remove'),
),

Run the app and try it. It starts at “0 Footlong sandwich(es):” with no emojis. Each time you press Add the count goes up and a 🥪 appears, and each time you press Remove the count goes down and a 🥪 disappears, stopping at 0. If you keep pressing Add past 5 nothing happens, because we set maxQuantity to 5.

Screenshot of OrderScreen after pressing Add several times, showing emojis

Commit your changes (11)

Commit your final changes with a message like Add interactivity with setState.

Exercises

These exercises apply to your Southsea Cinema coursework. Together, the exercises in Worksheet 1 and Worksheet 2 form Demo 1. See the Southsea Cinema coursework brief for the assessment requirements. Commit after each exercise.

The fork of the southsea_cinema repo that you have already made (in Worksheet 1) has a lib/views folder. This folder contains two pages. home_view.dart is the home page shown when the app starts, and movie_listing.dart is the movie listing page you will build.

When the app runs, you reach the listing page from the home page through the navigation drawer (the menu icon in the app bar). We will cover navigation and multiple pages properly in a later worksheet, so for now you only need to work on the listing page.

The movie_listing.dart page should look like this:

The starting movie listing page in the template

You can reach this page from the navigation drawer, by opening the menu in the app bar and choosing “Movie Listing”:

Opening the movie listing page from the navigation drawer

Open lib/views/movie_listing.dart. At this point, MovieListing is a StatelessWidget, and the body of its Scaffold contains an empty SizedBox:

import 'package:flutter/material.dart';
import 'package:southsea_cinema/constants.dart';
import 'package:southsea_cinema/widgets/nav_drawer.dart';

class MovieListing extends StatelessWidget {
  const MovieListing({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(appTitle, style: cinemaHeaderStyle),
        backgroundColor: cinemaSurface,
        iconTheme: const IconThemeData(color: cinemaBrand),
        elevation: 0,
      ),
      drawer: const NavDrawer(),
      body: const SizedBox.shrink(),
    );
  }
}

Your task is to update movie_listing.dart to show a single film. Use this example listing as a reference point:

The example film listing on the Southsea Cinema website

Choose your own favourite film and include its title, runtime, age rating and description, a ticket quantity dropdown and a booking button. Run your app on a phone-sized view, as you did in Worksheet 1.

  1. Wrap the existing SizedBox in a Container. A Container holds a single child and can give it a size, a colour, a border and more.

    Place the cursor on SizedBox in body: const SizedBox.shrink(). Click the lightbulb icon, or press Ctrl + . on Windows or ⌘ + . on macOS, and choose Wrap with Container, as shown below:

    The Wrap with Container action for SizedBox in VS Code

    The Container should now be the body of the Scaffold, with the SizedBox as its child:

     body: Container(
       child: const SizedBox.shrink(),
     ),
    

    If you need help, see the widget fundamentals tutorial.

  2. Decide whether the film title, description and booking controls should appear above one another or side by side. Which layout widget fits each arrangement: Column or Row? For a vertical listing, replace the SizedBox.shrink() inside your Container with a Column, then add the film title and a short description as Text widgets in its children list.

    Add an ElevatedButton labelled “Add to order” below the text. Remove const before the Column if present, because the callback is not constant. For now, print a message when the button is pressed:

     ElevatedButton(
       onPressed: () {
         print('Add to order button pressed');
       },
       child: const Text('Add to order'),
     ),
    

    Keep MovieListing stateless for now. Run the app and press the button to check the terminal output; printing alone does not need setState.

  3. Let the user choose how many tickets they want with a single DropdownMenu, offering quantities up to 5. You should try to match the screenshot shown above.

    Place the dropdown in your Column widget’s children list, above the booking button. Initially, use print(value); inside its onSelected callback to check the selected quantity in the terminal.

    Before using the selected quantity to update text on the page, convert MovieListing to a StatefulWidget. Select StatelessWidget in its class declaration, open the lightbulb menu with Ctrl + . on Windows or ⌘ + . on macOS, and choose Convert to StatefulWidget, as shown below:

    The Convert to StatefulWidget action in the MovieListing class declaration in VS Code

    VS Code creates _MovieListingState and moves build into it. Use this action rather than only changing the superclass name.

    Declare int _ticketQuantity = 1; inside _MovieListingState, above build. Update the dropdown’s callback to store the quantity inside setState, using the following pattern:

     DropdownMenu<int>(
       initialSelection: _ticketQuantity,
       onSelected: (int? value) {
         if (value != null) {
           setState(() {
             _ticketQuantity = value;
             print('Tickets selected: $_ticketQuantity');
           });
         }
       },
       dropdownMenuEntries: [
         DropdownMenuEntry(value: 1, label: '1'),
         DropdownMenuEntry(value: 2, label: '2'),
         DropdownMenuEntry(value: 3, label: '3'),
         DropdownMenuEntry(value: 4, label: '4'),
         DropdownMenuEntry(value: 5, label: '5'),
       ],
     ),
    

    int? allows null, so check the value before assigning it. Add Text('Tickets selected: $_ticketQuantity') below the dropdown and check that its text changes when you select a quantity.

    See the DropdownMenu documentation if you need more detail.

  4. Update your “Add to order” button so it gives the user feedback on the page rather than only printing a message. There is no basket yet, so add a Text widget below the button to display how many tickets were added to the order.

    Declare String _bookingMessage = ''; above build in _MovieListingState and display it with Text(_bookingMessage). In onPressed, use setState to assign 'Added $_ticketQuantity tickets to the order' to _bookingMessage. You can also print the message inside that callback. Select a quantity and press the button to check that the feedback matches.

  5. (Advanced) With all the pieces in place, update the movie_listing.dart page so that it resembles the screenshot above and the home page of the provided template. Reuse the colours and text styles from lib/constants.dart the same way home_view.dart does.

    For example, you can apply cinemaHeaderStyle directly to your title Text widget (style: cinemaHeaderStyle), or define custom text styles using colours such as cinemaFontWhite and cinemaFontMuted.

    Use ElevatedButton.styleFrom to apply the brand colours and squared corners to your booking button:

     style: ElevatedButton.styleFrom(
       backgroundColor: cinemaBrand,
       foregroundColor: cinemaFontWhite,
       shape: const RoundedRectangleBorder(
         borderRadius: BorderRadius.zero,
       ),
     ),
    
  6. (Advanced) Make your page adapt to the width of the window to prevent layout overflows.

    A Row can overflow on narrow screens. A LayoutBuilder provides constraints.maxWidth, so you can choose a Row on wide screens and a Column on narrow ones.

    In your sandwich shop app, for example, you can wrap the quantity buttons in a LayoutBuilder to switch between a Row and a Column:

     LayoutBuilder(
       builder: (context, constraints) {
         if (constraints.maxWidth > 600) {
           return Row(
             mainAxisAlignment: MainAxisAlignment.center,
             children: [
               ElevatedButton(
                 onPressed: _increaseQuantity,
                 child: const Text('Add'),
               ),
               ElevatedButton(
                 onPressed: _decreaseQuantity,
                 child: const Text('Remove'),
               ),
             ],
           );
         } else {
           return Column(
             children: [
               ElevatedButton(
                 onPressed: _increaseQuantity,
                 child: const Text('Add'),
               ),
               ElevatedButton(
                 onPressed: _decreaseQuantity,
                 child: const Text('Remove'),
               ),
             ],
           );
         }
       },
     ),
    

    Resize the browser window and check that your page works at phone widths without overflowing.