Created
October 20, 2020 04:03
-
-
Save yusufpats/eb126f297d0aeca9e538d792bb1edc26 to your computer and use it in GitHub Desktop.
Flutter implementation Drag-Follower widgets using Stack widget
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'; | |
final Color darkBlue = Color.fromARGB(255, 255, 255, 255); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
backgroundColor: Colors.white, | |
body: Center( | |
child: DragFollower(), | |
), | |
), | |
); | |
} | |
} | |
class DragFollower extends StatefulWidget { | |
@override | |
_DragFollowerState createState() => _DragFollowerState(); | |
} | |
class _DragFollowerState extends State<DragFollower> { | |
final double squareSize = 72; | |
final double distanceFromSquare = 8; | |
Offset currentPosition = Offset(0,0); | |
@override | |
Widget build(BuildContext context) { | |
return SafeArea( | |
child: Scaffold( | |
body: Stack( | |
children: [ | |
Positioned( | |
top: currentPosition.dy, | |
left: currentPosition.dx, | |
child: GestureDetector( | |
onPanUpdate: (DragUpdateDetails details) { | |
currentPosition = details.globalPosition; | |
print("" + currentPosition.dx.toString() + " : " + currentPosition.dy.toString()); | |
setState((){}); | |
}, | |
child: Container( | |
width: squareSize, | |
padding: EdgeInsets.all(16), | |
color: Colors.red, | |
child: Text("Red"), | |
), | |
), | |
), | |
Positioned( | |
top: currentPosition.dy, | |
left: currentPosition.dx + squareSize + distanceFromSquare, | |
child: Container( | |
padding: EdgeInsets.all(16), | |
color: Colors.blue, | |
child: Text("Blue"), | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment