Sub topic 15
Testing Adaptive Widgets with a Platform Override
This chapter assumes basic Dart, Flutter widgets, MaterialApp, Scaffold, BuildContext, and widget tests. You will test an adaptive widget without running the test on separate iOS and Android devices.
Why override the platform?
An adaptive widget chooses its appearance from the platform in the current Flutter theme. For example, this widget uses a Cupertino button on iOS and macOS, and a Material button elsewhere:
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;
final isApplePlatform =
platform == TargetPlatform.iOS ||
platform == TargetPlatform.macOS;
if (isApplePlatform) {
return CupertinoButton.filled(
onPressed: onPressed,
child: child,
);
}
return FilledButton(
onPressed: onPressed,
child: child,
);
}
}In a widget test, set ThemeData.platform to choose the branch deliberately.
| Theme platform | Expected widget |
|---|---|
| `TargetPlatform.iOS` | `CupertinoButton` |
| `TargetPlatform.android` | `FilledButton` |
| `TargetPlatform.macOS` | `CupertinoButton` |
This approach is more reliable than checking dart:io. The dart:io platform value describes the environment running the test and does not work on Flutter web. A theme override is also useful because each test can select its input independently.
Write tests for both branches
Create a test such as test/adaptive_button_test.dart:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import '../lib/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('Continue'),
),
),
),
);
expect(find.byType(CupertinoButton), findsOneWidget);
expect(find.byType(FilledButton), findsNothing);
});
testWidgets('uses FilledButton on Android', (tester) async {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(
body: AdaptiveButton(
onPressed: () {},
child: const Text('Continue'),
),
),
),
);
expect(find.byType(FilledButton), findsOneWidget);
expect(find.byType(CupertinoButton), findsNothing);
});
}Run the tests with:
flutter test test/adaptive_button_test.dartpumpWidget builds the widget tree. find.byType locates a widget by its runtime type. The findsNothing checks are important because they prove that the other branch was not built.
Exercise
Exercise
How would you test the macOS branch of AdaptiveButton?
Show answer
Pump it inside a MaterialApp whose ThemeData uses TargetPlatform.macOS, then expect one CupertinoButton and no FilledButton.