Sub topic 11
Built-in adaptive widgets and dialogs
What adaptive widgets do
This chapter assumes basic Dart, StatefulWidget, setState, MaterialApp, Scaffold, Navigator, and BuildContext.
Flutter provides adaptive widgets that choose a Material or Cupertino implementation from the current platform. This lets one widget tree follow platform conventions without repeating an if statement in every screen.
An adaptive widget changes its appearance and sometimes its interaction details. It does not automatically redesign your whole layout. You still decide how the screen is structured.
| Widget | Adaptive behavior |
|---|---|
| `Switch.adaptive` | Uses a Material switch or a Cupertino switch |
| `CircularProgressIndicator.adaptive` | Uses a platform-appropriate progress indicator |
| `AlertDialog.adaptive` | Uses a Material or Cupertino alert |
| `showAdaptiveDialog` | Presents a platform-appropriate dialog route |
The platform comes from the widget tree's theme. This makes the choice testable and avoids importing dart:io.
A screen with adaptive controls
The following app uses adaptive switches, a progress indicator, and a dialog:
import 'package:flutter/material.dart';
void main() => runApp(const AdaptiveApp());
class AdaptiveApp extends StatelessWidget {
const AdaptiveApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(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 = false;
Future<void> _showInfo() {
return showAdaptiveDialog<void>(
context: context,
builder: (context) {
return AlertDialog.adaptive(
title: const Text('Notifications'),
content: const Text('You can change this setting at any time.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
SwitchListTile.adaptive(
title: const Text('Notifications'),
value: _notificationsEnabled,
onChanged: (value) {
setState(() => _notificationsEnabled = value);
},
),
const Center(child: CircularProgressIndicator.adaptive()),
FilledButton(
onPressed: _showInfo,
child: const Text('More information'),
),
],
),
);
}
}showAdaptiveDialog chooses the dialog route. AlertDialog.adaptive chooses the alert itself. The dialog's content remains your responsibility, so keep its text and actions clear on both platforms.
Important limits
Adaptive constructors cover common controls, but they do not exist for every widget. A FilledButton remains a Material widget, and a CupertinoButton remains a Cupertino widget. Use a custom wrapper when a control needs a specific variant.
The current platform can also be overridden by the theme. Therefore, do not assume that a desktop build always means a Material branch or that every iOS-looking test is running on an iPhone.
How the decision is made
Exercise
Which two APIs are needed to show an adaptive alert dialog?
Show answer
Use showAdaptiveDialog to present the route and AlertDialog.adaptive to choose the alert's platform style.