Sub topic 2
Starting an app with CupertinoApp and CupertinoThemeData
This chapter assumes you can write StatelessWidget and StatefulWidget classes in Dart, you know what MaterialApp and Scaffold do, and you have used Navigator and BuildContext. It covers the root of a Cupertino app: what CupertinoApp sets up, how CupertinoThemeData controls appearance, and the errors that appear when you leave the Material world.
What CupertinoApp provides
CupertinoApp is the Cupertino counterpart of MaterialApp. Both wrap the lower-level WidgetsApp, which supplies the Navigator, route handling, MediaQuery, localization plumbing and the title. What differs is the design layer on top.
| Concern | MaterialApp | CupertinoApp |
|---|---|---|
| Theme object | `ThemeData` via `Theme` | `CupertinoThemeData` via `CupertinoTheme` |
| Default page transition | Platform-dependent Material transition | Right-to-left slide with swipe-back |
| Default scroll behavior | `MaterialScrollBehavior` | `CupertinoScrollBehavior` (bouncing) |
| Dark theme | Separate `darkTheme` argument | No `darkTheme`. Brightness comes from the theme or the system |
| Default localizations | `DefaultMaterialLocalizations` | `DefaultCupertinoLocalizations` |
| `ScaffoldMessenger`, ink splashes | Included | Not included |
The last row matters in practice. CupertinoApp has no Material ancestor widget, so widgets that need one fail until you provide it. That is covered under common errors below.
A minimal app
This is the smallest useful Cupertino app. It sets a theme, a home page, a navigation bar and a filled button.
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(
theme: CupertinoThemeData(primaryColor: CupertinoColors.systemIndigo),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(middle: Text('Settings')),
child: SafeArea(
child: Center(
child: CupertinoButton.filled(
onPressed: () {},
child: const Text('Save'),
),
),
),
);
}
}Three things to notice:
- The only import is
package:flutter/cupertino.dart. It exports the core widgets library, soText,Center,SafeAreaandNavigatorare available. primaryColoris set once.CupertinoButton.filleduses it as its background, andCupertinoNavigationBaruses it for tappable actions such as back buttons.CupertinoPageScaffoldis the Cupertino counterpart ofScaffold. The next chapter covers it in detail. Here it only gives the page a background and a place for the bar.
Routing options
CupertinoApp accepts the same routing arguments as MaterialApp: home, routes, initialRoute, onGenerateRoute and onUnknownRoute. For declarative routing packages such as go_router, use CupertinoApp.router, which takes routerConfig or a routerDelegate and routeInformationParser. Do not mix home or routes with the .router constructor.
Routes registered through routes: or onGenerateRoute are wrapped in CupertinoPageRoute by default. That gives you the iOS slide transition and the edge swipe-back gesture without extra code. Later chapters cover customizing it.
How CupertinoThemeData works
CupertinoThemeData is smaller than ThemeData. It has a handful of top-level properties:
brightness: forcesBrightness.lightorBrightness.dark. When it isnull, the app follows the system setting throughMediaQuery.platformBrightnessOf.primaryColor: the accent color for interactive elements. The default isCupertinoColors.activeBlue.primaryContrastingColor: the color drawn on top of the primary color, such as the label of a filled button.scaffoldBackgroundColor: the page background used byCupertinoPageScaffold.barBackgroundColor: the background of navigation and tab bars.textTheme: aCupertinoTextThemeDataholding the text styles.applyThemeToAll: whentrue, the theme also applies to Cupertino widgets that are not descendants of aCupertinoApp, which is useful when mixing frameworks.
Any property you leave out falls back to an iOS-like default. The defaults are resolved lazily, which is why a theme with only primaryColor set still has correct text styles and bar colors.
import 'package:flutter/cupertino.dart';
void main() => runApp(const ThemedApp());
class ThemedApp extends StatelessWidget {
const ThemedApp({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoApp(
debugShowCheckedModeBanner: false,
theme: CupertinoThemeData(
primaryColor: CupertinoColors.systemIndigo,
scaffoldBackgroundColor: CupertinoColors.systemGroupedBackground,
barBackgroundColor: CupertinoColors.systemBackground,
textTheme: CupertinoTextThemeData(
navTitleTextStyle: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w700,
color: CupertinoColors.label,
),
),
),
home: ThemeProbe(),
);
}
}
class ThemeProbe extends StatelessWidget {
const ThemeProbe({super.key});
@override
Widget build(BuildContext context) {
final theme = CupertinoTheme.of(context);
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(middle: Text('Theme')),
child: SafeArea(
child: Center(
child: Text(
'Primary color text',
style: theme.textTheme.textStyle.copyWith(color: theme.primaryColor),
),
),
),
);
}
}CupertinoTheme.of(context) is the way to read the theme from any descendant. It returns a theme already resolved against the current brightness, so a dynamic color such as systemGroupedBackground yields the light or dark variant that currently applies. The chapter on system colors and dark mode explains how those colors work.
Controlling dark mode
Because there is no darkTheme argument, you have two choices:
- Leave
brightnessunset and use dynamic colors. The app follows the device, and the sameCupertinoThemeDataserves both modes. - Set
brightnessto force one mode, for example when an in-app setting overrides the system. RebuildCupertinoAppwith a differentbrightnesswhen the user changes it.
Localizations
CupertinoApp ships with DefaultCupertinoLocalizations, which supports English only. Widgets such as date pickers and the text selection toolbar ("Copy", "Paste") need strings, so apps in other languages must add the delegates from flutter_localizations. Add the dependency first:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutterThen pass the delegates and the supported locales:
import 'package:flutter/cupertino.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
class LocalizedApp extends StatelessWidget {
const LocalizedApp({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoApp(
localizationsDelegates: [
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [Locale('en'), Locale('de'), Locale('ja')],
home: CupertinoPageScaffold(
child: Center(child: Text('Hello')),
),
);
}
}If the device locale is not in supportedLocales, Flutter falls back to the first entry in the list.
Common errors
- "No Material widget found." A Material widget such as
TextField,ListTileorChipwas placed in a tree with noMaterialancestor. In aCupertinoAppthere is none. Use the Cupertino equivalent (CupertinoTextField,CupertinoListTile), or wrap the subtree in aMaterialwidget. A later chapter covers mixing the two on purpose. - Yellow double underline under text. Text was built outside any widget that supplies a
DefaultTextStyle. Put the text inside aCupertinoPageScaffold, or wrap it in aDefaultTextStyle. - Theme changes seem ignored. A
constCupertinoAppwill not rebuild on its own. ChangingbrightnessorprimaryColorrequires the widget above it to rebuild, for example throughsetStatein a stateful root or aValueListenableBuilder. - Using
Theme.of(context)for colors. In a pure Cupertino tree, that returns a default Material theme, not yourCupertinoThemeData. UseCupertinoTheme.of(context). - Hard-coded fonts that break the iOS look. Leaving
fontFamilyunset lets Flutter use the system font on iOS. Setting one family for every style changes the feel of the whole app, so set it deliberately throughCupertinoTextThemeData.
Exercise
You build an app with CupertinoApp and CupertinoThemeData(primaryColor: CupertinoColors.systemIndigo). The device switches to dark mode, but you never set brightness. What happens, and what must you do to force the app to stay light regardless of the device setting?
Show answer
With brightness unset, the app follows the system, so it switches to dark mode and dynamic colors resolve to their dark variants. To force light mode, set brightness: Brightness.light in CupertinoThemeData. Because CupertinoApp has no darkTheme argument, this single property is the override.
Exercise
A CupertinoApp screen crashes with "No Material widget found" after you add a TextField. Give two ways to fix it.
Show answer
Replace TextField with CupertinoTextField, which does not need a Material ancestor. Alternatively, wrap the Material widget (or the subtree containing it) in a Material widget so it has the ancestor it needs.