Sub topic 12
Building Your Own Adaptive Widget
Assume you know basic Dart, StatelessWidget, StatefulWidget, setState, MaterialApp, Scaffold, Navigator, and BuildContext. This chapter shows how to hide a platform choice inside a reusable widget.
An adaptive widget chooses a suitable implementation for the current platform. A screen using that widget does not need to know whether it is displaying Material or Cupertino controls.
Keep Platform Decisions in One Place
Use Theme.of(context).platform to read Flutter's current platform setting. This works on Android, iOS, desktop, web, and widget tests. It can also be overridden in a theme.
Do not use dart:io's Platform.isIOS for this decision. That API is unavailable on some web targets and makes platform-specific widget tests harder to control.
| Platform choice | Widget variant |
|---|---|
| iOS or macOS | `CupertinoButton.filled` |
| Android, Windows, Linux, or web | `FilledButton` |
A reusable widget can expose a stable API while changing its internal implementation:
Implement an Adaptive Button
The public properties are the same for both variants: an optional callback and the button's child. A null callback disables the button in both design systems.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorSchemeSeed: Colors.indigo,
useMaterial3: true,
),
home: const ExamplePage(),
);
}
}
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;
final isApplePlatform =
platform == TargetPlatform.iOS ||
platform == TargetPlatform.macOS;
if (isApplePlatform) {
return CupertinoButton.filled(
onPressed: onPressed,
child: child,
);
}
return FilledButton(
onPressed: onPressed,
child: child,
);
}
}
class ExamplePage extends StatelessWidget {
const ExamplePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Adaptive UI')),
body: Center(
child: AdaptiveButton(
onPressed: () {},
child: const Text('Continue'),
),
),
);
}
}The branch belongs in AdaptiveButton, not in every screen. This keeps platform logic consistent and makes future changes local.
Common Errors and Trade-offs
A custom adaptive widget is useful when the same decision appears in several screens. It also gives you a place to add shared semantics, sizing, or analytics.
Avoid creating a custom abstraction for one isolated control. A direct CupertinoButton or FilledButton may be clearer. Also avoid checking only the physical device. A user can run an iOS-targeted interface in a test or preview with a different platform setting.
Keep the API small. If every platform-specific property is exposed, callers become coupled to both design systems. Add a property only when both variants can support it meaningfully.
Exercise
Why should an adaptive widget read Theme.of(context).platform instead of dart:io Platform.isIOS?
Show answer
Theme.of(context).platform works across supported Flutter targets and can be overridden in widget tests, while dart:io is unavailable on some web targets.