Want to implement VerticalMultiDragGestureRecognizer bottom to up gesture in flutter? I have created a sample code below that should guide you about how to detect two finger swipe.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Swipe Demo', debugShowCheckedModeBanner: false, theme: ThemeData( primarySwatch: Colors.blue, ), home: SwipeDemo(), ); } } class SwipeDemo extends StatefulWidget { @override SwipeDemoState createState() => SwipeDemoState(); } class SwipeDemoState extends State<SwipeDemo> { Offset offset = Offset.zero; @override Widget build(BuildContext context) { return SafeArea( child: TwoFingerPointerWidget( onUpdate: (details) { setState(() { offset += details.delta; }); }, child: Container( alignment: Alignment.center, color: Colors.white, child: Transform.translate( offset: offset, child: Container( width: 100, height: 100, color: Colors.red, ), ), ), ), ); } } class TwoFingerPointerWidget extends StatelessWidget { final Widget child; final OnUpdate onUpdate; TwoFingerPointerWidget({this.child, this.onUpdate}); @override Widget build(BuildContext context) { return RawGestureDetector( gestures: <Type, GestureRecognizerFactory>{ CustomVerticalMultiDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<CustomVerticalMultiDragGestureRecognizer>( () => CustomVerticalMultiDragGestureRecognizer(), (CustomVerticalMultiDragGestureRecognizer instance) { instance.onStart = (Offset position) { return CustomDrag(events: instance.events, onUpdate: onUpdate); }; }, ), }, child: child, ); } } typedef OnUpdate(DragUpdateDetails details); class CustomDrag extends Drag { final List<PointerDownEvent> events; final OnUpdate onUpdate; CustomDrag({this.events, this.onUpdate}); @override void update(DragUpdateDetails details) { super.update(details); final delta = details.delta; if (delta.dy.abs() > 0 && events.length == 2) { onUpdate?.call(DragUpdateDetails( sourceTimeStamp: details.sourceTimeStamp, delta: Offset(0, delta.dy), primaryDelta: details.primaryDelta, globalPosition: details.globalPosition, localPosition: details.localPosition, )); } } @override void end(DragEndDetails details) { super.end(details); } } class CustomVerticalMultiDragGestureRecognizer extends MultiDragGestureRecognizer<_CustomVerticalPointerState> { final List<PointerDownEvent> events = []; @override createNewPointerState(PointerDownEvent event) { events.add(event); return _CustomVerticalPointerState(event.position, onDisposeState: () { events.remove(event); }); } @override String get debugDescription => 'custom vertical multidrag'; } typedef OnDisposeState(); class _CustomVerticalPointerState extends MultiDragPointerState { final OnDisposeState onDisposeState; _CustomVerticalPointerState(Offset initialPosition, {this.onDisposeState}) : super(initialPosition); @override void checkForResolutionAfterMove() { if (pendingDelta.dy.abs() > kTouchSlop) { resolve(GestureDisposition.accepted); } } @override void accepted(GestureMultiDragStartCallback starter) { starter(initialPosition); } @override void dispose() { onDisposeState?.call(); super.dispose(); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.