Skip to content

Commit e9e8d51

Browse files
authored
Merge pull request #216 from FlutterFlow/pinkesh/update-action-parameters-callbacks
Action Parameters - Callbacks
2 parents 7225523 + 5d3ccae commit e9e8d51

File tree

5 files changed

+134
-16
lines changed

5 files changed

+134
-16
lines changed
Lines changed: 134 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
---
22
title: Action Parameters (Callbacks)
33
sidebar_position: 4
4+
tags: [Components, Actions]
5+
keywords: [Components, Actions, Action Parameter, Callbacks]
6+
description: Learn how to add action parameters or callbacks to custom components.
47
---
58

69
# Action Parameters (Callbacks)
@@ -12,6 +15,16 @@ define specific behaviors that the child entity should execute when certain even
1215
It enables dynamic and interactive behavior in child components, allowing them to perform actions
1316
defined by the parent, such as navigation, data updates, or displaying dialogs.
1417

18+
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:
19+
20+
- Resize and compress the image to reduce storage size.
21+
- Update the user's database record with the new image URL.
22+
- Refresh the UI to display the updated profile picture.
23+
24+
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.
25+
26+
![action-parameters-callbacks](imgs/action-parameters-callbacks.avif)
27+
1528
:::tip[Benefits of Using Callbacks in FlutterFlow]
1629

1730
- **Modularity:** Separate the logic of what happens when an event occurs from the child component,
@@ -21,35 +34,140 @@ defined by the parent, such as navigation, data updates, or displaying dialogs.
2134
:::
2235

2336

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.
37+
## Adding Callbacks
38+
39+
Let’s continue with our previous example (*image upload component*) and see how to add callbacks on it:
40+
41+
### Creating a Callback Parameter
42+
43+
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.
44+
45+
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. So, you can specify an action parameter called uploadedURL.
46+
47+
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.
48+
49+
<div style={{
50+
position: 'relative',
51+
paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding
52+
height: 0,
53+
width: '100%'}}>
54+
<iframe
55+
src="https://demo.arcade.software/BRHcXG6zWBYLCxaa9ifG?embed&show_copy_link=true"
56+
title=""
57+
style={{
58+
position: 'absolute',
59+
top: 0,
60+
left: 0,
61+
width: '100%',
62+
height: '100%',
63+
colorScheme: 'light'
64+
}}
65+
frameborder="0"
66+
loading="lazy"
67+
webkitAllowFullScreen
68+
mozAllowFullScreen
69+
allowFullScreen
70+
allow="clipboard-write">
71+
</iframe>
72+
</div>
73+
<p></p>
74+
75+
### Executing a Callback
76+
77+
You can execute the action passed into the component by using the **Execute Callback** action within the component's action flows.
78+
79+
For example, you can execute the above callback after the image is successfully uploaded and the pass the uploaded image URL into the callback.
80+
2681

27-
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.
82+
<div style={{
83+
position: 'relative',
84+
paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding
85+
height: 0,
86+
width: '100%'}}>
87+
<iframe
88+
src="https://demo.arcade.software/XEap1vCGTJ4x9Y1lswe3?embed&show_copy_link=true"
89+
title=""
90+
style={{
91+
position: 'absolute',
92+
top: 0,
93+
left: 0,
94+
width: '100%',
95+
height: '100%',
96+
colorScheme: 'light'
97+
}}
98+
frameborder="0"
99+
loading="lazy"
100+
webkitAllowFullScreen
101+
mozAllowFullScreen
102+
allowFullScreen
103+
allow="clipboard-write">
104+
</iframe>
105+
</div>
106+
<p></p>
29107

30-
You can create a parameter called `onAddToBag` which represents the action that will be executed when the button is tapped.
108+
### Passing in an Action to a Component
31109

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.
110+
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.
34111

35-
![action-parameters.png](imgs/action-parameters.png)
112+
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.
36113

37-
## Executing a Callback
114+
:::info
38115

39-
You can execute the action that is passed into the component by using the **Execute Callback** action in the component's action flows.
116+
You can access the value passed to the callback by navigating to the **Set Variable** menu > **Callback Parameters**.
40117

41-
For example, you may want to execute the above callback when the button is tapped.
118+
:::
42119

43-
![execute-callback.png](imgs/execute-callback.png)
44120

45-
## Passing in an Action to a Component
121+
<div style={{
122+
position: 'relative',
123+
paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding
124+
height: 0,
125+
width: '100%'}}>
126+
<iframe
127+
src="https://demo.arcade.software/61EV732tfLpqQAIzLP8G?embed&show_copy_link=true"
128+
title=""
129+
style={{
130+
position: 'absolute',
131+
top: 0,
132+
left: 0,
133+
width: '100%',
134+
height: '100%',
135+
colorScheme: 'light'
136+
}}
137+
frameborder="0"
138+
loading="lazy"
139+
webkitAllowFullScreen
140+
mozAllowFullScreen
141+
allowFullScreen
142+
allow="clipboard-write">
143+
</iframe>
144+
</div>
145+
<p></p>
146+
147+
148+
Now that we have an *image upload component* with action parameters set up, 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.
149+
150+
![component-action-parameters.avif](imgs/component-action-parameters.avif)
151+
152+
153+
## More Examples
154+
155+
Let's see some more examples of adding action parameters (callbacks) to deepen the understanding and use it in real-world scenarios.
156+
157+
### Example 1: Dynamic Dialog Component
158+
159+
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.
46160

47-
When you add a component to the widget tree of a page or another component, you can set values for its parameters.
161+
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 of what should happen—it simply executes the callback passed to it when users click on the "Yes" button.
48162

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.
163+
![dialog-component-action-parameters.avif](imgs/dialog-component-action-parameters.avif)
50164

51-
![execute-callback.png](imgs/set-action-callback.png)
165+
### Example 2: Custom Navigation Bar in Super App
52166

167+
Using Action Parameters to build a Custom Navigation Bar in a Super App is an excellent way to create a dynamic, reusable, and modular navigation solution. A **Super App** typically hosts multiple mini-apps or features, each requiring specific navigation logic. Action Parameters allows you to define navigation behavior dynamically, depending on the active context, making it perfect for this scenario.
53168

169+
Here, the navigation bar doesn’t require hardcoded routes. Instead, the navigation logic can be customized for each mini-app, allowing the navigation bar to remain focused solely on its UI role.
54170

171+
For example, in an **ecommerce mini-app**, the home button navigates to the product listing page, while the main (middle) button opens the shopping cart. In contrast, in a **cab booking mini-app**, the home button navigates to the dashboard, and the main (middle) button opens the quick booking page.
55172

173+
![navigation-bar-action-parameters.avif](imgs/navigation-bar-action-parameters.avif)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)