Skip to content

Commit

Permalink
fix: update widget properties
Browse files Browse the repository at this point in the history
  • Loading branch information
vaetas committed May 20, 2022
1 parent 54b33a1 commit e92de4e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 29 deletions.
7 changes: 5 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class HomeScreen extends StatelessWidget {
onPressed: () {
print('[HomeScreen.build] Scale pressed');
},
theme: const PressableScaleTheme(
scaleFactor: 0.8,
),
// onLongPressed: () {
// print('[HomeScreen.build] Scale long-pressed');
// },
Expand All @@ -65,9 +68,9 @@ class HomeScreen extends StatelessWidget {
onPressed: () {
print('[HomeScreen.build] Opacity pressed');
},
theme: PressableOpacityTheme(
theme: const PressableOpacityTheme(
curve: Curves.easeOut,
opacityFactor: 0.5,
opacityFactor: 0.4,
),
child: const ExampleButton(title: 'Opacity'),
),
Expand Down
2 changes: 2 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';

/// Default [State] for more complex [Pressable] Widgets.
abstract class PressableBaseState<T extends StatefulWidget> extends State<T> {
bool isPressed = false;

Expand Down
3 changes: 3 additions & 0 deletions lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';
import 'package:pressable/src/base.dart';

/// Builds [Widget] inside [PressableBuilder].
typedef PressableBuilderCallback = Widget Function(
BuildContext context,
bool isPressed,
);

/// Use [PressableBuilder] to define your own pressable animation. Simplifies
/// working with [GestureDetector].
class PressableBuilder extends Pressable {
const PressableBuilder({
Key? key,
Expand Down
1 change: 1 addition & 0 deletions lib/src/fill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';
import 'package:pressable/src/base.dart';

/// Fills [child] with selected color when pressed.
class PressableFill extends Pressable {
const PressableFill({
Key? key,
Expand Down
48 changes: 34 additions & 14 deletions lib/src/opacity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';
import 'package:pressable/src/base.dart';

/// Makes [child] transparent.
/// Makes [child] semi-transparent when pressed.
class PressableOpacity extends Pressable {
const PressableOpacity({
Key? key,
Expand All @@ -24,19 +24,9 @@ class PressableOpacity extends Pressable {

class _PressableOpacityState extends PressableBaseState<PressableOpacity>
with TickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
duration: theme.duration,
vsync: this,
value: 1.0,
lowerBound: theme.opacityFactor,
upperBound: 1.0,
);

late final Animation<double> _animation = CurvedAnimation(
parent: _controller,
curve: theme.curve,
reverseCurve: theme.reverseCurve,
);
late AnimationController _controller = _createAnimationController();

late Animation<double> _animation = _createAnimation();

PressableOpacityTheme get theme {
return widget.theme ??
Expand All @@ -46,6 +36,8 @@ class _PressableOpacityState extends PressableBaseState<PressableOpacity>

@override
Widget build(BuildContext context) {
assert(theme.opacityFactor < 1.0, 'Opacity factor must be less than 1.0');

return GestureDetector(
onTap: widget.onPressed,
onTapDown: widget.onPressed != null ? onPressStarted : null,
Expand Down Expand Up @@ -101,6 +93,34 @@ class _PressableOpacityState extends PressableBaseState<PressableOpacity>
_revertAnimation();
}

AnimationController _createAnimationController() {
return AnimationController(
duration: theme.duration,
vsync: this,
value: 1.0,
lowerBound: theme.opacityFactor,
upperBound: 1.0,
);
}

CurvedAnimation _createAnimation() {
return CurvedAnimation(
parent: _controller,
curve: theme.curve,
reverseCurve: theme.reverseCurve,
);
}

@override
void didUpdateWidget(covariant PressableOpacity oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.theme != widget.theme) {
_controller.dispose();
_controller = _createAnimationController();
_animation = _createAnimation();
}
}

@override
void dispose() {
_controller.dispose();
Expand Down
1 change: 1 addition & 0 deletions lib/src/platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';

/// Define default [Pressable] animation for each platform.
class PressablePlatform extends Pressable {
const PressablePlatform({
Key? key,
Expand Down
1 change: 1 addition & 0 deletions lib/src/ripple.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';

/// Uses default [InkWell] animation.
class PressableRipple extends Pressable {
const PressableRipple({
Key? key,
Expand Down
47 changes: 34 additions & 13 deletions lib/src/scale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:pressable/pressable.dart';
import 'package:pressable/src/base.dart';

/// Scales [child] down when pressed.
class PressableScale extends Pressable {
const PressableScale({
Key? key,
Expand All @@ -22,19 +23,9 @@ class PressableScale extends Pressable {

class _PressableScaleState extends PressableBaseState<PressableScale>
with TickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
duration: theme.duration,
vsync: this,
value: 1.0,
lowerBound: theme.scaleFactor,
upperBound: 1.0,
);

late final Animation<double> _animation = CurvedAnimation(
parent: _controller,
curve: theme.curve,
reverseCurve: theme.reverseCurve,
);
late AnimationController _controller = _createAnimationController();

late Animation<double> _animation = _createAnimation();

PressableScaleTheme get theme {
return widget.theme ??
Expand All @@ -44,6 +35,8 @@ class _PressableScaleState extends PressableBaseState<PressableScale>

@override
Widget build(BuildContext context) {
assert(theme.scaleFactor < 1.0, 'Scale factor must be less than 1.0');

return GestureDetector(
onTap: widget.onPressed,
onTapDown: widget.onPressed != null ? onPressStarted : null,
Expand Down Expand Up @@ -103,6 +96,34 @@ class _PressableScaleState extends PressableBaseState<PressableScale>
_revertAnimation();
}

AnimationController _createAnimationController() {
return AnimationController(
duration: theme.duration,
vsync: this,
value: 1.0,
lowerBound: theme.scaleFactor,
upperBound: 1.0,
);
}

CurvedAnimation _createAnimation() {
return CurvedAnimation(
parent: _controller,
curve: theme.curve,
reverseCurve: theme.reverseCurve,
);
}

@override
void didUpdateWidget(covariant PressableScale oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.theme != widget.theme) {
_controller.dispose();
_controller = _createAnimationController();
_animation = _createAnimation();
}
}

@override
void dispose() {
_controller.dispose();
Expand Down

0 comments on commit e92de4e

Please sign in to comment.