1
1
import 'package:flutter/widgets.dart' ;
2
- import 'post_frame_widget.dart' ;
3
2
4
3
import 'apple_curves.dart' ;
5
4
import 'effect_query.dart' ;
5
+ import 'post_frame_widget.dart' ;
6
6
7
7
/// Represents the pointer event for [PointerTransition] .
8
8
class PointerTransitionEvent {
@@ -52,6 +52,7 @@ extension PointerTransitionExt on Widget {
52
52
PointerTransitionBuilder builder, {
53
53
Alignment origin = Alignment .center,
54
54
bool useGlobalPointer = false ,
55
+ bool usePointerRouter = true ,
55
56
bool transitionBetweenBounds = true ,
56
57
bool resetOnExitBounds = true ,
57
58
Curve curve = appleEaseInOut,
@@ -61,6 +62,7 @@ extension PointerTransitionExt on Widget {
61
62
builder: builder,
62
63
origin: origin,
63
64
useGlobalPointer: useGlobalPointer,
65
+ usePointerRouter: usePointerRouter,
64
66
transitionBetweenBounds: transitionBetweenBounds,
65
67
resetOnExitBounds: resetOnExitBounds,
66
68
curve: curve,
@@ -87,7 +89,7 @@ class PointerTransition extends StatefulWidget {
87
89
88
90
/// Decides whether this transition calculates the value based on the global
89
91
/// position of the pointer device or the local position of the pointer
90
- /// device.
92
+ /// device relative to the widget's box .
91
93
final bool useGlobalPointer;
92
94
93
95
/// Decides whether this transition should transition between when the pointer
@@ -99,6 +101,17 @@ class PointerTransition extends StatefulWidget {
99
101
/// the widget.
100
102
final bool resetOnExitBounds;
101
103
104
+ /// Whether this transition should rely on
105
+ /// [WidgetsBinding.instance.pointerRouter.addGlobalRoute] to read
106
+ /// the pointer device's position, which is useful for reading cursor
107
+ /// events that may not be read properly via [MouseRegion] s like if the
108
+ /// cursor is outside the bounds of the physical window or applies some
109
+ /// unusual gesture that may not be normally detected.
110
+ ///
111
+ /// If set to false, a traditional [MouseRegion] is used to read
112
+ /// the pointer device's position.
113
+ final bool usePointerRouter;
114
+
102
115
/// The child widget to apply the effects to.
103
116
final Widget child;
104
117
@@ -119,6 +132,7 @@ class PointerTransition extends StatefulWidget {
119
132
this .useGlobalPointer = false ,
120
133
this .transitionBetweenBounds = true ,
121
134
this .resetOnExitBounds = true ,
135
+ this .usePointerRouter = true ,
122
136
this .builder,
123
137
this .curve = appleEaseInOut,
124
138
this .duration = const Duration (milliseconds: 125 ),
@@ -174,7 +188,10 @@ class _PointerTransitionState extends State<PointerTransition>
174
188
@override
175
189
void initState () {
176
190
super .initState ();
177
- WidgetsBinding .instance.pointerRouter.addGlobalRoute (updateState);
191
+ if (widget.usePointerRouter) {
192
+ WidgetsBinding .instance.pointerRouter.addGlobalRoute (updateState);
193
+ }
194
+
178
195
_controller.addListener (animationListener);
179
196
}
180
197
@@ -193,6 +210,14 @@ class _PointerTransitionState extends State<PointerTransition>
193
210
curve: widget.curve,
194
211
);
195
212
}
213
+
214
+ if (oldWidget.usePointerRouter != widget.usePointerRouter) {
215
+ if (widget.usePointerRouter) {
216
+ WidgetsBinding .instance.pointerRouter.addGlobalRoute (updateState);
217
+ } else {
218
+ WidgetsBinding .instance.pointerRouter.removeGlobalRoute (updateState);
219
+ }
220
+ }
196
221
}
197
222
198
223
void animationListener () {
@@ -210,7 +235,10 @@ class _PointerTransitionState extends State<PointerTransition>
210
235
_animation.dispose ();
211
236
_controller.removeListener (animationListener);
212
237
_controller.dispose ();
213
- WidgetsBinding .instance.pointerRouter.removeGlobalRoute (updateState);
238
+
239
+ if (widget.usePointerRouter) {
240
+ WidgetsBinding .instance.pointerRouter.removeGlobalRoute (updateState);
241
+ }
214
242
super .dispose ();
215
243
}
216
244
@@ -345,7 +373,7 @@ class _PointerTransitionState extends State<PointerTransition>
345
373
346
374
@override
347
375
Widget build (BuildContext context) {
348
- final Widget child = widget.builder? .call (
376
+ Widget child = widget.builder? .call (
349
377
context,
350
378
widget.child,
351
379
PointerTransitionEvent (
@@ -357,6 +385,14 @@ class _PointerTransitionState extends State<PointerTransition>
357
385
) ??
358
386
widget.child;
359
387
388
+ if (! widget.usePointerRouter) {
389
+ child = MouseRegion (
390
+ onHover: (event) => updateState (event),
391
+ onExit: (event) => resetValue (),
392
+ child: child,
393
+ );
394
+ }
395
+
360
396
return PostFrame (
361
397
child: AnimatedBuilder (
362
398
animation: _animation,
0 commit comments