Skip to content

Commit 6f45590

Browse files
author
June
committed
🔥 listen
1 parent e6110b1 commit 6f45590

File tree

5 files changed

+54
-35
lines changed

5 files changed

+54
-35
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
## 0.0.1
22

33
* initial
4+
5+
## 0.0.2
6+
7+
* add listen mode

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
https://user-images.githubusercontent.com/9412501/159013153-79af72be-30e9-4d92-b34e-7af11c772812.mp4
88

99
# how to use
10-
[![pub](https://img.shields.io/badge/pub-v0.0.1-green)](https://pub.dev/packages/parallaxj)
10+
[![pub](https://img.shields.io/badge/pub-v0.0.2-green)](https://pub.dev/packages/parallaxj)
1111
```dart
1212
Parallaxable(
1313
offsetRadio: 1.0 / 10,

example/.packages

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# For more info see: https://dart.dev/go/dot-packages-deprecation
55
#
6-
# Generated by pub on 2022-03-07 23:42:01.403198.
6+
# Generated by pub on 2022-03-30 23:20:46.643555.
77
async:file:///C:/Users/jiang/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/async-2.8.2/lib/
88
boolean_selector:file:///C:/Users/jiang/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/boolean_selector-2.1.0/lib/
99
characters:file:///C:/Users/jiang/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/characters-1.2.0/lib/

lib/src/parallax_touch.dart

+47-32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Parallaxable extends StatefulWidget {
1010
final double rotateDiff;
1111
final Widget above;
1212
final Widget under;
13+
final bool listen;
1314

1415
const Parallaxable({
1516
Key? key,
@@ -19,6 +20,7 @@ class Parallaxable extends StatefulWidget {
1920
this.rotateDiff = 1.1,
2021
this.offsetRadio = 1.0 / 6,
2122
this.offsetDepth = 2,
23+
this.listen = false,
2224
}) : super(key: key);
2325

2426
@override
@@ -55,63 +57,76 @@ class _ParallaxableState extends State<Parallaxable> with SingleTickerProviderSt
5557
builder: (BuildContext context, Widget? child) {
5658
final double rotatex = widget.angle * _aniController.value * xpercent;
5759
final double rotatey = widget.angle * _aniController.value * ypercent;
58-
final double translatex =
59-
widget.offsetRadio == 0 ? 0 : _aniController.value * halfHeight * widget.offsetRadio * ypercent * -1;
60+
final double translatex = widget.offsetRadio == 0
61+
? 0
62+
: _aniController.value * halfHeight * widget.offsetRadio * ypercent * -1;
6063
// double translatex = _aniController.value * halfHeight * widget.offsetRadio * ypercent;
6164
final double translatey =
6265
widget.offsetRadio == 0 ? 0 : _aniController.value * halfWidth * widget.offsetRadio * xpercent;
63-
return GestureDetector(
64-
onPanEnd: _panEnd,
65-
onPanUpdate: _panUpdate,
66-
onTapUp: _tapUp,
67-
onPanDown: _panDown,
68-
child: Stack(
69-
children: [
70-
Transform(
71-
transform: Matrix4.identity()
72-
..setEntry(3, 2, 0.001)
73-
..rotateY(rotatey)
74-
..rotateX(rotatex),
75-
alignment: Alignment.center,
76-
child: widget.under),
77-
Transform(
66+
final stack = Stack(
67+
children: [
68+
Transform(
7869
transform: Matrix4.identity()
7970
..setEntry(3, 2, 0.001)
80-
..rotateY(rotatey / widget.rotateDiff)
81-
..translate(translatex, translatey, widget.offsetDepth)
82-
..rotateX(rotatex / widget.rotateDiff),
71+
..rotateY(rotatey)
72+
..rotateX(rotatex),
8373
alignment: Alignment.center,
84-
child: widget.above,
85-
),
86-
],
87-
),
74+
child: widget.under),
75+
Transform(
76+
transform: Matrix4.identity()
77+
..setEntry(3, 2, 0.001)
78+
..rotateY(rotatey / widget.rotateDiff)
79+
..translate(translatex, translatey, widget.offsetDepth)
80+
..rotateX(rotatex / widget.rotateDiff),
81+
alignment: Alignment.center,
82+
child: widget.above,
83+
),
84+
],
85+
);
86+
if (widget.listen) {
87+
return Listener(
88+
onPointerMove: (event) => _panUpdate(event.localPosition.dx, event.localPosition.dy),
89+
onPointerCancel: (event) => _panEnd(),
90+
onPointerDown: (event) => _panDown(event.localPosition.dx, event.localPosition.dy),
91+
onPointerUp: (event) => _tapUp(),
92+
child: stack,
93+
);
94+
}
95+
96+
return GestureDetector(
97+
onPanCancel: _panEnd,
98+
onPanEnd: (event) => _panEnd(),
99+
onPanUpdate: (event) => _panUpdate(event.localPosition.dx, event.localPosition.dy),
100+
onTapUp: (event) => _tapUp(),
101+
onPanDown: (event) => _panDown(event.localPosition.dx, event.localPosition.dy),
102+
child: stack,
88103
);
89104
},
90105
animation: _aniController,
91106
);
92107
});
93108
}
94109

95-
void _panUpdate(DragUpdateDetails details) {
110+
void _panUpdate(double dx, double dy) {
96111
setState(() {
97-
ypercent = ((halfWidth - details.localPosition.dx) / halfWidth).clamp(-1, 1);
98-
xpercent = ((details.localPosition.dy - halfHeight) / halfHeight).clamp(-1, 1);
112+
ypercent = ((halfWidth - dx) / halfWidth).clamp(-1, 1);
113+
xpercent = ((dy - halfHeight) / halfHeight).clamp(-1, 1);
99114
});
100115
}
101116

102-
void _panDown(DragDownDetails details) {
117+
void _panDown(double dx, double dy) {
103118
_aniController.forward();
104119
setState(() {
105-
ypercent = (halfWidth - details.localPosition.dx) / halfWidth;
106-
xpercent = (details.localPosition.dy - halfHeight) / halfHeight;
120+
ypercent = (halfWidth - dx) / halfWidth;
121+
xpercent = (dy - halfHeight) / halfHeight;
107122
});
108123
}
109124

110-
void _tapUp(TapUpDetails details) {
125+
void _tapUp() {
111126
_aniController.reverse();
112127
}
113128

114-
void _panEnd(DragEndDetails details) {
129+
void _panEnd() {
115130
_aniController.reverse();
116131
}
117132
}

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: parallaxj
22
description: A new Flutter project.
3-
version: 0.0.1
3+
version: 0.0.2
44
homepage: https://github.com/ZuYun/parallaxj
55

66
environment:

0 commit comments

Comments
 (0)