Merge: Material host and controls on every platform

This commit is contained in:
2026-11-03 16:08:08 +00:00
13 changed files with 139 additions and 130 deletions
@@ -5,18 +5,13 @@ import 'package:flutter/material.dart';
/// Which standard control family this screen renders. /// Which standard control family this screen renders.
enum ChatControlsStyle { material, cupertino } enum ChatControlsStyle { material, cupertino }
/// Cupertino under a Cupertino app or an iOS/macOS Material theme, Material /// Cupertino under an explicit Cupertino app, Material everywhere else,
/// everywhere else. The shell decides the app class, so a bare Cupertino app /// whatever the target platform. The shell decides the app class, so a bare
/// never has to grow a Material ancestor just for this screen. /// Cupertino app never has to grow a Material ancestor just for this screen.
ChatControlsStyle chatControlsStyleOf(BuildContext context) { ChatControlsStyle chatControlsStyleOf(BuildContext context) =>
if (context.findAncestorWidgetOfExactType<MaterialApp>() == null) { context.findAncestorWidgetOfExactType<MaterialApp>() == null
return ChatControlsStyle.cupertino; ? ChatControlsStyle.cupertino
} : ChatControlsStyle.material;
final platform = Theme.of(context).platform;
return platform == TargetPlatform.iOS || platform == TargetPlatform.macOS
? ChatControlsStyle.cupertino
: ChatControlsStyle.material;
}
class ChatBarAction extends StatelessWidget { class ChatBarAction extends StatelessWidget {
const ChatBarAction({ const ChatBarAction({
@@ -100,7 +100,7 @@ class _ChatAttachmentThumbnailState extends State<ChatAttachmentThumbnail> {
child: Center( child: Center(
child: widget.cupertino child: widget.cupertino
? const CupertinoActivityIndicator() ? const CupertinoActivityIndicator()
: const CircularProgressIndicator.adaptive(), : const CircularProgressIndicator(),
), ),
); );
@@ -6,11 +6,9 @@ import '../../../ui/adaptive/segmented_bar.dart';
import '../pipeline_query.dart'; import '../pipeline_query.dart';
/// The one platform decision for this feature. The screen itself receives no /// The one control-family decision for this feature: Cupertino only under an
/// platform flag, so tests and integration obey Flutter's target platform. /// explicit Cupertino host, Material on every platform otherwise.
bool useCupertinoControls(BuildContext context) => bool useCupertinoControls(BuildContext context) => usesCupertinoHost(context);
usesCupertinoHost(context) ||
Theme.of(context).platform == TargetPlatform.iOS;
/// Standard platform activity indicator with an accessible status label. /// Standard platform activity indicator with an accessible status label.
class PipelineProgress extends StatelessWidget { class PipelineProgress extends StatelessWidget {
+3 -8
View File
@@ -81,14 +81,9 @@ class _SearchScreenState extends State<SearchScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// Follow the actual host app class first: a CupertinoApp supplies no // Follow the actual host app class: Cupertino only under an explicit
// Material theme, so reading only the platform would pick Material controls // Cupertino host, Material on every platform otherwise.
// inside a Cupertino host on macOS/iOS. final cupertino = widget.cupertino ?? usesCupertinoHost(context);
final cupertino =
widget.cupertino ??
(usesCupertinoHost(context) ||
Theme.of(context).platform == TargetPlatform.iOS ||
Theme.of(context).platform == TargetPlatform.macOS);
if (widget.embedded) { if (widget.embedded) {
// The shell's bar carries the controls; without a shell (a bare embed) // The shell's bar carries the controls; without a shell (a bare embed)
// they lead the results instead. // they lead the results instead.
@@ -3,9 +3,9 @@ import 'package:flutter/material.dart';
import '../../ui/adaptive/host.dart'; import '../../ui/adaptive/host.dart';
bool usesCupertinoControls(BuildContext context) => /// Cupertino only under an explicit Cupertino host; Material on every
usesCupertinoHost(context) || /// platform otherwise.
Theme.of(context).platform == TargetPlatform.iOS; bool usesCupertinoControls(BuildContext context) => usesCupertinoHost(context);
Color settingsErrorColor(BuildContext context) => usesCupertinoControls(context) Color settingsErrorColor(BuildContext context) => usesCupertinoControls(context)
? CupertinoColors.destructiveRed.resolveFrom(context) ? CupertinoColors.destructiveRed.resolveFrom(context)
+8 -8
View File
@@ -1,8 +1,8 @@
import 'package:flutter/foundation.dart'; /// The app renders the same Material layout and design on every platform:
/// web, Android, iOS and macOS. The Cupertino host and controls remain
/// Browsers use Material consistently, regardless of their host OS. /// available only when a caller asks for them explicitly (`cupertino: true`);
bool prefersCupertino({bool web = kIsWeb, TargetPlatform? platform}) { /// they are never chosen from the target platform.
final target = platform ?? defaultTargetPlatform; ///
return !web && /// Flutter's automatic platform behaviours (scroll physics, text selection,
(target == TargetPlatform.iOS || target == TargetPlatform.macOS); /// page transitions and back-swipe) still follow the target platform.
} bool prefersCupertino() => false;
@@ -6,25 +6,25 @@ import 'package:flutter_test/flutter_test.dart';
import 'chat_test_harness.dart'; import 'chat_test_harness.dart';
import 'fake_chat_gateway.dart'; import 'fake_chat_gateway.dart';
/// The message card is also mounted under a bare `MaterialApp` (the screen's /// iOS and macOS run the same Material app as web and Android, so the chat
/// iOS/macOS path). Without its own style, `Text` inherits MaterialApp's /// list under an iOS `MaterialApp` renders the Material screen and controls,
/// "missing Material" fallback: 48px, red, monospace with a double underline. /// with ordinary text rather than the "missing Material" fallback style.
void main() { void main() {
testWidgets('Cupertino empty message under MaterialApp uses normal text', ( testWidgets('iOS empty message under MaterialApp is the Material screen', (
tester, tester,
) async { ) async {
usePhoneSurface(tester); usePhoneSurface(tester);
await pumpIosMaterialChat(tester, FakeChatGateway()); await pumpIosMaterialChat(tester, FakeChatGateway());
expect(find.byType(CupertinoPageScaffold), findsOneWidget); expect(find.byType(Scaffold), findsWidgets);
expect(find.byType(Scaffold), findsNothing); expect(find.byType(CupertinoPageScaffold), findsNothing);
const message = 'No chat sessions on this server yet.'; const message = 'No chat sessions on this server yet.';
expect(find.text(message), findsOneWidget); expect(find.text(message), findsOneWidget);
expectNormalTextStyle(tester, message); expectNormalTextStyle(tester, message);
}); });
testWidgets('Cupertino load error under MaterialApp uses normal text', ( testWidgets('iOS load error under MaterialApp uses Material controls', (
tester, tester,
) async { ) async {
usePhoneSurface(tester); usePhoneSurface(tester);
@@ -42,18 +42,19 @@ void main() {
expect(find.text(message), findsOneWidget); expect(find.text(message), findsOneWidget);
expectNormalTextStyle(tester, message); expectNormalTextStyle(tester, message);
expect(find.text('No chat sessions on this server yet.'), findsNothing); expect(find.text('No chat sessions on this server yet.'), findsNothing);
expect(find.widgetWithText(CupertinoButton, 'Retry'), findsOneWidget); expect(find.widgetWithText(CupertinoButton, 'Retry'), findsNothing);
expect(find.text('Retry'), findsOneWidget);
}); });
} }
/// Resolves the style actually painted for [message] and checks it is the /// Resolves the style actually painted for [message] and checks it is not
/// ordinary Cupertino body style rather than MaterialApp's fallback style. /// MaterialApp's "missing Material" fallback style.
void expectNormalTextStyle(WidgetTester tester, String message) { void expectNormalTextStyle(WidgetTester tester, String message) {
final paragraph = tester.renderObject<RenderParagraph>(find.text(message)); final paragraph = tester.renderObject<RenderParagraph>(find.text(message));
final style = paragraph.text.style!; final style = paragraph.text.style!;
expect( expect(
style.fontSize, style.fontSize,
const CupertinoThemeData().textTheme.textStyle.fontSize, isNot(48),
reason: 'message should not use MaterialApp\'s 48px fallback style', reason: 'message should not use MaterialApp\'s 48px fallback style',
); );
expect(style.fontFamily, isNot('monospace')); expect(style.fontFamily, isNot('monospace'));
@@ -10,19 +10,17 @@ Widget _host(
NotificationServiceBridge bridge, { NotificationServiceBridge bridge, {
required bool cupertino, required bool cupertino,
Future<void> Function()? requestPermission, Future<void> Function()? requestPermission,
}) => MaterialApp( }) {
theme: ThemeData( final body = SingleChildScrollView(
platform: cupertino ? TargetPlatform.iOS : TargetPlatform.android, child: SettingsDeviceDeliverySection(
), bridge: bridge,
home: Scaffold( requestNotificationPermission: requestPermission,
body: SingleChildScrollView(
child: SettingsDeviceDeliverySection(
bridge: bridge,
requestNotificationPermission: requestPermission,
),
), ),
), );
); return cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(home: Scaffold(body: body));
}
bool _switchValue(WidgetTester tester, {required bool cupertino}) { bool _switchValue(WidgetTester tester, {required bool cupertino}) {
final tile = find.byKey(DeviceDeliveryKeys.toggle); final tile = find.byKey(DeviceDeliveryKeys.toggle);
@@ -88,7 +88,7 @@ void main() {
testWidgets('Cupertino uses inset-grouped sections on the grouped page', ( testWidgets('Cupertino uses inset-grouped sections on the grouped page', (
tester, tester,
) async { ) async {
await _pump(tester, platform: TargetPlatform.iOS); await _pump(tester, cupertino: true);
expect(find.byType(Card), findsNothing); expect(find.byType(Card), findsNothing);
expect(find.byType(CupertinoListSection), findsWidgets); expect(find.byType(CupertinoListSection), findsWidgets);
@@ -124,7 +124,7 @@ void main() {
testWidgets('Cupertino keeps the compact secondary field label', ( testWidgets('Cupertino keeps the compact secondary field label', (
tester, tester,
) async { ) async {
await _pump(tester, platform: TargetPlatform.iOS); await _pump(tester, cupertino: true);
final label = tester.widget<Text>(find.text('Server URL')); final label = tester.widget<Text>(find.text('Server URL'));
final context = tester.element(find.text('Server URL')); final context = tester.element(find.text('Server URL'));
@@ -191,7 +191,7 @@ void main() {
Future<void> _pump( Future<void> _pump(
WidgetTester tester, { WidgetTester tester, {
TargetPlatform platform = TargetPlatform.android, bool cupertino = false,
NotificationController? notifications, NotificationController? notifications,
Size size = const Size(800, 1600), Size size = const Size(800, 1600),
double topPadding = 0, double topPadding = 0,
@@ -201,24 +201,28 @@ Future<void> _pump(
final factory = FakeChannelFactory(); final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call); final client = transport.PipelinesClient(channelFactory: factory.call);
addTearDown(client.dispose); addTearDown(client.dispose);
final screen = SettingsScreen(
client: client,
store: MemorySettingsStore(),
notifications: notifications,
embedded: true,
);
Widget padded(BuildContext context, Widget? child) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(padding: EdgeInsets.only(top: topPadding)),
child: child!,
);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( cupertino
theme: ThemeData(platform: platform), ? CupertinoApp(
builder: (context, child) => MediaQuery( builder: padded,
data: MediaQuery.of( home: CupertinoPageScaffold(child: screen),
context, )
).copyWith(padding: EdgeInsets.only(top: topPadding)), : MaterialApp(
child: child!, builder: padded,
), home: Scaffold(body: screen),
home: Scaffold( ),
body: SettingsScreen(
client: client,
store: MemorySettingsStore(),
notifications: notifications,
embedded: true,
),
),
),
); );
await tester.pump(); await tester.pump();
await tester.pump(); await tester.pump();
@@ -33,16 +33,14 @@ JsonMap _reply(bool run, bool needs, bool reply) => {
}, },
}; };
Widget _host(ApiGateway gateway, {required bool cupertino}) => MaterialApp( Widget _host(ApiGateway gateway, {required bool cupertino}) {
theme: ThemeData( final body = SingleChildScrollView(
platform: cupertino ? TargetPlatform.iOS : TargetPlatform.android, child: SettingsNtfySection(gateway: gateway, connected: true),
), );
home: Scaffold( return cupertino
body: SingleChildScrollView( ? CupertinoApp(home: CupertinoPageScaffold(child: body))
child: SettingsNtfySection(gateway: gateway, connected: true), : MaterialApp(home: Scaffold(body: body));
), }
),
);
bool _checked(WidgetTester tester, String key, {required bool cupertino}) { bool _checked(WidgetTester tester, String key, {required bool cupertino}) {
final tile = find.byKey(ntfySettingKey(key)); final tile = find.byKey(ntfySettingKey(key));
@@ -242,14 +242,26 @@ void main() {
}, },
); );
testWidgets('iOS uses Cupertino fields, buttons and navigation', ( testWidgets('iOS uses the same Material fields and buttons', (tester) async {
final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call);
addTearDown(client.dispose);
await _pump(tester, client, platform: TargetPlatform.iOS);
expect(find.byType(CupertinoTextField), findsNothing);
expect(find.byType(TextField), findsWidgets);
expect(find.byKey(SettingsKeys.connect), findsOneWidget);
});
testWidgets('an explicit Cupertino host uses Cupertino fields and buttons', (
tester, tester,
) async { ) async {
final factory = FakeChannelFactory(); final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call); final client = transport.PipelinesClient(channelFactory: factory.call);
addTearDown(client.dispose); addTearDown(client.dispose);
await _pump(tester, client, platform: TargetPlatform.iOS); await _pump(tester, client, cupertino: true);
expect(find.byType(SegmentedBar), findsOneWidget); expect(find.byType(SegmentedBar), findsOneWidget);
expect(find.byType(CupertinoTextField), findsNWidgets(2)); expect(find.byType(CupertinoTextField), findsNWidgets(2));
@@ -354,11 +366,25 @@ Future<void> _pump(
transport.PipelinesClient client, { transport.PipelinesClient client, {
SettingsStore? store, SettingsStore? store,
TargetPlatform platform = TargetPlatform.android, TargetPlatform platform = TargetPlatform.android,
bool cupertino = false,
double textScale = 1, double textScale = 1,
Size size = const Size(800, 1200), Size size = const Size(800, 1200),
}) async { }) async {
await tester.binding.setSurfaceSize(size); await tester.binding.setSurfaceSize(size);
addTearDown(() => tester.binding.setSurfaceSize(null)); addTearDown(() => tester.binding.setSurfaceSize(null));
if (cupertino) {
await tester.pumpWidget(
CupertinoApp(
home: SettingsScreen(
client: client,
store: store ?? MemorySettingsStore(),
),
),
);
await tester.pump();
await tester.pump();
return;
}
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
theme: ThemeData(platform: platform), theme: ThemeData(platform: platform),
@@ -1,3 +1,4 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart' import 'package:pipelines_app/core/transport/pipelines_client.dart'
@@ -22,23 +23,19 @@ void main() {
final factory = FakeChannelFactory(); final factory = FakeChannelFactory();
final client = transport.PipelinesClient(channelFactory: factory.call); final client = transport.PipelinesClient(channelFactory: factory.call);
addTearDown(client.dispose); addTearDown(client.dispose);
final shell = AdaptiveShell(
cupertino: cupertino,
destination: AppDestination.settings,
destinationBodies: {
AppDestination.settings: (_) => SettingsScreen(
client: client,
store: MemorySettingsStore(),
embedded: true,
),
},
);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( cupertino ? CupertinoApp(home: shell) : MaterialApp(home: shell),
theme: ThemeData(
platform: cupertino ? TargetPlatform.iOS : TargetPlatform.android,
),
home: AdaptiveShell(
cupertino: cupertino,
destination: AppDestination.settings,
destinationBodies: {
AppDestination.settings: (_) => SettingsScreen(
client: client,
store: MemorySettingsStore(),
embedded: true,
),
},
),
),
); );
await tester.pump(); await tester.pump();
await tester.pump(); await tester.pump();
+19 -22
View File
@@ -7,35 +7,32 @@ import 'package:pipelines_app/ui/adaptive/platform.dart';
import 'package:pipelines_app/ui/theme/app_theme.dart'; import 'package:pipelines_app/ui/theme/app_theme.dart';
void main() { void main() {
test('web uses Material on every host; native Apple uses Cupertino', () { test('no platform prefers the Cupertino host', () {
for (final platform in TargetPlatform.values) { expect(prefersCupertino(), isFalse);
expect(prefersCupertino(web: true, platform: platform), isFalse);
expect(
prefersCupertino(web: false, platform: platform),
platform == TargetPlatform.iOS || platform == TargetPlatform.macOS,
);
}
}); });
for (final platform in [TargetPlatform.macOS, TargetPlatform.iOS]) { for (final platform in TargetPlatform.values) {
testWidgets('app and themes select the right controls on $platform', ( testWidgets('the app uses the Material host on $platform', (tester) async {
tester,
) async {
debugDefaultTargetPlatformOverride = platform; debugDefaultTargetPlatformOverride = platform;
addTearDown(() => debugDefaultTargetPlatformOverride = null); addTearDown(() => debugDefaultTargetPlatformOverride = null);
await tester.pumpWidget(const MyApp()); await tester.pumpWidget(const MyApp());
if (kIsWeb) { expect(find.byType(MaterialApp), findsOneWidget);
expect(find.byType(MaterialApp), findsOneWidget); expect(find.byType(CupertinoApp), findsNothing);
expect(find.byType(CupertinoApp), findsNothing); // Native hosts keep the target platform so Flutter's automatic
expect(appLightTheme().platform, TargetPlatform.android); // behaviours (scroll physics, text selection, back-swipe) still apply.
expect(appDarkTheme().platform, TargetPlatform.android); final expected = kIsWeb ? TargetPlatform.android : platform;
} else { expect(appLightTheme().platform, expected);
expect(find.byType(CupertinoApp), findsOneWidget); expect(appDarkTheme().platform, expected);
expect(appLightTheme().platform, platform);
expect(appDarkTheme().platform, platform);
}
await tester.pumpWidget(const SizedBox.shrink()); await tester.pumpWidget(const SizedBox.shrink());
debugDefaultTargetPlatformOverride = null; debugDefaultTargetPlatformOverride = null;
}); });
} }
testWidgets('an explicit cupertino choice still builds the Cupertino host', (
tester,
) async {
await tester.pumpWidget(const MyApp(cupertino: true));
expect(find.byType(CupertinoApp), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
} }