Skip to content

Instantly share code, notes, and snippets.

@chooyan-eng
Created October 19, 2025 14:33
Show Gist options
  • Select an option

  • Save chooyan-eng/aa2f5be46f52bdfc8c0463c4cdc1d514 to your computer and use it in GitHub Desktop.

Select an option

Save chooyan-eng/aa2f5be46f52bdfc8c0463c4cdc1d514 to your computer and use it in GitHub Desktop.
Flutter Alliance demo 2
import 'package:animated_to/animated_to.dart';
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: DraggableDemoPage()));
class DraggableDemoPage extends StatefulWidget {
const DraggableDemoPage({super.key});
@override
State<DraggableDemoPage> createState() => _DraggableDemoPageState();
}
class _Item {
_Item({required this.id, required this.color});
final String id;
final Color color;
}
class _DraggableDemoPageState extends State<DraggableDemoPage> {
final _cubes = List.generate(
40,
(index) => _Item(
id: index.toString(),
color: Colors.primaries[index % Colors.primaries.length],
),
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Wrap(
spacing: 16,
runSpacing: 16,
children: _cubes
.map(
(e) => _Cube(
item: e,
onAccept: (data) {
final targetIndex = _cubes.indexOf(e);
final draggingIndex = _cubes.indexOf(data);
setState(() {
final temp = _cubes[targetIndex];
_cubes[targetIndex] = _cubes[draggingIndex];
_cubes[draggingIndex] = temp;
});
},
),
)
.toList(),
),
),
),
);
}
}
class _Cube extends StatelessWidget {
const _Cube({required this.item, required this.onAccept});
final _Item item;
final ValueSetter<_Item> onAccept;
@override
Widget build(BuildContext context) {
return AnimatedTo.curve(
globalKey: GlobalObjectKey(item.id),
child: DragTarget(
onWillAcceptWithDetails: (details) {
if (details.data == item) {
return false;
}
onAccept(details.data as _Item);
return true;
},
builder: (context, candidateData, rejectedData) => Draggable(
feedback: _CubeFace(color: item.color, size: 60),
childWhenDragging: _CubeFace(color: Colors.transparent),
data: item,
child: _CubeFace(color: item.color),
),
),
);
}
}
class _CubeFace extends StatelessWidget {
const _CubeFace({required this.color, this.size = 60});
final Color color;
final double size;
@override
Widget build(BuildContext context) {
return Container(
width: size,
height: size,
decoration: BoxDecoration(color: color, shape: BoxShape.circle),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment