Sub topic 11
Built-in adaptive widgets and dialogs
This chapter assumes you can write StatefulWidgets with setState, you know MaterialApp and Scaffold, and you have used Navigator and BuildContext. It also assumes you know how to read the platform with Theme.of(context).platform from the earlier chapter on platform detection.
Flutter ships a set of widgets that choose between a Material and a Cupertino look for you. They are called adaptive widgets. They cover the most common controls, so you can get platform-appropriate UI without writing a branch yourself. This chapter covers what they do, how they decide, and where they fall short.
How the adaptive widgets decide
Every built-in adaptive widget reads Theme.of(context).platform. It does not read dart:io's Platform. This has three consequences:
- You can force a platform in a preview or a test by setting
ThemeData.platform. - The widgets work on the web, where
dart:iois unavailable. - Desktop targets behave predictably.
TargetPlatform.iOSandTargetPlatform.macOSselect the Cupertino variant. Android, Fuchsia, Linux and Windows select the Material variant.
flowchart TDThe diagram below shows the decision flow.
The adaptive constructors
An adaptive constructor is a named constructor, .adaptive, on a Material widget. It takes the same arguments as the default constructor. On iOS and macOS it builds the Cupertino equivalent. Elsewhere it builds the normal Material widget.
| Widget | Cupertino variant on iOS and macOS |
|---|---|
| `Switch.adaptive` | `CupertinoSwitch` |
| `SwitchListTile.adaptive` | List tile with a `CupertinoSwitch` |
| `CircularProgressIndicator.adaptive` | `CupertinoActivityIndicator` |
| `RefreshProgressIndicator`-style spinners in `RefreshIndicator.adaptive` | `CupertinoSliverRefreshControl` behavior |
| `Slider.adaptive` | `CupertinoSlider` |
| `Checkbox.adaptive` | Rounded Cupertino-style checkbox |
| `Radio.adaptive` | Cupertino-style radio |
| `AlertDialog.adaptive` | `CupertinoAlertDialog` |
The exact list grows between Flutter releases. Check the API docs for your Flutter version before you rely on a specific .adaptive constructor.
Some icons adapt too. Icons.adaptive.arrow_back, Icons.adaptive.share and Icons.adaptive.more return the iOS or the Material glyph.
import 'package:flutter/material.dart';
class AdaptiveIcons extends StatelessWidget {
const AdaptiveIcons({super.key});
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.adaptive.arrow_back),
Icon(Icons.adaptive.share),
Icon(Icons.adaptive.more),
],
);
}
}A worked example
The next example puts a switch, a spinner, a slider and an info dialog on one screen. The main function sets ThemeData.platform, so you can switch between the two looks on any device.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
// Change to TargetPlatform.android to compare the two looks.
theme: ThemeData(platform: TargetPlatform.iOS),
home: const Scaffold(body: Center(child: AdaptiveSettings())),
),
);
}
class AdaptiveSettings extends StatefulWidget {
const AdaptiveSettings({super.key});
@override
State<AdaptiveSettings> createState() => _AdaptiveSettingsState();
}
class _AdaptiveSettingsState extends State<AdaptiveSettings> {
bool _enabled = false;
double _volume = 0.5;
Future<void> _showInfo() {
return showAdaptiveDialog<void>(
context: context,
builder: (context) => AlertDialog.adaptive(
title: const Text('Heads up'),
content: const Text('Notifications can be changed later.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Switch.adaptive(
value: _enabled,
onChanged: (value) => setState(() => _enabled = value),
),
Slider.adaptive(
value: _volume,
onChanged: (value) => setState(() => _volume = value),
),
const CircularProgressIndicator.adaptive(),
TextButton(onPressed: _showInfo, child: const Text('Info')),
],
);
}
}The state still lives in your State object. Adaptive widgets are controlled widgets, like the Material ones: you pass value and onChanged, and you call setState.
Adaptive dialogs
Two pieces work together here:
showAdaptiveDialogopens the route. On iOS and macOS it usesshowCupertinoDialog, which has a Cupertino fade-and-scale transition. Elsewhere it usesshowDialog.AlertDialog.adaptivebuilds the dialog body. On iOS and macOS it returns aCupertinoAlertDialog.
Use them as a pair. If you pass a Material AlertDialog to showAdaptiveDialog, you get the Cupertino route with a Material card inside, which looks wrong on iOS.
Gotcha: the actions do not adapt
AlertDialog.adaptive converts the dialog frame, but it passes your actions through unchanged. A TextButton inside a CupertinoAlertDialog keeps its Material styling. It does not get the full-width tap area, the dividers' fit or the bold and red styles that iOS users expect.
Build the actions yourself with a small helper:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Widget dialogAction(
BuildContext context, {
required String label,
required VoidCallback onPressed,
bool isDestructive = false,
}) {
final platform = Theme.of(context).platform;
if (platform == TargetPlatform.iOS || platform == TargetPlatform.macOS) {
return CupertinoDialogAction(
isDestructiveAction: isDestructive,
onPressed: onPressed,
child: Text(label),
);
}
return TextButton(
onPressed: onPressed,
style: isDestructive
? TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.error,
)
: null,
child: Text(label),
);
}
Future<bool?> confirmReset(BuildContext context) {
return showAdaptiveDialog<bool>(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog.adaptive(
title: const Text('Reset settings?'),
content: const Text('This cannot be undone.'),
actions: [
dialogAction(
context,
label: 'Cancel',
onPressed: () => Navigator.pop(context, false),
),
dialogAction(
context,
label: 'Reset',
isDestructive: true,
onPressed: () => Navigator.pop(context, true),
),
],
),
);
}Two details in this code are worth noting:
barrierDismissibleis set explicitly. Its default is not the same on both platforms, so state what you want.- The dialog returns a
bool?. Tapping outside a dismissible dialog returnsnull, so treatnullas cancel.
Limits of the built-in set
The built-ins cover controls and dialogs, but not everything:
- There is no
.adaptivefor buttons, text fields, app bars, navigation bars or tab bars. For those you write the branch yourself, which is the topic of the next chapter. - Adaptive widgets still need a Material ancestor on the Material path. Under
MaterialAppandScaffoldthis is automatic. In aCupertinoApp, a Material variant such asSwitch.adaptivecan fail with a "No Material widget found" error on Android. - They follow the theme platform, not the user's taste. A tester on Android who sets
platform: TargetPlatform.iOSsees Cupertino controls with Android scrolling and system bars. - Colors still come from different themes.
Switch.adaptiveusesThemeDatacolors on Material and the Cupertino color on iOS. Properties such asactiveColorapply to both, so check the result on both platforms.
Checklist for using adaptive widgets
- Prefer
.adaptivewhen the Material and Cupertino versions do the same job, as with a switch or a slider. - Pair
showAdaptiveDialogwithAlertDialog.adaptive. - Build dialog actions per platform.
- Preview both platforms with
ThemeData.platformbefore you ship. - Read the platform from the theme, never from
dart:io.
Exercise
A colleague wrote showAdaptiveDialog(context: context, builder: (_) => AlertDialog(title: Text('Delete?'), actions: [TextButton(onPressed: ..., child: Text('Delete'))])). On an iPhone the dialog looks like a Material card with a fade-and-scale transition. Name the two changes needed so it looks like an iOS alert, and say what is still not fully adapted after those changes.
Show answer
Replace AlertDialog with AlertDialog.adaptive, so the body becomes a CupertinoAlertDialog on iOS and macOS. Then the TextButton still uses Material styling, so you should build the action per platform, for example with CupertinoDialogAction(isDestructiveAction: true) on iOS and macOS. The route already adapts because showAdaptiveDialog uses showCupertinoDialog on those platforms. Before the first change, only the route was adapted. After the first change, the frame is adapted but the actions are not, unless you replace them with a helper.
Exercise
Why does Switch.adaptive pick its look from Theme.of(context).platform instead of dart:io's Platform, and how can you use that in a test?
Show answer
Theme.of(context).platform works on the web, where dart:io is not available, and it can be overridden. In a widget test you set ThemeData(platform: TargetPlatform.iOS) on the MaterialApp, and Switch.adaptive builds a CupertinoSwitch. You can then check it with find.byType(CupertinoSwitch). You can repeat the test with TargetPlatform.android to check the Material branch.