2021-07-08 10:57:20 +02:00
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
|
2021-09-15 08:58:06 +02:00
|
|
|
import 'package:nc_photos/widget/draggable.dart' as my;
|
2021-07-08 10:57:20 +02:00
|
|
|
import 'package:nc_photos/widget/measurable_item_list.dart';
|
|
|
|
|
|
|
|
abstract class DraggableItem {
|
|
|
|
Widget buildWidget(BuildContext context);
|
|
|
|
|
2021-07-09 12:06:28 +02:00
|
|
|
/// The widget to show under the pointer when a drag is under way.
|
|
|
|
///
|
|
|
|
/// Return null if you wish to just use the same widget as display
|
2021-07-23 22:05:57 +02:00
|
|
|
Widget? buildDragFeedbackWidget(BuildContext context) => null;
|
2021-07-09 12:06:28 +02:00
|
|
|
|
2021-07-08 10:57:20 +02:00
|
|
|
bool get isDraggable => false;
|
2021-07-23 22:05:57 +02:00
|
|
|
DragTargetAccept<DraggableItem>? get onDropBefore => null;
|
|
|
|
DragTargetAccept<DraggableItem>? get onDropAfter => null;
|
|
|
|
VoidCallback? get onDragStarted => null;
|
|
|
|
VoidCallback? get onDragEndedAny => null;
|
2021-07-08 10:57:20 +02:00
|
|
|
StaggeredTile get staggeredTile => const StaggeredTile.count(1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
mixin DraggableItemListMixin<T extends StatefulWidget> on State<T> {
|
|
|
|
@protected
|
|
|
|
Widget buildDraggableItemList({
|
2021-07-23 22:05:57 +02:00
|
|
|
required double maxCrossAxisExtent,
|
|
|
|
ValueChanged<double?>? onMaxExtentChanged,
|
2021-07-08 10:57:20 +02:00
|
|
|
}) {
|
|
|
|
_maxCrossAxisExtent = maxCrossAxisExtent;
|
|
|
|
return MeasurableItemList(
|
|
|
|
maxCrossAxisExtent: maxCrossAxisExtent,
|
|
|
|
itemCount: _items.length,
|
|
|
|
itemBuilder: _buildItem,
|
|
|
|
staggeredTileBuilder: (index) => _items[index].staggeredTile,
|
|
|
|
onMaxExtentChanged: onMaxExtentChanged,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@protected
|
|
|
|
set draggableItemList(List<DraggableItem> newItems) {
|
|
|
|
_items = newItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildItem(BuildContext context, int index) {
|
|
|
|
final item = _items[index];
|
2021-09-15 08:58:06 +02:00
|
|
|
return my.Draggable(
|
2021-07-08 10:57:20 +02:00
|
|
|
data: item,
|
|
|
|
child: item.buildWidget(context),
|
2021-07-09 12:06:28 +02:00
|
|
|
feedback: item.buildDragFeedbackWidget(context),
|
2021-07-08 10:57:20 +02:00
|
|
|
onDropBefore: item.onDropBefore,
|
|
|
|
onDropAfter: item.onDropAfter,
|
|
|
|
onDragStarted: item.onDragStarted,
|
|
|
|
onDragEndedAny: item.onDragEndedAny,
|
2021-07-23 22:05:57 +02:00
|
|
|
feedbackSize: Size(_maxCrossAxisExtent * .65, _maxCrossAxisExtent * .65),
|
2021-07-08 10:57:20 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var _items = <DraggableItem>[];
|
2021-07-23 22:05:57 +02:00
|
|
|
late double _maxCrossAxisExtent;
|
2021-07-08 10:57:20 +02:00
|
|
|
}
|