Skip to content

Commit 0fcbcdf

Browse files
authored
feat: add a smooth transition effect when gestures enter (#29)
1 parent afb5ffb commit 0fcbcdf

15 files changed

+533
-84
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
> [!IMPORTANT]
44
> See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.
55
6+
## 3.3.0
7+
8+
### New features
9+
10+
- By default, add a smooth transition effect when gestures (hover and touch) enter. ([#29](https://github.com/fluttercandies/flutter_tilt/pull/29))
11+
- Configurable via:
12+
- `TiltConfig.enterDuration`
13+
- `TiltConfig.moveDuration`
14+
- `TiltConfig.enterToMoveDuration`
15+
- `TiltConfig.enterToMoveCurve`
16+
17+
If you prefer to disable this effect:
18+
19+
```diff
20+
Tilt(
21+
tiltConfig: TiltConfig(
22+
enterToMoveDuration: Duration.zero,
23+
...
24+
)
25+
...
26+
)
27+
```
28+
629
## 3.2.3
730

831
### Improvements

README-ZH.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<a href="https://pub.dev/packages/flutter_tilt/score"><img src="https://img.shields.io/pub/points/flutter_tilt?color=2E8B57&label=%E5%88%86%E6%95%B0&logo=flutter" alt="pub points" /></a>
2020
<a href="https://www.codefactor.io/repository/github/fluttercandies/flutter_tilt"><img src="https://img.shields.io/codefactor/grade/github/fluttercandies/flutter_tilt?color=0CAB6B&label=%E4%BB%A3%E7%A0%81%E8%B4%A8%E9%87%8F&logo=codefactor" alt="CodeFactor" /></a>
2121
<a href="https://codecov.io/gh/fluttercandies/flutter_tilt"><img src="https://img.shields.io/codecov/c/github/fluttercandies/flutter_tilt?label=%E6%B5%8B%E8%AF%95%E8%A6%86%E7%9B%96&logo=codecov" alt="codecov" /></a>
22-
<a href="https://pub.dev/packages/flutter_tilt"><img src="https://img.shields.io/github/languages/top/fluttercandies/flutter_tilt?color=00B4AB" alt="top language" /></a>
22+
<a href="https://pub.dev/packages/flutter_tilt/license"><img src="https://img.shields.io/github/license/fluttercandies/flutter_tilt?label=%E8%AE%B8%E5%8F%AF%E8%AF%81&color=8EBC06" alt="license" /></a>
2323
</p>
2424

2525
<p align="center">
@@ -318,9 +318,12 @@ tiltStreamController.add(
318318
| enableGestureTouch | `bool` | `true` | Touch 手势触发倾斜。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` |
319319
| enableRevert | `bool` | `true` | 启用倾斜复原,会复原至初始状态。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` <br/> `GesturesType.hover` <br/> `GesturesType.controller` |
320320
| enableOutsideAreaMove | `bool` | `true` | 可以继续在区域外触发倾斜。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` <br/> `GesturesType.controller` |
321+
| enterDuration | `Duration` | `Duration(milliseconds: 1000)` | 手势刚进入时的动画持续时间,需要搭配 `[moveDuration]``[enterToMoveDuration]` 使用。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
321322
| moveDuration | `Duration` | `Duration(milliseconds: 100)` | 手势移动时的动画持续时间。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
323+
| enterToMoveDuration | `Duration` | `Duration(milliseconds: 600)` | 手势从 Enter 过渡至 Move 的持续时间,需要搭配 `[enterDuration]``[moveDuration]` 使用。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
322324
| leaveDuration | `Duration` | `Duration(milliseconds: 300)` | 手势离开后的动画持续时间。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
323325
| moveCurve | `Curve` | `Curves.linear` | 手势移动时的动画曲线。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
326+
| enterToMoveCurve | `Curve` | `Curves.easeOutCubic` | 手势从 Enter 过渡至 Move 的曲线,需要搭配 `[enterToMoveDuration]` 使用。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
324327
| leaveCurve | `Curve` | `Curves.linear` | 手势离开后的动画曲线。 <br/> 仅以下手势生效: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
325328
| controllerMoveDuration | `Duration` | `Duration(milliseconds: 100)` | controller 手势移动时的动画持续时间。 <br/> 仅以下手势生效: <br/> `GesturesType.controller` |
326329
| controllerLeaveDuration | `Duration` | `Duration(milliseconds: 300)` | controller 手势离开后的动画持续时间。 <br/> 仅以下手势生效: <br/> `GesturesType.controller` |

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<a href="https://pub.dev/packages/flutter_tilt/score"><img src="https://img.shields.io/pub/points/flutter_tilt?color=2E8B57&logo=flutter" alt="pub points" /></a>
2020
<a href="https://www.codefactor.io/repository/github/fluttercandies/flutter_tilt"><img src="https://img.shields.io/codefactor/grade/github/fluttercandies/flutter_tilt?color=0CAB6B&logo=codefactor" alt="CodeFactor" /></a>
2121
<a href="https://codecov.io/gh/fluttercandies/flutter_tilt"><img src="https://img.shields.io/codecov/c/github/fluttercandies/flutter_tilt?label=coverage&logo=codecov" alt="codecov" /></a>
22-
<a href="https://pub.dev/packages/flutter_tilt"><img src="https://img.shields.io/github/languages/top/fluttercandies/flutter_tilt?color=00B4AB" alt="top language" /></a>
22+
<a href="https://pub.dev/packages/flutter_tilt/license"><img src="https://img.shields.io/github/license/fluttercandies/flutter_tilt?color=8EBC06" alt="license" /></a>
2323
</p>
2424

2525
<p align="center">
@@ -65,7 +65,7 @@ Check out the [Live Demo][].
6565

6666
- [Sensors compatibility][]
6767

68-
- [Gestures priority](#gestures-priority-)
68+
- [Gesture priority](#gesture-priority-)
6969

7070
- [Simple usage](#simple-usage-)
7171

@@ -160,7 +160,7 @@ Platforms without sensors support do not affect the normal use of other features
160160
<!-- > After that maybe replace it with [DeviceMotionEvent][]. -->
161161
162162
163-
## Gestures priority 📱
163+
## Gesture priority 📱
164164
165165
When multiple gestures are enabled, they are triggered based on priority:
166166
@@ -318,9 +318,12 @@ tiltStreamController.add(
318318
| enableGestureTouch | `bool` | `true` | Touch gesture triggered tilt. <br/> Only the following gestures: <br/> `GesturesType.touch` |
319319
| enableRevert | `bool` | `true` | Enable tilt revert, will revert to the initial state. <br/> Only the following gestures: <br/> `GesturesType.touch` <br/> `GesturesType.hover` <br/> `GesturesType.controller` |
320320
| enableOutsideAreaMove | `bool` | `true` | Tilt can continue to be triggered outside the area. <br/> Only the following gestures: <br/> `GesturesType.touch` <br/> `GesturesType.controller` |
321+
| enterDuration | `Duration` | `Duration(milliseconds: 1000)` | Animation duration during gesture enter, must be used with `[moveDuration]` and `[enterToMoveDuration]`. <br/> Only the following gestures: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
321322
| moveDuration | `Duration` | `Duration(milliseconds: 100)` | Animation duration during gesture move. <br/> Only the following gestures: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
323+
| enterToMoveDuration | `Duration` | `Duration(milliseconds: 600)` | The duration of the transition from enter to move, must be used with `[enterDuration]` and `[moveDuration]`. <br/> Only the following gestures: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
322324
| leaveDuration | `Duration` | `Duration(milliseconds: 300)` | Animation duration after gesture leave. <br/> Only the following gestures: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
323325
| moveCurve | `Curve` | `Curves.linear` | Animation curve during gesture move. <br/> Only the following gestures: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
326+
| enterToMoveCurve | `Curve` | `Curves.easeOutCubic` | The curve of the transition from enter to move, must be used with `[enterToMoveDuration]`. <br/> Only the following gestures: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
324327
| leaveCurve | `Curve` | `Curves.linear` | Animation curve after gesture leave. <br/> Only the following gestures: <br/> `GesturesType.touch` <br/> `GesturesType.hover` |
325328
| controllerMoveDuration | `Duration` | `Duration(milliseconds: 100)` | Animation duration during controller gesture move. <br/> Only the following gestures: <br/> `GesturesType.controller` |
326329
| controllerLeaveDuration | `Duration` | `Duration(milliseconds: 300)` | Animation duration after controller gesture leave. <br/> Only the following gestures: <br/> `GesturesType.controller` |

lib/src/config/tilt_config.dart

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ class TiltConfig {
3333
this.enableGestureTouch = true,
3434
this.enableRevert = true,
3535
this.enableOutsideAreaMove = true,
36+
this.enterDuration = const Duration(milliseconds: 1000),
3637
this.moveDuration = const Duration(milliseconds: 100),
38+
this.enterToMoveDuration = const Duration(milliseconds: 600),
3739
this.leaveDuration = const Duration(milliseconds: 300),
3840
this.moveCurve = Curves.linear,
41+
this.enterToMoveCurve = Curves.easeOutCubic,
3942
this.leaveCurve = Curves.linear,
4043
this.controllerMoveDuration = const Duration(milliseconds: 100),
4144
this.controllerLeaveDuration = const Duration(milliseconds: 300),
@@ -275,6 +278,26 @@ class TiltConfig {
275278
///
276279
final bool enableOutsideAreaMove;
277280

281+
/// Animation duration during gesture enter,
282+
///
283+
/// must be used with [moveDuration] and [enterToMoveDuration].
284+
///
285+
/// Only the following gestures:
286+
/// [GesturesType.touch]
287+
/// [GesturesType.hover]
288+
///
289+
/// ------
290+
///
291+
/// 手势刚进入时的动画持续时间,
292+
///
293+
/// 需要搭配 [moveDuration][enterToMoveDuration] 使用。
294+
///
295+
/// 仅以下手势生效:
296+
/// [GesturesType.touch]
297+
/// [GesturesType.hover]
298+
///
299+
final Duration enterDuration;
300+
278301
/// Animation duration during gesture move.
279302
///
280303
/// Only the following gestures:
@@ -291,6 +314,26 @@ class TiltConfig {
291314
///
292315
final Duration moveDuration;
293316

317+
/// The duration of the transition from enter to move,
318+
///
319+
/// must be used with [enterDuration] and [moveDuration].
320+
///
321+
/// Only the following gestures:
322+
/// [GesturesType.touch]
323+
/// [GesturesType.hover]
324+
///
325+
/// ------
326+
///
327+
/// 手势从 Enter 过渡至 Move 的持续时间,
328+
///
329+
/// 需要搭配 [enterDuration][moveDuration] 使用。
330+
///
331+
/// 仅以下手势生效:
332+
/// [GesturesType.touch]
333+
/// [GesturesType.hover]
334+
///
335+
final Duration enterToMoveDuration;
336+
294337
/// Animation duration after gesture leave.
295338
///
296339
/// Only the following gestures:
@@ -323,6 +366,26 @@ class TiltConfig {
323366
///
324367
final Curve moveCurve;
325368

369+
/// The curve of the transition from enter to move,
370+
///
371+
/// must be used with [enterToMoveDuration].
372+
///
373+
/// Only the following gestures:
374+
/// [GesturesType.touch]
375+
/// [GesturesType.hover]
376+
///
377+
/// ------
378+
///
379+
/// 手势从 Enter 过渡至 Move 的曲线,
380+
///
381+
/// 需要搭配 [enterToMoveDuration] 使用。
382+
///
383+
/// 仅以下手势生效:
384+
/// [GesturesType.touch]
385+
/// [GesturesType.hover]
386+
///
387+
final Curve enterToMoveCurve;
388+
326389
/// Animation curve after gesture leave.
327390
///
328391
/// Only the following gestures:
@@ -382,9 +445,12 @@ class TiltConfig {
382445
bool? enableGestureTouch,
383446
bool? enableRevert,
384447
bool? enableOutsideAreaMove,
448+
Duration? enterDuration,
385449
Duration? moveDuration,
450+
Duration? enterToMoveDuration,
386451
Duration? leaveDuration,
387452
Curve? moveCurve,
453+
Curve? enterToMoveCurve,
388454
Curve? leaveCurve,
389455
Duration? controllerMoveDuration,
390456
Duration? controllerLeaveDuration,
@@ -406,9 +472,12 @@ class TiltConfig {
406472
enableRevert: enableRevert ?? this.enableRevert,
407473
enableOutsideAreaMove:
408474
enableOutsideAreaMove ?? this.enableOutsideAreaMove,
475+
enterDuration: enterDuration ?? this.enterDuration,
409476
moveDuration: moveDuration ?? this.moveDuration,
477+
enterToMoveDuration: enterToMoveDuration ?? this.enterToMoveDuration,
410478
leaveDuration: leaveDuration ?? this.leaveDuration,
411479
moveCurve: moveCurve ?? this.moveCurve,
480+
enterToMoveCurve: enterToMoveCurve ?? this.enterToMoveCurve,
412481
leaveCurve: leaveCurve ?? this.leaveCurve,
413482
controllerMoveDuration:
414483
controllerMoveDuration ?? this.controllerMoveDuration,
@@ -442,9 +511,12 @@ class TiltConfig {
442511
other.enableGestureTouch == enableGestureTouch &&
443512
other.enableRevert == enableRevert &&
444513
other.enableOutsideAreaMove == enableOutsideAreaMove &&
514+
other.enterDuration == enterDuration &&
445515
other.moveDuration == moveDuration &&
516+
other.enterToMoveDuration == enterToMoveDuration &&
446517
other.leaveDuration == leaveDuration &&
447518
other.moveCurve == moveCurve &&
519+
other.enterToMoveCurve == enterToMoveCurve &&
448520
other.leaveCurve == leaveCurve &&
449521
other.controllerMoveDuration == controllerMoveDuration &&
450522
other.controllerLeaveDuration == controllerLeaveDuration;
@@ -468,9 +540,12 @@ class TiltConfig {
468540
enableGestureTouch,
469541
enableRevert,
470542
enableOutsideAreaMove,
543+
enterDuration,
471544
moveDuration,
545+
enterToMoveDuration,
472546
leaveDuration,
473547
moveCurve,
548+
enterToMoveCurve,
474549
leaveCurve,
475550
controllerMoveDuration,
476551
controllerLeaveDuration,

lib/src/internal/controllers/tilt_gestures_controller.dart

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,40 @@ class TiltGesturesController {
8484

8585
/// 过滤 TiltStream
8686
TiltStreamModel filterTiltStream(TiltStreamModel tiltStreamModel) {
87-
/// 当前手势是否高优先级
88-
final isHighPriority = gesturesTypePriority(
89-
tiltStreamModel.gesturesType,
90-
latestTiltStreamModel.gesturesType,
91-
) ==
92-
tiltStreamModel.gesturesType;
93-
9487
switch (tiltStreamModel.gesturesType) {
9588
case GesturesType.none:
9689
break;
9790
case GesturesType.touch || GesturesType.hover || GesturesType.controller:
98-
if (isHighPriority || !latestTiltStreamModel.gestureUse) {
99-
latestTiltStreamModel = tiltStreamModel;
100-
}
91+
{
92+
if (latestTiltStreamModel == tiltStreamModel) {
93+
return tiltStreamModel;
94+
}
10195

102-
/// 避免 sensors 与其他手势触发冲突
103-
if (!tiltStreamModel.gestureUse) {
104-
_handleGestureConflict(tiltStreamModel.gesturesType);
105-
_enableSensors = true;
106-
} else {
107-
_enableSensors = false;
96+
/// 当前手势是否高优先级
97+
final isHighPriority = gesturesTypePriority(
98+
tiltStreamModel.gesturesType,
99+
latestTiltStreamModel.gesturesType,
100+
) ==
101+
tiltStreamModel.gesturesType;
102+
103+
if (isHighPriority || !latestTiltStreamModel.gestureUse) {
104+
latestTiltStreamModel = tiltStreamModel;
105+
}
106+
107+
/// 避免 sensors 与其他手势触发冲突
108+
if (!tiltStreamModel.gestureUse) {
109+
_handleGestureConflict(tiltStreamModel.gesturesType);
110+
_enableSensors = true;
111+
} else {
112+
_enableSensors = false;
113+
}
108114
}
109115
case GesturesType.sensors:
110-
// 避免 sensors 与其他手势触发冲突
111-
if (_enableSensors && _gesturesHarmonizerTimer == null) {
112-
_updateSensorTiltPosition(tiltStreamModel);
116+
{
117+
/// 避免 sensors 与其他手势触发冲突
118+
if (_enableSensors && _gesturesHarmonizerTimer == null) {
119+
_updateSensorTiltPosition(tiltStreamModel);
120+
}
113121
}
114122
}
115123
return latestTiltStreamModel;
@@ -150,6 +158,7 @@ class TiltGesturesController {
150158
///
151159
/// 避免其他手势离开后的动画与 sensors 冲突(出现闪现)
152160
void _gesturesHarmonizer(Duration duration) {
161+
if (_gesturesHarmonizerTimer != null) return;
153162
_gesturesHarmonizerTimer?.cancel();
154163
_gesturesHarmonizerTimer = async.Timer(
155164
duration,

lib/src/internal/mixin/tilt_decoration_mixin.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:flutter/widgets.dart';
33
import '../../enums.dart';
44
import '../../utils.dart';
55

6-
mixin TiltDecoration {
6+
mixin TiltDecorationMixin {
77
/// 计算提供的方向进度
88
///
99
/// 范围:0-1

0 commit comments

Comments
 (0)