2023-04-04 flutter侧滑返回手势

2023-04-05  本文已影响0人  我是小胡胡分胡

页面是一个StatefulWidget,类型:

_CupertinoBackGestureDetector
_CupertinoBackGestureDetectorState

late HorizontalDragGestureRecognizer _recognizer;
_recognizer.addPointer(event);

_CupertinoBackGestureController<T>? _backGestureController;
_backGestureController = widget.onStartPopGesture();

class _CupertinoBackGestureDetector<T> extends StatefulWidget {
 const _CupertinoBackGestureDetector({
   Key? key,
   required this.enabledCallback,
   required this.onStartPopGesture,
   required this.child,
 }) : assert(enabledCallback != null),
      assert(onStartPopGesture != null),
      assert(child != null),
      super(key: key);

 final Widget child;

 final ValueGetter<bool> enabledCallback;

 final ValueGetter<_CupertinoBackGestureController<T>> onStartPopGesture;

 @override
 _CupertinoBackGestureDetectorState<T> createState() => _CupertinoBackGestureDetectorState<T>();
}

class _CupertinoBackGestureDetectorState<T> extends State<_CupertinoBackGestureDetector<T>> {
 _CupertinoBackGestureController<T>? _backGestureController;

 late HorizontalDragGestureRecognizer _recognizer;

 @override
 void initState() {
   super.initState();
   _recognizer = HorizontalDragGestureRecognizer(debugOwner: this)
     ..onStart = _handleDragStart
     ..onUpdate = _handleDragUpdate
     ..onEnd = _handleDragEnd
     ..onCancel = _handleDragCancel;
 }

 @override
 void dispose() {
   _recognizer.dispose();
   super.dispose();
 }

 void _handleDragStart(DragStartDetails details) {
   assert(mounted);
   assert(_backGestureController == null);
   _backGestureController = widget.onStartPopGesture();
 }

 void _handleDragUpdate(DragUpdateDetails details) {
   assert(mounted);
   assert(_backGestureController != null);
   _backGestureController!.dragUpdate(_convertToLogical(details.primaryDelta! / context.size!.width));
 }

 void _handleDragEnd(DragEndDetails details) {
   assert(mounted);
   assert(_backGestureController != null);
   _backGestureController!.dragEnd(_convertToLogical(details.velocity.pixelsPerSecond.dx / context.size!.width));
   _backGestureController = null;
 }

 void _handleDragCancel() {
   assert(mounted);
   // This can be called even if start is not called, paired with the "down" event
   // that we don't consider here.
   _backGestureController?.dragEnd(0.0);
   _backGestureController = null;
 }

 void _handlePointerDown(PointerDownEvent event) {
   if (widget.enabledCallback())
     _recognizer.addPointer(event);
 }

 double _convertToLogical(double value) {
   switch (Directionality.of(context)) {
     case TextDirection.rtl:
       return -value;
     case TextDirection.ltr:
       return value;
   }
 }

 @override
 Widget build(BuildContext context) {
   assert(debugCheckHasDirectionality(context));
   // For devices with notches, the drag area needs to be larger on the side
   // that has the notch.
   double dragAreaWidth = Directionality.of(context) == TextDirection.ltr ?
                          MediaQuery.of(context).padding.left :
                          MediaQuery.of(context).padding.right;
   dragAreaWidth = max(dragAreaWidth, _kBackGestureWidth);
   return Stack(
     fit: StackFit.passthrough,
     children: <Widget>[
       widget.child,
       PositionedDirectional(
         start: 0.0,
         width: dragAreaWidth,
         top: 0.0,
         bottom: 0.0,
         child: Listener(
           onPointerDown: _handlePointerDown,
           behavior: HitTestBehavior.translucent,
         ),
       ),
     ],
   );
 }
}
上一篇下一篇

猜你喜欢

热点阅读