Skip to content

Instantly share code, notes, and snippets.

@richardshiue
Last active September 15, 2023 08:54
Show Gist options
  • Save richardshiue/d402ab47c0494afa536548ce10aeb763 to your computer and use it in GitHub Desktop.
Save richardshiue/d402ab47c0494afa536548ce10aeb763 to your computer and use it in GitHub Desktop.
bustling-glacier-6043

bustling-glacier-6043

Created with <3 with dartpad.dev.

import 'dart:ui';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const MyStatefulWidget(),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
final List<int> _items = List<int>.generate(50, (int index) => index);
bool _dragStarted = false;
bool _grabStarted = false;
@override
Widget build(BuildContext context) {
final ColorScheme colorScheme = Theme.of(context).colorScheme;
final Color oddItemColor = colorScheme.primary.withOpacity(0.05);
final Color evenItemColor = colorScheme.primary.withOpacity(0.15);
return ReorderableListView(
proxyDecorator: (child, index, animation) => MouseRegion(
cursor: SystemMouseCursors.grabbing,
child: AnimatedBuilder(
animation: animation,
builder: (BuildContext context, Widget? child) {
final double animValue =
Curves.easeInOut.transform(animation.value);
final double elevation = lerpDouble(0, 6, animValue)!;
return Material(
color: Colors.transparent,
elevation: elevation,
child: child,
);
},
),
),
buildDefaultDragHandles: false,
padding: const EdgeInsets.symmetric(horizontal: 40),
children: [
for (int index = 0; index < _items.length; index += 1)
ColoredBox(
color: _items[index].isOdd ? oddItemColor : evenItemColor,
key: Key('$index'),
child: Row(
children: <Widget>[
Container(
width: 64,
height: 64,
padding: const EdgeInsets.all(8),
child: ReorderableDragStartListener(
index: index,
child: MouseRegion(
cursor: _grabStarted
? SystemMouseCursors.grabbing
: SystemMouseCursors.grab,
child: Card(
color: Theme.of(context).colorScheme.primary,
elevation: 2,
),
),
),
),
Text('Item ${_items[index]}'),
],
),
),
],
onReorder: (int oldIndex, int newIndex) {
setState(() {
if (oldIndex < newIndex) {
newIndex -= 1;
}
final int item = _items.removeAt(oldIndex);
_items.insert(newIndex, item);
});
},
onReorderStart: (index) {},
onReorderEnd: (_) => setState(() => _grabStarted = false),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment