diff --git a/docs/resources/ui/widgets/built-in-widgets/draggable.md b/docs/resources/ui/widgets/built-in-widgets/draggable.md index f3e4b71c..61f9e33b 100644 --- a/docs/resources/ui/widgets/built-in-widgets/draggable.md +++ b/docs/resources/ui/widgets/built-in-widgets/draggable.md @@ -1,28 +1,50 @@ --- +slug: draggable +title: Draggable and DragTarget tags: [Base Elements] +description: Learn how to build drag-and-drop interactions with the Draggable and DragTarget widgets in FlutterFlow. --- -# Draggable + DragTarget +# Draggable and DragTarget -The Draggable widget is used to make a widget that can be dragged and dropped to a different location within the app. It allows users to interact with the app by moving an item using touch gestures or a mouse. The DragTarget widget is used in conjunction with the Draggable widget to specify where a dragged item can be dropped. It creates a region that can accept the data carried by the Draggable widget. +The **Draggable** and **DragTarget** widgets let users move items from one place to another in your app. Use **Draggable** for the item that can be moved and **DragTarget** for the area where it can be dropped. For example, users can sort items, move tasks between columns, or drag products into a cart. -When an item is dragged over a DragTarget, the DragTarget has the opportunity to determine whether it can accept the item. If it accepts, it can then trigger actions such as updating the app's state to reflect the change. +![A task card moving from a Draggable widget to a highlighted DragTarget area.](imgs/draggable-dragtarget-overview.png) -For example, in a shopping cart app, you could use these widgets together to allow users to add items to their cart by dragging and dropping them onto a cart icon. +## How Draggable and DragTarget Work -## Adding Draggable and DragTarget Widgets +A drag-and-drop interaction has three parts: -Let's see how to add a drag-and-drop functionality by building an example that allows users to put only plants on the shelf. Here's how it looks: +1. The user starts dragging the child of a **Draggable** widget. +2. The Draggable carries its configured data **Value**. +3. A **DragTarget** with a matching data **Type** receives the value and runs the configured actions. + +The data type controls compatibility between the widgets. The value identifies the dragged item and can be checked before the app updates its state. + +## 1. Adding a Draggable Widget + +1. Add a **Draggable** widget from the [Widget Palette](../../../../intro/ff-ui/widget-palette.md). +2. Add the widget that users should drag, such as an **Image**, **Container**, or **Card**, as its child. + +:::tip +The child of a Draggable widget must have its **Width** and **Height** set explicitly. +::: + +### Setting the Draggable Data + +- **Type:** Defines the kind of data carried by the widget. This must match the **Type** configured on the intended DragTarget. +- **Value:** Contains the data passed to the DragTarget. The value can be static or set from a variable. + +For simple interactions, the value can be a string or number that identifies the item. Use a custom data type when the target needs multiple related values, such as a product ID, name, and price.
- -
-

+- Set a temporary state variable with **On Drag Enter** to highlight an active drop area. +- Reset the temporary state with **On Drag Exit** when the item leaves the area. +- Update persistent app or page state only after **On Drag Accept** runs. +- Use **On Drag Started** and **On Drag End** to change the appearance of the draggable item or surrounding interface during the interaction. +- Show a message when a dropped value does not meet the target's conditions. + +Keep hover state separate from the state created after an accepted drop. Resetting persistent state with **On Drag Exit** can undo a completed interaction when the pointer leaves the target. -3. Now, select the **On Drag Exit** andadd an action to [update](../../pages/page-lifecycle.md#page-state) the `isShelfFull` variable to False. This ensures that if the user decides not to drop the item and moves it away, the shelf image reverts to the empty one. +## Best Practices - ![img_2.png](imgs/img_2.png) \ No newline at end of file +- Use the simplest data type that contains the information needed after the drop. Use a custom data type when several related values must move together. +- Do not rely on color alone to identify an active or valid drop target. Pair color with a border, icon, label, or another visual cue. +- For important tasks, provide a tap or button-based alternative to drag and drop. diff --git a/docs/resources/ui/widgets/built-in-widgets/imgs/draggable-data-flow.svg b/docs/resources/ui/widgets/built-in-widgets/imgs/draggable-data-flow.svg new file mode 100644 index 00000000..bc0bcc12 --- /dev/null +++ b/docs/resources/ui/widgets/built-in-widgets/imgs/draggable-data-flow.svg @@ -0,0 +1,78 @@ + + Draggable data type compatibility + Three Draggable widgets send data to a String DragTarget. String plant and String spoon are accepted because their types match, while Integer 10 is rejected because its type does not match. + + + + + + + Draggable widgets + + + + + Aa + Type: + String + Value: + plant + + + + + Aa + Type: + String + Value: + spoon + + + + + 123 + Type: + Integer + Value: + 10 + + + Type matches + + + + Type matches + + + + Type mismatch + + + + + + + DragTarget + Type: String + + + + String / plant + Type matches the target + + Accepted + + + + String / spoon + Type matches the target + + Accepted + + + + Integer / 10 + Type does not match + + Rejected + diff --git a/docs/resources/ui/widgets/built-in-widgets/imgs/draggable-dragtarget-overview.png b/docs/resources/ui/widgets/built-in-widgets/imgs/draggable-dragtarget-overview.png new file mode 100644 index 00000000..6894ba3e Binary files /dev/null and b/docs/resources/ui/widgets/built-in-widgets/imgs/draggable-dragtarget-overview.png differ