Move container to a different location on touch in flutter

This Article is posted by seven.srikanth at 1/2/2023 5:21:27 AM



To move a container to a different location on touch, you can use the Positioned widget in combination with the GestureDetector widget.

Here is the sample output of this app.

![](https://fluttercentral.com/Uploads/174083be-3fee-4e8c-833e-43a8df6d8f66.gifwidth="50%height="auto](

Here's the code of how you can do this:

color: #dcdcaa;](runApp(color: #569cd6;](const color: #4ec9b0;](MyApp());

color: #569cd6;](class color: #4ec9b0;](MyApp color: #569cd6;](extends color: #4ec9b0;](StatelessWidget ); color: #9cdcfe;](@ color: #9cdcfe;](override

color: #4ec9b0;](Widget color: #dcdcaa;](build(color: #4ec9b0;](BuildContext color: #9cdcfe;](context) { color: #c586c0;](return color: #4ec9b0;](MaterialApp( color: #9cdcfe;](home: color: #4ec9b0;](Scaffold( color: #9cdcfe;](appBar: color: #4ec9b0;](AppBar( color: #9cdcfe;](title: color: #569cd6;](const color: #4ec9b0;](Text(color: #ce9178;]('Move Container on Touch App'), ), color: #9cdcfe;](body: color: #569cd6;](const color: #4ec9b0;](TouchMoveExample(), ), ); } }

color: #569cd6;](class color: #4ec9b0;](TouchMoveExample color: #569cd6;](extends color: #4ec9b0;](StatefulWidget ); color: #9cdcfe;](@ color: #9cdcfe;](override

color: #4ec9b0;](State color: #dcdcaa;](createState() => color: #4ec9b0;](_TouchMoveExampleState(); }

color: #569cd6;](class color: #4ec9b0;](_TouchMoveExampleState color: #569cd6;](extends color: #4ec9b0;](State { color: #4ec9b0;](double color: #9cdcfe;](_xPosition = color: #b5cea8;](0; color: #4ec9b0;](double color: #9cdcfe;](_yPosition = color: #b5cea8;](0; color: #9cdcfe;](@ color: #9cdcfe;](override

color: #4ec9b0;](Widget color: #dcdcaa;](build(color: #4ec9b0;](BuildContext color: #9cdcfe;](context) { color: #c586c0;](return color: #4ec9b0;](Stack( color: #9cdcfe;](children: [ color: #4ec9b0;](Positioned( color: #9cdcfe;](left: color: #9cdcfe;](_xPosition, color: #9cdcfe;](top: color: #9cdcfe;](_yPosition, color: #9cdcfe;](child: color: #4ec9b0;](GestureDetector( color: #9cdcfe;](onPanUpdate: (color: #9cdcfe;](details) { color: #dcdcaa;](setState(() { color: #9cdcfe;](_xPosition += color: #9cdcfe;](details.color: #9cdcfe;](delta.color: #9cdcfe;](dx; color: #9cdcfe;](_yPosition += color: #9cdcfe;](details.color: #9cdcfe;](delta.color: #9cdcfe;](dy; }); }, color: #9cdcfe;](child: color: #4ec9b0;](Container( color: #9cdcfe;](width: color: #b5cea8;](100, color: #9cdcfe;](height: color: #b5cea8;](100, color: #9cdcfe;](color: color: #4ec9b0;](Colors.color: #9cdcfe;](red, ), ), ), ], ); } } ` Thanks,Srikanth


Tags: drag and drop; move on touch;








0 Comments
Login to comment.
Recent Comments