Sub topic 9
Using Cupertino widgets inside a MaterialApp
Most real apps do not pick one design language for every screen. A MaterialApp is often the better base, because it works with Material 3, Scaffold, and the wider package ecosystem. Individual controls such as a switch, a slider, or an action sheet can still look native to iOS. This chapter explains what happens when Cupertino widgets run under a Material root, what they need from it, and where the combination breaks.
It assumes you know StatelessWidget, StatefulWidget, setState, MaterialApp, Scaffold, BuildContext, and basic Navigator use. It also assumes you have read the earlier chapters on CupertinoApp, CupertinoThemeData, and the Cupertino controls.
Why mixing works
Cupertino widgets do not require CupertinoApp. They require a few ancestors, and MaterialApp provides most of them:
- A
CupertinoTheme.MaterialAppinserts aTheme. ThatThemealso inserts aCupertinoThemebelow it, built from the Material theme. The Cupertino primary color comes fromcolorScheme.primary. The brightness and the background colors come from the Material theme too. - Cupertino localizations.
MaterialAppincludesDefaultCupertinoLocalizationsin its default delegates. Widgets such asCupertinoDatePickerandCupertinoAlertDialoguse these strings. - A
Navigatorand anOverlay. These are needed for dialogs and modal popups.
The reverse direction is different. Material widgets need a Material ancestor, which Scaffold provides. CupertinoPageScaffold does not provide one.
A working example
The following app uses a Material shell with Cupertino controls inside it. It sets cupertinoOverrideTheme so the Cupertino controls use a different accent color from the Material widgets.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(const MixedApp());
class MixedApp extends StatelessWidget {
const MixedApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
cupertinoOverrideTheme: const CupertinoThemeData(
primaryColor: CupertinoColors.systemOrange,
),
),
home: const MixedScreen(),
);
}
}
class MixedScreen extends StatefulWidget {
const MixedScreen({super.key});
@override
State<MixedScreen> createState() => _MixedScreenState();
}
class _MixedScreenState extends State<MixedScreen> {
bool _wifi = true;
double _volume = 0.4;
Future<void> _chooseSort() {
return showCupertinoModalPopup<void>(
context: context,
builder: (context) => CupertinoActionSheet(
title: const Text('Sort by'),
actions: [
CupertinoActionSheetAction(
onPressed: () => Navigator.pop(context),
child: const Text('Name'),
),
CupertinoActionSheetAction(
onPressed: () => Navigator.pop(context),
child: const Text('Date'),
),
],
cancelButton: CupertinoActionSheetAction(
isDefaultAction: true,
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Mixed screen')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
const CupertinoTextField(placeholder: 'Device name'),
const SizedBox(height: 16),
Row(
children: [
const Expanded(child: Text('Wi-Fi')),
CupertinoSwitch(
value: _wifi,
onChanged: (value) => setState(() => _wifi = value),
),
],
),
CupertinoSlider(
value: _volume,
onChanged: (value) => setState(() => _volume = value),
),
const SizedBox(height: 16),
FilledButton(onPressed: _chooseSort, child: const Text('Sort')),
],
),
);
}
}The Scaffold, AppBar, Text, and FilledButton are Material. The text field, switch, slider, and action sheet are Cupertino. The switch and slider use orange because cupertinoOverrideTheme wins over the value derived from the Material color scheme. The FilledButton stays indigo.
What the Cupertino theme inherits
Without an override, the derived Cupertino theme follows the Material theme. Changing ThemeData.colorScheme or brightness therefore also changes the Cupertino controls. This is usually what you want, because one seed color then themes the whole screen.
Set cupertinoOverrideTheme only when the Cupertino controls should differ. The override replaces only the properties you set. Unset properties are still taken from the Material theme.
Rules for each direction
| Situation | Result | Fix |
|---|---|---|
| Cupertino widget inside `Scaffold` under `MaterialApp` | Works. | None. |
| Material `TextField` inside `CupertinoPageScaffold` under `MaterialApp` and no `Scaffold` | Throws "No Material widget found". | Wrap it in `Material`, or use `CupertinoTextField`. |
| `Text` under `CupertinoPageScaffold` in a `MaterialApp` route, with no `Scaffold` or `Material` above it | Debug builds show a yellow double underline. | Wrap the page in `Material`, or set a `DefaultTextStyle`. |
| Custom `localizationsDelegates` on `MaterialApp` | Cupertino strings may be missing. | Add `GlobalCupertinoLocalizations.delegate`. |
| `Container(color: CupertinoColors.systemBackground)` | Always shows the light value. | Resolve the color against the context. |
The third row happens because MaterialApp does not set a Cupertino text style. CupertinoApp does, and Scaffold supplies one for Material. CupertinoPageScaffold supplies neither.
Dynamic colors need resolving
CupertinoColors.systemBackground is a CupertinoDynamicColor. It holds separate light and dark values. Cupertino widgets resolve it for you. A plain Container or DecoratedBox does not, so it paints the default light value even in dark mode.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Panel extends StatelessWidget {
const Panel({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
color: CupertinoDynamicColor.resolve(
CupertinoColors.secondarySystemBackground,
context,
),
borderRadius: BorderRadius.circular(12),
),
child: Padding(padding: const EdgeInsets.all(16), child: child),
);
}
}CupertinoDynamicColor.resolve reads the brightness from the nearest CupertinoTheme. Under MaterialApp that brightness follows the Material theme, so it matches the rest of the screen.
Navigation and dialogs
CupertinoPageRoute works in a MaterialApp Navigator. It pushes with the iOS slide transition and supports the swipe-back gesture from the left edge.
Navigator.of(context).push(
CupertinoPageRoute<void>(builder: (context) => const DetailPage()),
);Note that MaterialPageRoute already uses the Cupertino transition on iOS and macOS, through PageTransitionsTheme. You only need CupertinoPageRoute when you want the iOS transition on every platform. The details of routes and tab scaffolds are covered in the navigation chapter.
showCupertinoDialog and showCupertinoModalPopup look up the nearest Navigator, so they work unchanged. By default, showCupertinoModalPopup uses the root navigator and places the sheet over the whole app. This is also the right behavior for an action sheet.
Using CupertinoNavigationBar in a Scaffold
Scaffold.appBar accepts any PreferredSizeWidget, and CupertinoNavigationBar qualifies. You can place it there:
Scaffold(
appBar: const CupertinoNavigationBar(middle: Text('Inbox')),
body: const Center(child: Text('Empty')),
)This works, but check two things. The bar's translucent background blends with scaffoldBackgroundColor, which may look different from a CupertinoPageScaffold screen. The bar's text also takes its style from the Cupertino theme, so it will not match Material AppBar titles elsewhere. If you want the full iOS page structure, use CupertinoPageScaffold and wrap its child in Material when you need Material widgets inside.
When to mix
Mixing is a good fit in these cases:
- Only a few controls need the iOS look, such as switches, sliders, pickers, and action sheets.
- The app already depends on Material components with no Cupertino equivalent, such as
NavigationBar,Chip, orSnackBar. - You will make the choice per platform with an adaptive widget, covered in later chapters.
Mixing is a poor fit when the whole app must look like an iOS app. In that case CupertinoApp gives a consistent text style, scaffold behavior, and navigation without the extra wrappers above.
Exercise
A CupertinoPageScaffold is used as the home screen of a MaterialApp, and its child contains a Material TextField. The app throws "No Material widget found". Give two ways to fix it, and say which one you would choose if the screen should look like iOS.
Show answer
Wrap the TextField (or the whole page body) in a Material widget, or replace the TextField with CupertinoTextField, which needs no Material ancestor. For an iOS look, use CupertinoTextField, because a Material TextField would keep Material styling inside an otherwise Cupertino screen.
Exercise
In a MaterialApp with a darkTheme, a Container(color: CupertinoColors.systemBackground) stays white in dark mode, while a CupertinoSwitch next to it looks correct. Why, and how do you fix the container?
Show answer
CupertinoSwitch resolves its dynamic colors against the context, but Container paints the CupertinoDynamicColor as a plain Color and uses its default light value. Fix it with CupertinoDynamicColor.resolve(CupertinoColors.systemBackground, context), which picks the light or dark value from the nearest CupertinoTheme.
Exercise
You want all Cupertino controls in a MaterialApp to use a green accent, but the Material widgets must keep the seed-based color scheme. Which ThemeData property do you set, and what does it replace?
Show answer
Set ThemeData.cupertinoOverrideTheme to a CupertinoThemeData with primaryColor set to the green color. It replaces only the properties you provide in the derived Cupertino theme. The Material colorScheme is not changed.