text
stringlengths
1
372
photoProvider: item.imageProvider,
),
);
<code_end>
in this case, when the user long presses on the
MenuListItem widget, the LongPressDraggable
widget displays a DraggingListItem.
this DraggingListItem displays a photo of the
selected food item, centered beneath
the user’s finger.
the dragAnchorStrategy property is set to
pointerDragAnchorStrategy.
this property value instructs LongPressDraggable
to base the DraggableListItem’s position on the
user’s finger. as the user moves a finger,
the DraggableListItem moves with it.
dragging and dropping is of little use if no information
is transmitted when the item is dropped.
for this reason, LongPressDraggable takes a data parameter.
in this case, the type of data is item,
which holds information about the
food menu item that the user pressed on.
the data associated with a LongPressDraggable
is sent to a special widget called DragTarget,
where the user releases the drag gesture.
you’ll implement the drop behavior next.
<topic_end>
<topic_start>
drop the draggable
the user can drop a LongPressDraggable wherever they choose,
but dropping the draggable has no effect unless it’s dropped
on top of a DragTarget. when the user drops a draggable on
top of a DragTarget widget, the DragTarget widget
can either accept or reject the data from the draggable.
in this recipe, the user should drop a menu item on a
CustomerCart widget to add the menu item to the user’s cart.
<code_start>
CustomerCart(
hasItems: customer.items.isNotEmpty,
highlighted: candidateItems.isNotEmpty,
customer: customer,
);
<code_end>
wrap the CustomerCart widget with a DragTarget widget.
<code_start>
DragTarget<Item>(
builder: (context, candidateItems, rejectedItems) {
return CustomerCart(
hasItems: customer.items.isNotEmpty,
highlighted: candidateItems.isNotEmpty,
customer: customer,
);
},
onAcceptWithDetails: (details) {
_itemDroppedOnCustomerCart(
item: details.data,
customer: customer,
);
},
)
<code_end>
the DragTarget displays your existing widget and
also coordinates with LongPressDraggable to recognize
when the user drags a draggable on top of the DragTarget.
the DragTarget also recognizes when the user drops
a draggable on top of the DragTarget widget.
when the user drags a draggable on the DragTarget widget,
candidateItems contains the data items that the user is dragging.
this draggable allows you to change what your widget looks
like when the user is dragging over it. in this case,
the customer widget turns red whenever any items are dragged above the
DragTarget widget. the red visual appearance is configured with the
highlighted property within the CustomerCart widget.
when the user drops a draggable on the DragTarget widget,
the onAcceptWithDetails callback is invoked. this is when you get
to decide whether or not to accept the data that was dropped.
in this case, the item is always accepted and processed.
you might choose to inspect the incoming item to make a
different decision.
notice that the type of item dropped on DragTarget
must match the type of the item dragged from LongPressDraggable.
if the types are not compatible, then
the onAcceptWithDetails method isn’t invoked.
with a DragTarget widget configured to accept your
desired data, you can now transmit data from one part
of your UI to another by dragging and dropping.
in the next step,
you update the customer’s cart with the dropped menu item.
<topic_end>
<topic_start>
add a menu item to a cart
each customer is represented by a customer object,
which maintains a cart of items and a price total.
<code_start>
class customer {
customer({
required this.name,
required this.imageProvider,
List<Item>? items,
}) : items = items ?? [];