Sub topic 9
Using Cupertino widgets inside a MaterialApp
This chapter assumes basic Dart, StatelessWidget, StatefulWidget, setState, MaterialApp, Scaffold, Navigator, and BuildContext. The goal is to use selected iOS-style controls without replacing the Material structure of an existing app.
Why mix Material and Cupertino?
Material and Cupertino are Flutter’s two main design languages. Material provides Android-style components such as Scaffold, AppBar, and FilledButton. Cupertino provides controls that follow Apple’s visual and interaction conventions.
A MaterialApp does not prevent you from using Cupertino widgets. Both widget libraries can appear in the same widget tree. Keep the surrounding page Material when your app already follows Material design, then use Cupertino controls where their behavior or appearance is useful.
| Material widget | Cupertino alternative | Typical use |
|---|---|---|
| `Switch` | `CupertinoSwitch` | iOS-style settings toggle |
| `TextButton` | `CupertinoButton` | iOS-style text action |
| `AlertDialog` | `CupertinoAlertDialog` | iOS-style confirmation |
| `CircularProgressIndicator` | `CupertinoActivityIndicator` | iOS-style loading indicator |
A Material page with Cupertino controls
The example below keeps MaterialApp, Scaffold, and AppBar, but uses a CupertinoSwitch and a Cupertino confirmation dialog.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const SettingsApp());
}
class SettingsApp extends StatelessWidget {
const SettingsApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorSchemeSeed: Colors.indigo,
useMaterial3: true,
),
home: const SettingsPage(),
);
}
}
class SettingsPage extends StatefulWidget {
const SettingsPage({super.key});
@override
State<SettingsPage> createState() => _SettingsPageState();
}
class _SettingsPageState extends State<SettingsPage> {
bool _notificationsEnabled = true;
Future<void> _confirmReset() {
return showCupertinoDialog<void>(
context: context,
builder: (dialogContext) {
return CupertinoAlertDialog(
title: const Text('Reset settings?'),
content: const Text('Your notification choice will be cleared.'),
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.pop(dialogContext),
child: const Text('Cancel'),
),
CupertinoDialogAction(
isDestructiveAction: true,
onPressed: () {
setState(() => _notificationsEnabled = false);
Navigator.pop(dialogContext);
},
child: const Text('Reset'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: ListView(
children: [
ListTile(
title: const Text('Notifications'),
trailing: CupertinoSwitch(
value: _notificationsEnabled,
onChanged: (value) {
setState(() => _notificationsEnabled = value);
},
),
),
CupertinoButton(
onPressed: _confirmReset,
child: const Text('Reset notifications'),
),
],
),
);
}
}CupertinoSwitch is controlled by the parent state. Its value describes the current setting, and onChanged reports a new value. As with a Material Switch, update the state inside setState.
showCupertinoDialog inserts a Cupertino-style route above the current page. Use the dialog’s local BuildContext when closing it so the intended dialog route is popped.
Theme and platform details
Cupertino widgets can read values from CupertinoTheme. In a Material app, wrap a Cupertino section when it needs a specific Cupertino color or brightness.
flowchart TD
A[MaterialApp] --> B[Material Scaffold]
B --> C[CupertinoTheme]
C --> D[Cupertino controls]
B --> E[Material controls]A mixed interface should remain understandable. Avoid placing a Cupertino dialog beside Material buttons without a reason. If most of the product follows one design language, adapt only controls whose platform-specific behavior improves the user experience.
Exercise
Why can a CupertinoSwitch be used inside a MaterialApp, and where should its state live?
Show answer
Flutter widgets from both libraries can share one widget tree. The parent should own the switch state and pass it through value, then update it in onChanged.