Sub topic 2
Cupertino routes, swipe-back and tab scaffolds
This chapter assumes you know Navigator, BuildContext, and the Cupertino basics from earlier chapters. It covers navigation, modal popups, pickers, colors, mixing libraries, and platform checks.
Routes and swipe-back
CupertinoPageRoute is a route that slides in from the right, the iOS style. Pushing a page with it enables the left-edge swipe-back gesture automatically.
Navigator.of(context).push(
CupertinoPageRoute(builder: (_) => const DetailPage()),
);Tab scaffolds
CupertinoTabScaffold pairs a bottom CupertinoTabBar with one page per tab. Each tab keeps its own navigation stack.
CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: const [
BottomNavigationBarItem(icon: Icon(CupertinoIcons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(CupertinoIcons.settings), label: 'Settings'),
],
),
tabBuilder: (context, index) => const CupertinoPageScaffold(
child: Center(child: Text('Tab content')),
),
)Alert dialogs and action sheets
showCupertinoDialog shows a CupertinoAlertDialog. showCupertinoModalPopup shows a CupertinoActionSheet from the bottom of the screen.
showCupertinoModalPopup<void>(
context: context,
builder: (context) => CupertinoActionSheet(
title: const Text('Share'),
actions: [
CupertinoActionSheetAction(
onPressed: () => Navigator.pop(context),
child: const Text('Copy link'),
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
),
);Pickers and date pickers
CupertinoDatePicker is a wheel control. Show it inside a modal popup, with a height near 216 logical pixels to match the iOS picker. Its month and day names need GlobalCupertinoLocalizations.delegate in the MaterialApp (see below).
System colors and dark mode
Use CupertinoColors.label and CupertinoColors.systemBackground instead of fixed values. These colors change with light and dark mode. To read the resolved value, call CupertinoDynamicColor.resolve(CupertinoColors.label, context).
Cupertino inside a MaterialApp
You can place Cupertino widgets inside a MaterialApp. Add the Cupertino localizations delegate, or date pickers will fail to load their labels:
MaterialApp(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: const HomePage(),
)Detecting the platform
Read Theme.of(context).platform rather than dart:io's Platform. The theme value works on web and in tests, and tests can override it.
final platform = Theme.of(context).platform;
final isIOS = platform == TargetPlatform.iOS || platform == TargetPlatform.macOS;Exercise
A screen uses MaterialPageRoute, and you want iOS users to get swipe-back. Which change do you make, and why does it not require a platform check?
Show answer
Replace MaterialPageRoute with CupertinoPageRoute. The swipe gesture comes from the route, so you do not need a platform check at the call site.