Sub topic 5
Cupertino routes, swipe-back and tab scaffolds
Assume you know basic Dart syntax, StatelessWidget, StatefulWidget, setState, BuildContext, and Navigator. You should also be familiar with CupertinoApp and CupertinoPageScaffold.
Cupertino navigation has two main parts:
CupertinoPageRoutepresents a page with an iOS-style transition.CupertinoTabScaffoldcreates a tab bar where each tab can maintain its own navigation history.
Pushing pages with CupertinoPageRoute
Use CupertinoPageRoute when a page should slide in from the right. On iOS, the user can usually swipe from the left edge to return to the previous page.
import 'package:flutter/cupertino.dart';
void main() {
runApp(
const CupertinoApp(
home: HomePage(),
),
);
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('Home'),
),
child: Center(
child: CupertinoButton.filled(
onPressed: () {
Navigator.of(context).push(
CupertinoPageRoute(
builder: (context) => const DetailsPage(),
),
);
},
child: const Text('Open details'),
),
),
);
}
}
class DetailsPage extends StatelessWidget {
const DetailsPage({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text('Details'),
),
child: Center(child: Text('Detail content')),
);
}
}Navigator.push adds a route to the navigation stack. Navigator.pop removes the current route. Avoid replacing CupertinoPageRoute with a custom route unless you need different transition behavior.
Building tabs with independent navigation
CupertinoTabScaffold combines a tab bar with tab content. Each CupertinoTabView owns a separate navigator. This means a details page opened in one tab does not change the navigation stack of another tab.
class RootPage extends StatelessWidget {
const RootPage({super.key});
@override
Widget build(BuildContext context) {
return CupertinoTabScaffold(
tabBar: const CupertinoTabBar(
items: [
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.house),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.gear),
label: 'Settings',
),
],
),
tabBuilder: (context, index) {
if (index == 0) {
return const CupertinoTabView(
builder: (_) => HomeTab(),
);
}
return const CupertinoTabView(
builder: (_) => SettingsTab(),
);
},
);
}
}Place a CupertinoTabView around every tab root. Without it, the tab content does not get the tab-specific navigator that preserves its route history.
Common navigation errors
- Calling
Navigator.of(context).pushwith a context above the intendedCupertinoTabViewcan push onto the wrong navigator. - Calling
Navigator.popwhen the tab stack contains only its root page can exit the tab or app unexpectedly. - Resetting a tab’s
CupertinoTabViewduring every rebuild can lose its navigation state. Keep tab roots stable. - A swipe-back gesture is controlled by the route and platform conventions. Do not depend on it as the only way to navigate back; provide normal back navigation too.
Exercise
Why does each tab commonly use its own CupertinoTabView?
Show answer
CupertinoTabView supplies an independent navigator, so each tab can preserve its own route stack while the user switches tabs.