Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add PlatformWidgetBuilder. #319

Merged
merged 9 commits into from
Mar 21, 2024
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'platform/catalyst_platform.dart';
export 'platform_widget_builder/platform_widget_builder.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:catalyst_voices_shared/src/platform/catalyst_platform.dart';
import 'package:flutter/widgets.dart';

class PlatformWidgetBuilder<T> extends StatelessWidget {
Mr-Leshiy marked this conversation as resolved.
Show resolved Hide resolved
coire1 marked this conversation as resolved.
Show resolved Hide resolved
minikin marked this conversation as resolved.
Show resolved Hide resolved
final Widget Function(BuildContext context, T? data) builder;
final T? android;
minikin marked this conversation as resolved.
Show resolved Hide resolved
final T? desktop;
final T? fuchsia;
final T? iOS;
final T? linux;
final T? macOS;
final T? mobile;
final T? mobileWeb;
final T? web;
final T? webDesktop;
final T? windows;
final T? other;

const PlatformWidgetBuilder({
super.key,
required this.builder,
this.android,
this.desktop,
this.fuchsia,
this.iOS,
this.linux,
this.macOS,
this.mobile,
this.mobileWeb,
this.web,
this.webDesktop,
this.windows,
this.other,
});

@override
Widget build(BuildContext context) {
minikin marked this conversation as resolved.
Show resolved Hide resolved
if (CatalystPlatform.isAndroid && android != null) {
return builder(context, android as T);
} else if (CatalystPlatform.isDesktop && desktop != null) {
return builder(context, desktop as T);
} else if (CatalystPlatform.isFuchsia && fuchsia != null) {
return builder(context, fuchsia as T);
} else if (CatalystPlatform.isIOS && iOS != null) {
return builder(context, iOS as T);
} else if (CatalystPlatform.isLinux && linux != null) {
return builder(context, linux as T);
} else if (CatalystPlatform.isMacOS && macOS != null) {
return builder(context, linux as T);
} else if (CatalystPlatform.isMobile && mobile != null) {
return builder(context, mobile as T);
} else if (CatalystPlatform.isMobileWeb && mobileWeb != null) {
return builder(context, mobileWeb as T);
} else if (CatalystPlatform.isWeb && web != null) {
return builder(context, web as T);
} else if (CatalystPlatform.isWebDesktop && webDesktop != null) {
return builder(context, webDesktop as T);
} else if (CatalystPlatform.isWindows && windows != null) {
return builder(context, windows as T);
} else if (other != null) {
return builder(context, other as T);
} else {
return builder(context, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ dependencies:
dev_dependencies:
catalyst_analysis:
path: ../../../catalyst_voices_packages/catalyst_analysis
flutter_test:
sdk: flutter
test: ^1.24.9

Original file line number Diff line number Diff line change
@@ -1 +1 @@
void main() {}
export 'platform_widget_builder/platform_widget_builder_test.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:catalyst_voices_shared/catalyst_voices_shared.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {

Widget buildApp() => MaterialApp(
home: Scaffold(
body: PlatformWidgetBuilder<String>(
other: 'other',
builder: (context, platformData) => Text(platformData!),
),
),
);

group('Test platform detection', () {
testWidgets('PlatformWidgetBuilder fallbacks to other', (tester) async {
await tester.pumpWidget(buildApp());
// Verify the Widget renders properly
expect(find.byType(Text), findsOneWidget);
// Check the output contains the platform that was tested.
expect(find.text('other'), findsOneWidget);

});
});
}
Loading