Skip to content

Commit

Permalink
feat: platform pressable
Browse files Browse the repository at this point in the history
  • Loading branch information
vaetas committed Jan 18, 2022
1 parent 86c2eb9 commit 23f00ac
Show file tree
Hide file tree
Showing 16 changed files with 1,422 additions and 85 deletions.
6 changes: 6 additions & 0 deletions .run/example.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="example" type="FlutterRunConfigurationType" factoryName="Flutter">
<option name="filePath" value="$PROJECT_DIR$/example/lib/main.dart" />
<method v="2" />
</configuration>
</component>
4 changes: 2 additions & 2 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 50;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -127,7 +127,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1020;
LastUpgradeCheck = 1300;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
LastUpgradeVersion = "1300"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
8 changes: 8 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ class HomeScreen extends StatelessWidget {
),
child: const ExampleButton(title: 'Fill'),
),
Pressable.platform(
onPressed: () {
print('[HomeScreen.build] Platform pressed');
},
ios: const PressableTheme.opacity(),
android: const PressableTheme.ripple(),
child: const ExampleButton(title: 'Platform'),
),
SizedBox(
width: 150,
child: Stack(
Expand Down
28 changes: 21 additions & 7 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,7 +21,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -74,6 +74,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
freezed_annotation:
dependency: transitive
description:
name: freezed_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.4.0"
lints:
dependency: transitive
description:
Expand All @@ -87,7 +101,7 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
meta:
dependency: transitive
description:
Expand All @@ -108,7 +122,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.1.0"
version: "0.2.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -155,7 +169,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
version: "0.4.3"
typed_data:
dependency: transitive
description:
Expand All @@ -169,7 +183,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
flutter: ">=1.17.0"
39 changes: 34 additions & 5 deletions lib/pressable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import 'package:flutter/material.dart';
import 'package:pressable/src/builder.dart';
import 'package:pressable/src/fill.dart';
import 'package:pressable/src/opacity.dart';
import 'package:pressable/src/platform.dart';
import 'package:pressable/src/ripple.dart';
import 'package:pressable/src/scale.dart';
import 'package:pressable/src/theme/theme.dart';

export 'package:pressable/src/builder.dart' show PressableBuilderCallback;
export 'package:pressable/src/fill.dart' show PressableFillTheme;
export 'package:pressable/src/opacity.dart' show PressableOpacityTheme;
export 'package:pressable/src/ripple.dart' show PressableRippleTheme;
export 'package:pressable/src/scale.dart' show PressableScaleTheme;
export 'package:pressable/src/builder.dart';
export 'package:pressable/src/fill.dart';
export 'package:pressable/src/opacity.dart';
export 'package:pressable/src/ripple.dart';
export 'package:pressable/src/scale.dart';
export 'package:pressable/src/theme/theme.dart';

/// Choose named constructors to pick press effect.
abstract class Pressable extends StatefulWidget {
Expand Down Expand Up @@ -90,6 +93,32 @@ abstract class Pressable extends StatefulWidget {
);
}

/// Provide platform specific [PressableTheme]. You must provide theme for
/// current platform otherwise error is thrown.
factory Pressable.platform({
Key? key,
required Widget child,
VoidCallback? onPressed,
VoidCallback? onLongPressed,
PressableTheme? ios,
PressableTheme? android,
PressableTheme? macOS,
PressableTheme? windows,
PressableTheme? linux,
}) {
return PressablePlatform(
key: key,
ios: ios,
macOS: macOS,
windows: windows,
linux: linux,
android: android,
onPressed: onPressed,
onLongPressed: onLongPressed,
child: child,
);
}

/// Use builder to define your own effect.
factory Pressable.builder({
Key? key,
Expand Down
1 change: 0 additions & 1 deletion lib/src/base.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

abstract class PressableBaseState<T extends StatefulWidget> extends State<T> {
Expand Down
14 changes: 0 additions & 14 deletions lib/src/fill.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';
import 'package:pressable/src/base.dart';
Expand Down Expand Up @@ -35,16 +34,3 @@ class _PressableFillState extends PressableBaseState<PressableFill> {
);
}
}

class PressableFillTheme extends Equatable {
const PressableFillTheme({
this.fillColor = Colors.black38,
this.borderRadius = BorderRadius.zero,
});

final Color fillColor;
final BorderRadius borderRadius;

@override
List<Object?> get props => [fillColor, borderRadius];
}
18 changes: 0 additions & 18 deletions lib/src/opacity.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';
import 'package:pressable/src/base.dart';
Expand Down Expand Up @@ -40,20 +39,3 @@ class _PressableOpacityState extends PressableBaseState<PressableOpacity> {
);
}
}

class PressableOpacityTheme extends Equatable {
const PressableOpacityTheme({
this.duration = const Duration(milliseconds: 100),
this.curve = Curves.linear,
this.opacityFactor = 0.6,
this.backgroundColor = Colors.transparent,
});

final Duration duration;
final double opacityFactor;
final Curve curve;
final Color backgroundColor;

@override
List<Object?> get props => [duration, opacityFactor, curve, backgroundColor];
}
93 changes: 93 additions & 0 deletions lib/src/platform.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';

class PressablePlatform extends Pressable {
const PressablePlatform({
Key? key,
required this.child,
this.onPressed,
this.onLongPressed,
this.ios,
this.android,
this.macOS,
this.windows,
this.linux,
}) : super(key: key);

final Widget child;
final VoidCallback? onPressed;
final VoidCallback? onLongPressed;
final PressableTheme? ios;
final PressableTheme? android;
final PressableTheme? macOS;
final PressableTheme? windows;
final PressableTheme? linux;

@override
_PressablePlatformState createState() => _PressablePlatformState();
}

class _PressablePlatformState extends State<PressablePlatform> {
@override
Widget build(BuildContext context) {
PressableTheme? pressable;
if (Platform.isIOS) {
pressable = widget.ios;
}
if (Platform.isAndroid) {
pressable = widget.android;
}
if (Platform.isMacOS) {
pressable = widget.macOS;
}
if (Platform.isWindows) {
pressable = widget.windows;
}
if (Platform.isLinux) {
pressable = widget.linux;
}

if (pressable == null) {
throw ArgumentError(
'PressableTheme for current platform is not provided.',
);
}

return pressable.map(
ripple: (theme) {
return Pressable.ripple(
onPressed: widget.onPressed,
onLongPressed: widget.onLongPressed,
theme: theme,
child: widget.child,
);
},
scale: (theme) {
return Pressable.scale(
onPressed: widget.onPressed,
onLongPressed: widget.onLongPressed,
theme: theme,
child: widget.child,
);
},
opacity: (theme) {
return Pressable.opacity(
onPressed: widget.onPressed,
onLongPressed: widget.onLongPressed,
theme: theme,
child: widget.child,
);
},
fill: (theme) {
return Pressable.fill(
onPressed: widget.onPressed,
onLongPressed: widget.onLongPressed,
theme: theme,
child: widget.child,
);
},
);
}
}
16 changes: 0 additions & 16 deletions lib/src/ripple.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';

Expand Down Expand Up @@ -34,18 +33,3 @@ class _PressableRippleState extends State<PressableRipple> {
);
}
}

class PressableRippleTheme extends Equatable {
const PressableRippleTheme({
this.splashColor,
this.highlightColor,
this.borderRadius,
});

final Color? splashColor;
final Color? highlightColor;
final BorderRadius? borderRadius;

@override
List<Object?> get props => [splashColor, highlightColor, borderRadius];
}
16 changes: 0 additions & 16 deletions lib/src/scale.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';
import 'package:pressable/src/base.dart';
Expand Down Expand Up @@ -85,18 +84,3 @@ class _PressableScaleState extends PressableBaseState<PressableScale>
super.dispose();
}
}

class PressableScaleTheme extends Equatable {
const PressableScaleTheme({
this.scaleFactor = 0.6,
this.duration = const Duration(milliseconds: 100),
this.curve = Curves.easeInOut,
});

final double scaleFactor;
final Duration duration;
final Curve curve;

@override
List<Object?> get props => [scaleFactor, duration, curve];
}
Loading

0 comments on commit 23f00ac

Please sign in to comment.