You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -12,6 +12,16 @@ define specific behaviors that the child entity should execute when certain even
12
12
It enables dynamic and interactive behavior in child components, allowing them to perform actions
13
13
defined by the parent, such as navigation, data updates, or displaying dialogs.
14
14
15
+
For example, if you have an *image upload component*, the parent can define what should happen after an image is successfully uploaded. Using callbacks, the *image upload component* can execute a parent-defined action, such as:
16
+
17
+
- Resize and compress the image to reduce storage size.
18
+
- Update the user's database record with the new image URL.
19
+
- Refresh the UI to display the updated profile picture.
20
+
21
+
This makes the *image upload component* component reusable, as it doesn't need to know the specifics of what should happen post-upload. Instead, the parent controls the behavior by passing the appropriate actions via a callback.
:::tip[Benefits of Using Callbacks in FlutterFlow]
16
26
17
27
-**Modularity:** Separate the logic of what happens when an event occurs from the child component,
@@ -21,35 +31,144 @@ defined by the parent, such as navigation, data updates, or displaying dialogs.
21
31
:::
22
32
23
33
24
-
## Creating a Callback Parameter
25
-
In order to create a component that will execute a callback, you must create a component with a parameter of type Action.
34
+
## Adding Callbacks
35
+
36
+
Let’s continue with our previous example (image upload component) and see how to add callbacks on it:
37
+
38
+
### Creating a Callback Parameter
39
+
40
+
In order to create a component that will execute a callback, you must create a component with a parameter of **Type****Action**. You can create an action parameter called `uploadAction`, which represents the action that will be executed after the image is uploaded.
41
+
42
+
When you create an action parameter, you can also specify parameters that will be passed into the action. For this example, the action that will be executed will likely need to know the uploaded image URL to process it further, such as resizing and compressing the image, updating it in the database, and refreshing the profile picture. So, you can specify an action parameter called `uploadedURL`.
43
+
44
+
Now, the page or component that uses this button can utilize this parameter in its own action flow. An example of this is shown below.
45
+
46
+
<div style={{
47
+
position: 'relative',
48
+
paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding
When you create an action parameter, you can also specify parameters that will get passed into the action. For example,
28
-
you might have a button that allows your app to add an item to a user's cart.
107
+
When you add a component to the widget tree of a page or another component, you can define values for its parameters, including action parameters.
29
108
30
-
You can create a parameter called `onAddToBag` which represents the action that will be executed when the button is tapped.
109
+
For instance, when you add an *image upload component*, you can specify the action flows to be executed when the callback is triggered. For this example, we simply update the profile picture.
31
110
32
-
The action that will be executed will likely need to know which item is being added to the cart. So, you can also specify an action parameter -
33
-
`item`. Now, the page or component that leverages this button can use this parameter in its own action flow. You can see an example of that below.
Now that we have an image upload component with action parameters, it can be reused across different pages or contexts, as it relies on the parent to define the post-upload logic. For example, the same component can be used to upload an image while posting reviews for a product, eliminating the need to create a separate component for this functionality.
Let’s take another example of a reusable dialog component that uses callbacks to handle context-specific actions like confirming a deletion, logging out, or saving data. In one context, "Yes" deletes an item. In another, it logs out a user.
44
159
45
-
## Passing in an Action to a Component
160
+
Here the dialog component can execute different actions based on the context in which it's used. The specific logic for each action is defined by the parent component or page using the dialog. The dialog itself does not need to know the specifics of what should happen—it simply executes the callback passed to it.
46
161
47
-
When you add a component to the widget tree of a page or another component, you can set values for its parameters.
162
+
[image]
48
163
49
-
This holds true for action parameters as well. For example, when you create an instance of the button component mentioned above, you can specify the action flows that should be used when the callback is executed.
0 commit comments