Sub topic 3
Screens with CupertinoPageScaffold and CupertinoNavigationBar
This chapter assumes you can write StatelessWidget and StatefulWidget classes. It also assumes you know MaterialApp with Scaffold, and that you have used Navigator.push and BuildContext. Here you will build iOS-style screens: how the two core widgets cooperate, how the bar behaves during navigation, and which mistakes show up most often.
What the scaffold does
CupertinoPageScaffold is the Cupertino counterpart of Scaffold, with a much smaller job. It has two slots, an optional navigationBar at the top and a required child for the content. It has no drawer, no floating action button and no snack bar support. iOS has no such concepts, so the widget leaves them out.
The scaffold's main properties:
| Property | Purpose | Default |
|---|---|---|
| `navigationBar` | Top bar, for example `CupertinoNavigationBar` | none |
| `child` | The screen content | required |
| `backgroundColor` | Page background | `CupertinoTheme` scaffold background color, which adapts to dark mode |
| `resizeToAvoidBottomInset` | Shrinks the content when the keyboard opens | `true` |
A two-screen example
The example below builds a settings list with a large title and a detail page. It shows the navigation bar in both of its forms.
import 'package:flutter/cupertino.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoApp(home: SettingsPage());
}
}
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: CustomScrollView(
slivers: [
const CupertinoSliverNavigationBar(
largeTitle: Text('Settings'),
),
SliverToBoxAdapter(
child: CupertinoListSection.insetGrouped(
children: [
CupertinoListTile(
title: const Text('Account'),
trailing: const CupertinoListTileChevron(),
onTap: () => Navigator.of(context).push(
CupertinoPageRoute<void>(
title: 'Settings',
builder: (_) => const AccountPage(),
),
),
),
],
),
),
],
),
);
}
}
class AccountPage extends StatelessWidget {
const AccountPage({super.key});
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: const Text('Account'),
trailing: CupertinoButton(
padding: EdgeInsets.zero,
onPressed: () => Navigator.of(context).pop(),
child: const Text('Done'),
),
),
child: const SafeArea(
child: Center(child: Text('Account details')),
),
);
}
}Run it and tap Account. The new page slides in from the right, and a back button labelled "Settings" appears at the left of the bar. You did not build that button. The next sections explain where it comes from.
The regular navigation bar
CupertinoNavigationBar is a fixed bar, 44 logical pixels tall plus the status bar inset. It has three slots, laid out the way iOS lays them out.
leadingis the left slot. If you leave it empty, the bar fills it for you (see below).middleis the centered title. It is usually aText.trailingis the right slot. It normally holds one or twoCupertinoButtons with zero padding.
The bar also has a few configuration properties:
backgroundColorsets the bar color. The default is the theme's bar color, which is translucent.borderdraws the hairline under the bar. Passnullto remove it.automaticallyImplyLeadingcontrols the automatic back button. It defaults totrue.previousPageTitlesets the text of the back label.
The automatic back button
When leading is null and automaticallyImplyLeading is true, the bar checks whether the current route can pop. If it can, the bar inserts a CupertinoNavigationBarBackButton. The label comes from one of three places, in this order:
- The
previousPageTitleyou pass to the bar. - The
titleof the previous route, if that route is aCupertinoPageRoute. - The word "Back", used when neither is available or the title is too long to fit.
That is why title: 'Settings' on the CupertinoPageRoute in the example produces a "Settings" label on the detail page. If the previous page uses a MaterialPageRoute, or a route with no title, you must set previousPageTitle yourself.
The same logic applies to the middle slot. With automaticallyImplyMiddle true and no middle given, the bar shows the route's title.
Large titles with CupertinoSliverNavigationBar
iOS settings-style screens show a big title that shrinks into the bar as you scroll. In Flutter this is CupertinoSliverNavigationBar. Because it is a sliver, it must be a child of a scroll view such as CustomScrollView, and you place it inside the scaffold's child instead of using the scaffold's navigationBar slot. The first screen in the example does exactly this.
The largeTitle widget is shown in large type while the list is at the top. When the user scrolls, it collapses into a normal-size title in the bar. If you also set middle, that widget is what the collapsed bar displays. Otherwise the bar reuses largeTitle.
Translucency, padding and SafeArea
The default navigation bar background is semi-transparent, with a blur behind it. Content scrolled underneath shows through, which matches iOS. This changes how the scaffold treats the child:
- If the bar is translucent, the child fills the whole screen and extends underneath the bar. The scaffold does not move it down.
- If the bar is fully opaque, the child is placed below the bar.
In both cases the scaffold adjusts the MediaQuery padding it gives to the child, so the top padding includes the bar height. A SafeArea around your content therefore does the right thing in either case. Without it, the first lines of text sit behind the bar.
To make the bar opaque, give it a solid color:
import 'package:flutter/cupertino.dart';
class OpaqueBarPage extends StatelessWidget {
const OpaqueBarPage({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text('Opaque'),
backgroundColor: CupertinoColors.systemBackground,
),
child: SafeArea(
child: Align(
alignment: Alignment.topCenter,
child: Text('Starts below the bar'),
),
),
);
}
}CupertinoColors.systemBackground is a dynamic color. It resolves to white in light mode and black in dark mode, so the bar stays correct when the system theme changes. Dynamic colors are covered in a later chapter.
Hero transitions between bars
By default transitionBetweenRoutes is true. When you push a route whose page also has a CupertinoNavigationBar, the title and back button animate between the two bars, using a Hero internally. This is the smooth "title slides into the back label" effect you see on iOS.
It causes one frequent error. A Hero needs a unique tag within a route, and all navigation bars share a default tag. If one route shows two navigation bars at once (for example, nested navigators in a tab layout), Flutter throws a duplicate Hero tag error. Fix it in one of two ways:
- Set
transitionBetweenRoutes: falseon one of the bars. - Give each bar a distinct
heroTag.
Common errors
- Content hidden under the bar. The child is not wrapped in
SafeArea, and the bar is translucent. Wrap the child, or useSliverSafeAreainside aCustomScrollView. - "No Material widget found". You used a Material widget such as
TextField,ListTileorInkdirectly under aCupertinoApp. Use the Cupertino version (CupertinoTextField,CupertinoListTile), or wrap that part in aMaterial. Mixing is covered in a later chapter. - Two back buttons or none. Setting
leadingyourself disables the automatic back button. If you provide a customleading, you are responsible for popping. Also check that the page was pushed on a navigator, because a first page has nothing to pop. - Wrong back label. The previous route is not a
CupertinoPageRoutewith a title. SetpreviousPageTitle. - Keyboard covers the input.
resizeToAvoidBottomInsetwas set tofalse, or the input sits in a view that does not scroll. Leave the default and use a scrollable child.
Exercise
You push a page built with MaterialPageRoute, and its CupertinoNavigationBar(middle: Text('Detail')) shows a back button labelled "Back" instead of the previous screen's name "Inbox". Which property fixes this, and why does the label not appear automatically?
Show answer
Set previousPageTitle: 'Inbox' on the CupertinoNavigationBar. The automatic label only reads the title from a previous route that is a CupertinoPageRoute. A MaterialPageRoute has no such title, so the bar falls back to "Back" unless you provide the text yourself.
Exercise
A screen has CupertinoPageScaffold(navigationBar: CupertinoNavigationBar(middle: Text('Feed')), child: Text('First line')). The first line is partly hidden behind the bar. What are two ways to fix it without changing the bar's look?
Show answer
Wrap the child in SafeArea, because the scaffold adds the bar height to the MediaQuery top padding. Alternatively, give the bar an opaque backgroundColor, such as CupertinoColors.systemBackground, so the scaffold places the child below it.