Sub topic 11
Adapting to Platform Conventions
Assume you know basic Dart, widget trees, Scaffold, and stateful widgets. This chapter focuses on choosing controls and interactions that feel natural on Android, iOS, desktop, and the web.
Responsive layout changes the arrangement of content when space changes. Platform adaptation changes controls and interaction patterns when the platform has different conventions. A phone and desktop window might use the same navigation structure, but a switch, dialog, pointer cursor, or keyboard shortcut may need different treatment.
Use adaptive widgets when the meaning stays the same
Flutter provides adaptive widgets for controls whose purpose is shared across platforms. For example, Switch.adaptive displays a Material-style switch on platforms that use Material conventions and a Cupertino-style switch on Apple platforms.
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(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> showResetDialog() async {
final platform = Theme.of(context).platform;
await showDialog<void>(
context: context,
builder: (context) {
final title = const Text('Reset settings?');
final message = const Text('This action cannot be undone.');
if (platform == TargetPlatform.iOS ||
platform == TargetPlatform.macOS) {
return AlertDialog.adaptive(
title: title,
content: message,
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Reset'),
),
],
);
}
return AlertDialog(
title: title,
content: message,
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () => Navigator.pop(context),
child: const Text('Reset'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: ListView(
children: [
SwitchListTile.adaptive(
title: const Text('Notifications'),
value: notificationsEnabled,
onChanged: (value) {
setState(() => notificationsEnabled = value);
},
),
ListTile(
title: const Text('Reset settings'),
trailing: const Icon(Icons.chevron_right),
onTap: showResetDialog,
),
],
),
);
}
}AlertDialog.adaptive is useful when the dialog content and action are the same, but the visual treatment should follow the platform. If the interaction itself differs, use separate Material and Cupertino widgets deliberately.
| Decision | Typical approach |
|---|---|
| Same meaning, different visual convention | Use an adaptive widget |
| Different interaction model | Build platform-specific branches |
| Business logic | Keep it shared outside the platform branch |
| Keyboard or pointer support | Add it for every platform that needs it |
Adapt input, not only appearance
Touch interfaces need large tap targets and clear pressed states. Mouse users expect hover feedback, a cursor, and sometimes context menus. Keyboard users need focus order, visible focus, and activation with Enter or Space.
Use FocusableActionDetector, Shortcuts, and Actions when an important operation must work without a pointer. Keep labels and semantics stable so assistive technologies receive the same meaning on every platform.
Choose adaptation boundaries carefully
Keep platform checks close to the widget that changes. Do not duplicate data loading, validation, or navigation logic. Also remember that Flutter tests can override the platform through the theme, which makes Theme.of(context).platform easier to control than scattered global checks.
Exercise
When should you use an adaptive widget instead of two completely separate implementations?
Show answer
Use an adaptive widget when the control has the same meaning and behavior but should follow different platform visual conventions. Use separate implementations when the interaction model or required behavior differs.