Skip to content

Commit

Permalink
feat: PlatformTextButton add
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Sep 25, 2022
1 parent ea4e61d commit e065399
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 2 deletions.
4 changes: 4 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

linter:
rules:
library_prefixes: false
12 changes: 10 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:platform_ui/platform_ui.dart';

void main() {
platform = TargetPlatform.windows;
runApp(const MyApp());
}

Expand All @@ -11,6 +10,7 @@ class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
platform = TargetPlatform.macOS;
return const PlatformApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
Expand Down Expand Up @@ -60,7 +60,15 @@ class _MyHomePageState extends State<MyHomePage> {
_counter++;
});
},
)
),
PlatformTextButton(
child: const Text("Text Button"),
onPressed: () {
setState(() {
_counter++;
});
},
),
],
),
),
Expand Down
1 change: 1 addition & 0 deletions lib/platform_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export 'src/platform_app.dart';
export 'src/platform_mixin.dart';
export 'src/platform_scaffold.dart';
export 'src/platform_filled_button.dart';
export 'src/platform_text_button.dart';
172 changes: 172 additions & 0 deletions lib/src/platform_text_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// ignore_for_file: library_prefixes

import 'package:fluent_ui/fluent_ui.dart' as FluentUI;
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:platform_ui/platform_ui.dart';
import "package:platform_ui/src/utils.dart";

class PlatformTextButton extends StatelessWidget with PlatformMixin<Widget> {
final VoidCallback? onPressed;
final VoidCallback? onLongPress;
final void Function(PointerHoverEvent)? onHover;
final ValueChanged<bool>? onFocusChange;
final ButtonStyle? style;
final Clip clipBehavior;
final FocusNode? focusNode;
final bool autofocus;
final Widget child;
final double macOSiOSPressedOpacity;
final MouseCursor mouseCursor;

const PlatformTextButton({
Key? key,
required this.child,
required this.onPressed,
this.onLongPress,
this.onFocusChange,
this.style,
this.focusNode,
this.autofocus = false,
this.clipBehavior = Clip.none,
this.mouseCursor = SystemMouseCursors.click,
this.onHover,
this.macOSiOSPressedOpacity = .4,
}) : super(key: key);

final allStates = const {
MaterialState.focused,
MaterialState.disabled,
MaterialState.error,
MaterialState.hovered,
MaterialState.pressed,
MaterialState.selected,
};

@override
Widget android() {
return MouseRegion(
onHover: onHover,
cursor: mouseCursor,
child: TextButton(
onPressed: onPressed,
onLongPress: onLongPress,
onFocusChange: onFocusChange,
style: style,
clipBehavior: clipBehavior,
focusNode: focusNode,
autofocus: autofocus,
child: child,
),
);
}

BorderRadius? get borderRadius {
final shape = style?.shape?.resolve(allStates);
if (shape is RoundedRectangleBorder && shape.borderRadius is BorderRadius) {
return shape.borderRadius as BorderRadius;
}
return null;
}

@override
Widget ios() {
return ClipRect(
clipBehavior: clipBehavior,
child: Focus(
autofocus: autofocus,
focusNode: focusNode,
child: MouseRegion(
onHover: onHover,
cursor: mouseCursor,
child: GestureDetector(
onLongPress: onLongPress,
child: CupertinoButton(
color: style?.foregroundColor?.resolve(allStates),
onPressed: onPressed,
pressedOpacity: macOSiOSPressedOpacity,
borderRadius:
borderRadius ?? const BorderRadius.all(Radius.circular(8.0)),
minSize: style?.minimumSize?.resolve(allStates)?.width,
disabledColor:
style?.backgroundColor?.resolve({MaterialState.disabled}) ??
CupertinoColors.quaternarySystemFill,
padding: style?.padding?.resolve(allStates),
child: child,
),
),
),
),
);
}

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

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

@override
Widget windows() {
return ClipRect(
clipBehavior: clipBehavior,
child: MouseRegion(
cursor: mouseCursor,
onHover: onHover,
child: FluentUI.TextButton(
onPressed: onPressed,
onLongPress: onLongPress,
autofocus: autofocus,
focusNode: focusNode,
style: FluentUI.ButtonStyle(
backgroundColor:
Utils.resolveMaterialPropertyAsAllButtonState<Color?>(
allStates,
style?.backgroundColor,
),
elevation: Utils.resolveMaterialPropertyAsAllButtonState<double?>(
allStates,
style?.elevation,
),
foregroundColor:
Utils.resolveMaterialPropertyAsAllButtonState<Color?>(
allStates,
style?.foregroundColor,
),
padding: Utils.resolveMaterialPropertyAsAllButtonState(
allStates,
style?.padding,
),
shadowColor: Utils.resolveMaterialPropertyAsAllButtonState<Color?>(
allStates,
style?.shadowColor,
),
textStyle: Utils.resolveMaterialPropertyAsAllButtonState(
allStates,
style?.textStyle,
),
shape: Utils.resolveMaterialPropertyAsAllButtonState(
allStates,
style?.shape,
),
border: Utils.resolveMaterialPropertyAsAllButtonState(
allStates,
style?.side,
),
),
child: child,
),
),
);
}

@override
Widget build(BuildContext context) {
return getPlatformType(context);
}
}

0 comments on commit e065399

Please sign in to comment.