Created
April 19, 2021 13:18
-
-
Save followthemoney1/c8d9576c21b0464639bc017bed0f5364 to your computer and use it in GitHub Desktop.
temp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/physics.dart'; | |
main() { | |
runApp( | |
MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: PhysicsCardDragDemo(), | |
), | |
); | |
} | |
class PhysicsCardDragDemo extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() { | |
return PhysicsCardDragDemoS(); | |
} | |
} | |
class PhysicsCardDragDemoS extends State<PhysicsCardDragDemo> { | |
bool selected = false; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Test diachenko'), | |
), | |
body: Stack(children: [ | |
Positioned( | |
width: 400, | |
height: 200, | |
left: 150.0, | |
child: Card( | |
child: Center(child:Text("Loading data..."),) | |
), | |
), | |
AnimatedPositioned( | |
width: 400, | |
height: 200, | |
left: selected ? -450.0 : 150.0, | |
duration: selected ? const Duration(seconds: 2) : const Duration(milliseconds: 1), | |
curve: Curves.fastOutSlowIn, | |
onEnd:(){ | |
setState((){ | |
selected = false; | |
}); | |
}, | |
child: SwipeGestureRecognizer( | |
child: Card( | |
child: FlutterLogo(), | |
), | |
onSwipeLeft: () { | |
setState(() { | |
selected = true; | |
}); | |
}), | |
), | |
]), | |
); | |
} | |
} | |
class SwipeGestureRecognizer extends StatefulWidget { | |
final Function() onSwipeLeft; | |
final Function() onSwipeRight; | |
final Function() onSwipeUp; | |
final Function() onSwipeDown; | |
final Widget child; | |
SwipeGestureRecognizer({ | |
Key key, | |
this.child, | |
this.onSwipeDown, | |
this.onSwipeLeft, | |
this.onSwipeRight, | |
this.onSwipeUp, | |
}) : super(key: key); | |
@override | |
_SwipeGestureRecognizerState createState() => _SwipeGestureRecognizerState(); | |
} | |
class _SwipeGestureRecognizerState extends State<SwipeGestureRecognizer> { | |
Offset _horizontalSwipeStartingOffset; | |
Offset _verticalSwipeStartingOffset; | |
bool _isSwipeLeft; | |
bool _isSwipeRight; | |
bool _isSwipeUp; | |
bool _isSwipeDown; | |
@override | |
void initState() { | |
super.initState(); | |
_horizontalSwipeStartingOffset = | |
_horizontalSwipeStartingOffset = Offset(0, 0); | |
_isSwipeDown = _isSwipeUp = _isSwipeRight = _isSwipeLeft = false; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return (widget.onSwipeLeft != null || widget.onSwipeRight != null) && | |
(widget.onSwipeDown != null || widget.onSwipeUp != null) | |
? GestureDetector( | |
child: widget.child, | |
onHorizontalDragStart: (details) { | |
_horizontalSwipeStartingOffset = details.localPosition; | |
}, | |
onHorizontalDragUpdate: (details) { | |
if (_horizontalSwipeStartingOffset.dx > | |
details.localPosition.dx) { | |
_isSwipeLeft = true; | |
_isSwipeRight = false; | |
} else { | |
_isSwipeRight = true; | |
_isSwipeLeft = false; | |
} | |
}, | |
onHorizontalDragEnd: (details) { | |
if (_isSwipeLeft) { | |
if (widget.onSwipeLeft != null) { | |
widget.onSwipeLeft(); | |
} | |
} else if (_isSwipeRight) { | |
if (widget.onSwipeRight != null) { | |
widget.onSwipeRight(); | |
} | |
} | |
}, | |
onVerticalDragStart: (details) { | |
_verticalSwipeStartingOffset = details.localPosition; | |
}, | |
onVerticalDragUpdate: (details) { | |
if (_verticalSwipeStartingOffset.dy > details.localPosition.dy) { | |
_isSwipeUp = true; | |
_isSwipeDown = false; | |
} else { | |
_isSwipeDown = true; | |
_isSwipeUp = false; | |
} | |
}, | |
onVerticalDragEnd: (details) { | |
if (_isSwipeUp && widget.onSwipeUp != null) { | |
widget.onSwipeUp(); | |
} else if (_isSwipeDown && widget.onSwipeDown != null) { | |
widget.onSwipeDown(); | |
} | |
}, | |
) | |
: (widget.onSwipeLeft != null || widget.onSwipeRight != null) | |
? GestureDetector( | |
child: widget.child, | |
onHorizontalDragStart: (details) { | |
_horizontalSwipeStartingOffset = details.localPosition; | |
}, | |
onHorizontalDragUpdate: (details) { | |
if (_horizontalSwipeStartingOffset.dx > | |
details.localPosition.dx) { | |
_isSwipeLeft = true; | |
_isSwipeRight = false; | |
} else { | |
_isSwipeRight = true; | |
_isSwipeLeft = false; | |
} | |
}, | |
onHorizontalDragEnd: (details) { | |
if (_isSwipeLeft && widget.onSwipeLeft != null) { | |
widget.onSwipeLeft(); | |
} else if (_isSwipeRight && widget.onSwipeRight != null) { | |
widget.onSwipeRight(); | |
} | |
}, | |
) | |
: (widget.onSwipeDown != null || widget.onSwipeUp != null) | |
? GestureDetector( | |
child: widget.child, | |
onVerticalDragStart: (details) { | |
_verticalSwipeStartingOffset = details.localPosition; | |
}, | |
onVerticalDragUpdate: (details) { | |
if (_verticalSwipeStartingOffset.dy > | |
details.localPosition.dy) { | |
_isSwipeUp = true; | |
_isSwipeDown = false; | |
} else { | |
_isSwipeDown = true; | |
_isSwipeUp = false; | |
} | |
}, | |
onVerticalDragEnd: (details) { | |
if (_isSwipeUp && widget.onSwipeUp != null) { | |
widget.onSwipeUp(); | |
} else if (_isSwipeDown && widget.onSwipeDown != null) { | |
widget.onSwipeDown(); | |
} | |
}, | |
) | |
: SizedBox.shrink(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment