text stringlengths 1 474 |
|---|
<code_start>LongPressDraggable<Item>( |
data: item, |
dragAnchorStrategy: pointerDragAnchorStrategy, |
feedback: DraggingListItem( |
dragKey: _draggableKey, |
photoProvider: item.imageProvider, |
), |
child: MenuListItem( |
name: item.name, |
price: item.formattedTotalItemPrice, |
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 ?? []; |
final String name; |
final ImageProvider imageProvider; |
final List<Item> items; |
String get formattedTotalItemPrice { |
final totalPriceCents = |
items.fold<int>(0, (prev, item) => prev + item.totalPriceCents); |
return '\$${(totalPriceCents / 100.0).toStringAsFixed(2)}'; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.