Skip to content

Commit

Permalink
feat: pass context to platform specific methods
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Oct 5, 2022
1 parent 23b244f commit ed40eaa
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 47 deletions.
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
platform = TargetPlatform.windows;
platform = TargetPlatform.android;
return const PlatformApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
Expand Down
12 changes: 6 additions & 6 deletions lib/src/platform_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class PlatformApp extends StatelessWidget with PlatformMixin<Widget> {
}) : super(key: key);

@override
Widget android() {
Widget android(context) {
return MaterialApp(
navigatorKey: navigatorKey,
scaffoldMessengerKey: scaffoldMessengerKey,
Expand Down Expand Up @@ -134,7 +134,7 @@ class PlatformApp extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget ios() {
Widget ios(context) {
return CupertinoApp(
navigatorKey: navigatorKey,
home: home,
Expand Down Expand Up @@ -168,12 +168,12 @@ class PlatformApp extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget linux() {
return android();
Widget linux(context) {
return android(context);
}

@override
Widget macos() {
Widget macos(context) {
return MacosApp(
navigatorKey: navigatorKey,
home: home,
Expand Down Expand Up @@ -208,7 +208,7 @@ class PlatformApp extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget windows() {
Widget windows(context) {
return FluentApp(
navigatorKey: navigatorKey,
home: home,
Expand Down
14 changes: 7 additions & 7 deletions lib/src/platform_filled_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PlatformFilledButton extends StatelessWidget with PlatformMixin<Widget> {
Set<MaterialState> get allStates => Utils.allMaterialStates;

@override
Widget android() {
Widget android(context) {
return MouseRegion(
onHover: onHover,
cursor: mouseCursor,
Expand All @@ -70,7 +70,7 @@ class PlatformFilledButton extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget ios() {
Widget ios(context) {
return ClipRect(
clipBehavior: clipBehavior,
child: Focus(
Expand Down Expand Up @@ -100,12 +100,12 @@ class PlatformFilledButton extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget linux() {
return android();
Widget linux(context) {
return android(context);
}

@override
Widget macos() {
Widget macos(context) {
return ClipRect(
clipBehavior: clipBehavior,
child: Focus(
Expand Down Expand Up @@ -138,7 +138,7 @@ class PlatformFilledButton extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget windows() {
Widget windows(context) {
return ClipRect(
clipBehavior: clipBehavior,
child: MouseRegion(
Expand Down Expand Up @@ -192,7 +192,7 @@ class PlatformFilledButton extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget build(BuildContext context) {
Widget build(context) {
return getPlatformType(context);
}
}
12 changes: 6 additions & 6 deletions lib/src/platform_icon_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PlatformIconButton extends StatelessWidget with PlatformMixin<Widget> {
}) : super(key: key);

@override
Widget android() {
Widget android(context) {
return IconButton(
iconSize: iconSize,
visualDensity: visualDensity,
Expand Down Expand Up @@ -77,7 +77,7 @@ class PlatformIconButton extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget ios() {
Widget ios(context) {
final cupertinoButton = CupertinoButton(
alignment: alignment,
onPressed: onPressed,
Expand All @@ -103,12 +103,12 @@ class PlatformIconButton extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget linux() {
return android();
Widget linux(context) {
return android(context);
}

@override
Widget macos() {
Widget macos(context) {
return Focus(
focusNode: focusNode,
autofocus: autofocus,
Expand Down Expand Up @@ -138,7 +138,7 @@ class PlatformIconButton extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget windows() {
Widget windows(context) {
return Align(
alignment: alignment,
child: FluentUI.Tooltip(
Expand Down
25 changes: 12 additions & 13 deletions lib/src/platform_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ import 'package:flutter/material.dart';
TargetPlatform? platform;

mixin PlatformMixin<T> {
T android();
T ios();
T macos();
T windows();
T linux();
T android(BuildContext context);
T ios(BuildContext context);
T macos(BuildContext context);
T windows(BuildContext context);
T linux(BuildContext context);

T getPlatformType(BuildContext context) {
if (kIsWeb) return android();
if (kIsWeb) return android(context);
switch (platform ?? defaultTargetPlatform) {
case TargetPlatform.android:
return android();
case TargetPlatform.iOS:
return ios();
return ios(context);
case TargetPlatform.macOS:
return macos();
return macos(context);
case TargetPlatform.windows:
return windows();
return windows(context);
case TargetPlatform.linux:
return linux();
return linux(context);
case TargetPlatform.android:
default:
return android();
return android(context);
}
}
}
14 changes: 7 additions & 7 deletions lib/src/platform_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PlatformScaffold extends StatelessWidget with PlatformMixin<Widget> {
this.restorationId,
}) : super(key: key);
@override
Widget android() {
Widget android(context) {
return Scaffold(
extendBody: extendBody,
extendBodyBehindAppBar: extendBodyBehindAppBar,
Expand Down Expand Up @@ -88,7 +88,7 @@ class PlatformScaffold extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget ios() {
Widget ios(context) {
return CupertinoPageScaffold(
// TODO: Do something for [CuopertinoNavBar]
backgroundColor: backgroundColor,
Expand All @@ -99,12 +99,12 @@ class PlatformScaffold extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget linux() {
return android();
Widget linux(context) {
return android(context);
}

@override
Widget macos() {
Widget macos(context) {
return MacosScaffold(
// TODO: Do something for [macOS ToolBar]
toolBar: null,
Expand All @@ -118,8 +118,8 @@ class PlatformScaffold extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget windows() {
return android();
Widget windows(context) {
return android(context);
}

@override
Expand Down
14 changes: 7 additions & 7 deletions lib/src/platform_text_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PlatformTextButton extends StatelessWidget with PlatformMixin<Widget> {
};

@override
Widget android() {
Widget android(context) {
return MouseRegion(
onHover: onHover,
cursor: mouseCursor,
Expand All @@ -71,7 +71,7 @@ class PlatformTextButton extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget ios() {
Widget ios(context) {
return ClipRect(
clipBehavior: clipBehavior,
child: Focus(
Expand Down Expand Up @@ -102,17 +102,17 @@ class PlatformTextButton extends StatelessWidget with PlatformMixin<Widget> {
}

@override
Widget linux() {
return android();
Widget linux(context) {
return android(context);
}

@override
Widget macos() {
return ios();
Widget macos(context) {
return ios(context);
}

@override
Widget windows() {
Widget windows(context) {
return ClipRect(
clipBehavior: clipBehavior,
child: MouseRegion(
Expand Down

0 comments on commit ed40eaa

Please sign in to comment.