Sub topic 2
Starting an App with `CupertinoApp` and `CupertinoThemeData`
This chapter assumes basic Dart syntax, Flutter widgets, StatelessWidget, StatefulWidget, and setState. You should also be familiar with MaterialApp, Scaffold, BuildContext, and basic navigation.
Flutter's Cupertino library provides widgets that follow Apple's iOS design language. CupertinoApp is the application root for a Cupertino-style app. It provides navigation, localization, text direction, and an inherited Cupertino theme to descendant widgets.
CupertinoThemeData stores visual settings such as the primary color, text styles, and brightness. Widgets read these settings from the nearest CupertinoTheme or CupertinoApp.
Create a minimal Cupertino app
The following app displays an iOS-style screen with 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: CupertinoNavigationBar(
middle: const Text('Settings'),
),
child: SafeArea(
child: Center(
child: CupertinoButton.filled(
onPressed: null,
child: const Text('Save'),
),
),
),
);
}
}CupertinoPageScaffold and CupertinoNavigationBar are covered in the next chapter. Here, focus on how they receive their app-wide configuration.
The theme property passes a CupertinoThemeData object down the widget tree. A descendant can read it with:
final theme = CupertinoTheme.of(context);This lookup requires a BuildContext below the CupertinoApp. Theme values are inherited, so a widget does not need a theme parameter passed through every constructor.
Choose theme values
Use system colors when a color should remain appropriate in light and dark appearances. For example, CupertinoColors.systemIndigo can change its actual value according to the current system brightness.
You can set brightness explicitly when the app should not follow the device:
const CupertinoThemeData(
brightness: Brightness.dark,
primaryColor: CupertinoColors.systemOrange,
);Usually, leave brightness unset and let the app follow the platform appearance. Hard-coded colors can become unreadable when the device switches between light and dark mode.
How the theme reaches a widget
The main decision is whether the entire app should use Cupertino styling. If it should, use CupertinoApp as the root. If the rest of the app is Material-based, keep MaterialApp as the root and add Cupertino widgets only where they fit.
Exercise
Why should an app usually use Cupertino system colors instead of fixed color values?
Show answer
System colors can respond to light and dark mode, which helps preserve contrast and match the device appearance.