text stringlengths 1 474 |
|---|
class MyHomePage extends StatelessWidget { |
final String title; |
const MyHomePage({super.key, required this.title}); |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
title: Text(title), |
), |
body: const Center( |
child: MyButton(), |
), |
); |
} |
} |
class MyButton extends StatelessWidget { |
const MyButton({super.key}); |
@override |
Widget build(BuildContext context) { |
// The GestureDetector wraps the button. |
return GestureDetector( |
// When the child is tapped, show a snackbar. |
onTap: () { |
const snackBar = SnackBar(content: Text('Tap')); |
ScaffoldMessenger.of(context).showSnackBar(snackBar); |
}, |
// The custom button |
child: Container( |
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. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.