Sub topic 3
Built-in adaptive widgets and dialogs
This chapter assumes you know Dart syntax, StatelessWidget and StatefulWidget, MaterialApp and Scaffold, and how BuildContext and Navigator work.
Built-in adaptive constructors and dialogs
Several Material widgets have an .adaptive constructor. On iOS and macOS it builds the Cupertino version of the widget. On other platforms it builds the Material version. Switch.adaptive, CircularProgressIndicator.adaptive, and AlertDialog.adaptive are common examples. showAdaptiveDialog opens the dialog in the platform's style.
import 'package:flutter/material.dart';
class AdaptiveSettings extends StatefulWidget {
const AdaptiveSettings({super.key});
@override
State<AdaptiveSettings> createState() => _AdaptiveSettingsState();
}
class _AdaptiveSettingsState extends State<AdaptiveSettings> {
bool _enabled = false;
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(
children: [
Switch.adaptive(
value: _enabled,
onChanged: (value) => setState(() => _enabled = value),
),
TextButton(onPressed: _showInfo, child: const Text('Info')),
],
);
}
}Building your own adaptive widget
When no built-in constructor fits, write a widget that hides the platform check. Read the platform from Theme.of(context).platform, not from dart:io. The theme value works on web and in tests, and tests can override it.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class AdaptiveButton extends StatelessWidget {
const AdaptiveButton({
super.key,
required this.onPressed,
required this.child,
});
final VoidCallback? onPressed;
final Widget child;
@override
Widget build(BuildContext context) {
final platform = Theme.of(context).platform;
if (platform == TargetPlatform.iOS || platform == TargetPlatform.macOS) {
return CupertinoButton.filled(onPressed: onPressed, child: child);
}
return FilledButton(onPressed: onPressed, child: child);
}
}Platform-appropriate scrolling
Flutter chooses scroll physics from the platform through ScrollBehavior. iOS scrolls with a bounce at the edges, and Android shows a clamping glow. Scrollable widgets already do this, so you rarely need to change it. Use ScrollConfiguration only when you need to override the default behavior for one part of the screen.
Adapting layout to size and input
Platform is not the only signal. A phone, a tablet, and a desktop window can all run the same iOS build. Use LayoutBuilder to read the available width, and use the layout to choose between one column and two. Input type, such as mouse or touch, is a separate question, and it does not follow from the platform.
Testing with a platform override
Set ThemeData(platform: ...) in the test's MaterialApp to select a branch without a device. Then assert which widget appears.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'adaptive_button.dart';
void main() {
testWidgets('uses CupertinoButton on iOS', (tester) async {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(platform: TargetPlatform.iOS),
home: Scaffold(
body: AdaptiveButton(onPressed: () {}, child: const Text('Go')),
),
),
);
expect(find.byType(CupertinoButton), findsOneWidget);
});
}