text
stringlengths
1
372
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.lightBlue,
borderRadius: BorderRadius.circular(8),
),
child: const Text('My button'),
),
);
}
}
<code_end>
<topic_end>
<topic_start>
drag outside an app
you might want to implement
drag and drop somewhere in your app.
you have a couple potential approaches
that you can take. one directly uses
flutter widgets and the other uses a package
(super_drag_and_drop), available on pub.dev.
<topic_end>
<topic_start>
create draggable widgets within your app
if you want to implement drag and drop within
your application, you can use the draggable
widget. for insight into this approach, see
the drag a UI element within an app recipe.
an advantage of using draggable and DragTarget is
that you can supply dart code to decide whether to accept a drop.
for more information, check out the
draggable widget of the week video.
<topic_end>
<topic_start>
implement drag and drop between apps
if you want to implement drag and drop within
your application and also between your
application and another (possibly non-Flutter) app,
check out the super_drag_and_drop package.
to avoid implementing two styles of drag and drop,
one for drags outside of the app and another for
dragging inside the app,
you can supply local data to the package to
perform drags within your app.
another difference between this approach and
using draggable directly,
is that you must tell the package up front
what data your app accepts because the platform
APIs need a synchronous response, which doesn’t
allow an asynchronous response from the framework.
an advantage of using this approach is that it
works across desktop, mobile, and web.
<topic_end>
<topic_start>
drag a UI element
drag and drop is a common mobile app interaction.
as the user long presses (sometimes called touch & hold)
on a widget, another widget appears beneath the
user’s finger, and the user drags the widget to a
final location and releases it.
in this recipe, you’ll build a drag-and-drop interaction
where the user long presses on a choice of food,
and then drags that food to the picture of the customer who
is paying for it.
the following animation shows the app’s behavior:
this recipe begins with a prebuilt list of menu items and
a row of customers.
the first step is to recognize a long press
and display a draggable photo of a menu item.
<topic_end>
<topic_start>
press and drag
flutter provides a widget called LongPressDraggable
that provides the exact behavior that you need to begin
a drag-and-drop interaction. a LongPressDraggable
widget recognizes when a long press occurs and then
displays a new widget near the user’s finger.
as the user drags, the widget follows the user’s finger.
LongPressDraggable gives you full control over the
widget that the user drags.
each menu list item is displayed with a custom
MenuListItem widget.
<code_start>
MenuListItem(
name: item.name,
price: item.formattedTotalItemPrice,
photoProvider: item.imageProvider,
)
<code_end>
wrap the MenuListItem widget with a LongPressDraggable widget.
<code_start>
LongPressDraggable<Item>(
data: item,
dragAnchorStrategy: pointerDragAnchorStrategy,
feedback: DraggingListItem(
dragKey: _draggableKey,
photoProvider: item.imageProvider,
),
child: MenuListItem(
name: item.name,
price: item.formattedTotalItemPrice,