Skip to content

Instantly share code, notes, and snippets.

@karabanovbs
Last active April 20, 2021 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karabanovbs/1884963b3e7a67249c2683607304f64d to your computer and use it in GitHub Desktop.
Save karabanovbs/1884963b3e7a67249c2683607304f64d to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() async {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Container(
color: Color(0xFF15202D),
child: SizedBox.expand(
child: ListVisited(),
),
),
),
),
);
}
class ListVisited extends StatefulWidget {
ListVisited() : super();
@override
_ListVisitedState createState() => _ListVisitedState();
}
class _ListVisitedState extends State<ListVisited> {
final List<int> list = Iterable.generate(100, (i) => i).toList();
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
for (var index in list)
DragTarget<int>(
onWillAccept: (data) {
return true;
},
onAccept: (data) {
setState(() {
var newPos = list.indexOf(index);
var dragIndex = list.indexOf(data);
var tmp = index;
list[newPos] = data;
list[dragIndex] = tmp;
});
},
builder: (context, List<int?> candidate, rejected) {
return Draggable<int>(
data: index,
child: Container(
height: 200,
width: 300,
child: Center(child: Text(index.toString())),
color: Colors.blue,
),
childWhenDragging: Container(
height: 200,
width: 300,
color: Colors.red,
),
feedback: Container(
height: 200,
width: 300,
color: Colors.green,
),
);
},
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment