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
Copy file name to clipboardExpand all lines: docs/ff-concepts/adding-customization/custom-code.md
+55-33Lines changed: 55 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,8 +17,9 @@ This is where writing custom code comes into play.
17
17
There are a few different ways to make custom code accessible in FlutterFLow:
18
18
19
19
***[Custom Functions](custom-functions.md):** Custom Dart functions that can be used to set Widget or Action properties.
20
-
***[Custom Actions](custom-actions.md):** Custom Dart functions that can be triggered by [Action Triggers](https://docs.flutterflow.io/resources/functions/action-triggers/) or used as nodes in an [Action Flow](https://docs.flutterflow.io/resources/functions/action-flow-editor#action-flow-editor). These are usually `async` functions and are able to import custom package dependencies.
21
-
***[Custom Widgets](custom-widgets.md):** Custom Flutter widgets, which can be used in the same way [Components](https://docs.flutterflow.io/resources/ui/components) are used throughout your project.
20
+
***[Custom Actions](custom-actions.md):** Custom Dart functions that can be triggered by [Action Triggers](https://docs.flutterflow.io/resources/functions/action-triggers/) or used as nodes in an [Action Flow](https://docs.flutterflow.io/resources/functions/action-flow-editor#action-flow-editor). These are usually `async` functions and are able to import [custom package dependencies](#adding-a-pubspec-dependency).
21
+
***[Custom Widgets](custom-widgets.md):** Custom Flutter widgets that can also import [custom package dependencies](#adding-a-pubspec-dependency) and be used in the same way as [Components](https://docs.flutterflow.io/resources/ui/components) throughout your project.
22
+
***[Custom Files](custom-files.md):** Within Custom Files, you'll have the ability to edit some parts of the `main.dart` file.
22
23
23
24
24
25
:::tip[Why Write Custom Code?]
@@ -48,12 +49,6 @@ Note that the desktop version of the In-App Code Editor is limited. We recommend
48
49
or the **[VSCode Extension](vscode-extension.md)**.
49
50
:::
50
51
51
-
52
-
Beyond the custom code resoureces outlined above, you'll also see an additional section in the left
53
-
hand sidebar of the Custom Code page - [Custom Files:](custom-files.md).
54
-
55
-
Within Custom Files you'll have the ability to edit some parts of the `main.dart` file.
56
-
57
52
### Code Copilot
58
53
59
54
Code Copilot is an AI-assisted feature that helps you generate code snippets,
@@ -122,17 +117,23 @@ When there is a compilation error, the code analyzer will stop running and displ
122
117
by the compiler. Once fixed, save the code and rerun using the Compile Code button. The code analyzer
123
118
should then be reconnected. You can also manually reconnect it if needed.
124
119
125
-
### Custom Code Automatic Imports
120
+
### Custom Code Default Imports
126
121
127
122
When creating a new custom code snippet (Actions, Widgets, or Functions) in FlutterFlow, some fundamental imports will be automatically added for you. These imports cannot be modified by the developer. Custom Functions do not allow adding any custom imports, but you can add custom imports in Custom Actions and Widgets after the line **"Do not remove or modify the code above"**.
When you edit a custom code snippet in FlutterFlow, the Settings block will open on the right. This
134
-
block may vary slightly depending on the type of custom code (Actions, Functions, Widgets), but here
135
-
we'll discuss the common settings.
128
+
When you edit a custom code snippet in FlutterFlow, the Settings menu opens on the right. This menu may vary slightly depending on the type of custom code (Actions, Functions, or Widgets), but here, we’ll cover the common settings.
129
+
130
+
#### Generate Boilerplate Code
131
+
This setting allows you to generate boilerplate code, providing a structured starting point with essential code imports and a basic widget or function structure.
After creating a new resource file, click the code icon on the Widget Settings menu to generate the boilerplate code. Then, click "Copy to Editor" to add the boilerplate to your resource file’s code editor, where you can further customize it.
136
+
136
137
137
138
#### Exclude From Compilation
138
139
@@ -156,7 +157,7 @@ context-specific data.
156
157
This option is only available for Custom Actions.
157
158
:::
158
159
159
-
####Input Arguments
160
+
## Input Arguments
160
161
161
162
When writing custom code in FlutterFlow, you can define input arguments to make your custom
162
163
functions, widgets, or actions more dynamic and reusable. Input arguments allow you to pass data
@@ -174,7 +175,7 @@ as `<YourTypeName>Struct`. For example, if your custom data type is called `Item
174
175
referenced in the generated code as `ItemsStruct`.
175
176
:::
176
177
177
-
##### Add a Callback Action
178
+
###Callback Action As Parameter
178
179
179
180
A callback action is an action passed as a parameter to a custom action or widget and triggered at some point in the future when a specific event occurs.
180
181
@@ -190,7 +191,7 @@ action with an Action Parameter `searchKeyword`. This means that the custom acti
To provide a callback action to your main custom action, check out this quick guide where we provide a "**Show Snackbar**" action to `onError`, displaying a combined text using the search keyword.
@@ -221,7 +222,7 @@ To provide a callback action to your main custom action, check out this quick gu
221
222
</iframe>
222
223
</div>
223
224
224
-
####Return Values
225
+
## Return Values
225
226
226
227
In FlutterFlow, custom code can not only take input arguments but also return values, back to
227
228
the caller. Return values allow your custom functions, or actions to pass data back to the
@@ -237,12 +238,14 @@ Here's an example of an Action that returns a _nullable_ integer.
To utilize community-built Flutter solutions in your FlutterFlow projects, you can add a "pubspec dependency". The **pubspec file** is the configuration file in Flutter projects that lists external packages or libraries, along with other project configurations.
242
+
240
243
241
244
:::tip[Scope]
242
-
You can only add a pubspec dependency to [**Custom Action**](custom-actions.md) & [**Custom Widgets**](custom-widgets.md).
245
+
You can only add a pubspec dependency to [**Custom Actions**](custom-actions.md) & [**Custom Widgets**](custom-widgets.md).
243
246
:::
244
247
245
-
### Pub.dev
248
+
####Pub.dev
246
249
[Pub.dev](https://pub.dev) is the official package repository for Dart and Flutter. It hosts a wide range of packages, libraries, and tools that developers can use to extend the functionality of their Dart and Flutter applications.
247
250
248
251
:::info[Flutter Favorite Packages]
@@ -253,6 +256,18 @@ You can explore the Flutter Favorite packages on **[pub.dev's Flutter Favorites
253
256
254
257
:::
255
258
259
+
To add a pubspec dependency from [**pub.dev**](#pubdev), follow these steps:
260
+
261
+
1. Create a new Custom Widget or Custom Action resource file, and be sure to give it a meaningful name.
262
+
263
+
2.[**Generate the boilerplate code**](#generate-boilerplate-code) and copy the basic widget or function structure into the code editor.
264
+
265
+
3. Select the [**package you want from pub.dev**](#choosing-the-correct-package-from-pubdev) and review its details.
266
+
267
+
4. Copy the package name and version, and add them to the Custom Code settings in FlutterFlow. Then, copy the import statement and add it to the list of imports in the Custom Code resource. You can also copy example code from the Example tab on the package’s pub.dev page and modify it as needed — see more in the **[Setup Code](#setup-code)** section.
268
+
269
+
5. Click "Save & **[Compile Code](#compile-code)**" to apply the changes.
270
+
256
271
### Choosing the correct package from pub.dev
257
272
258
273
You will find varieties of dependencies for a specific requirement, and choosing the best one can be
@@ -282,38 +297,45 @@ one.
282
297
283
298
When adding a pubspec dependency to your custom code in FlutterFlow, you’ll need two pieces of information: the [package name with its version number](#copy-package-name--version) and the [import statement](#copying-import-statement).
284
299
285
-
#### Copy Package Name & Version
300
+
### Setup Code
301
+
To configure your custom code with the package, copy and paste the following items from the package's pub.dev page:
286
302
287
-
To use the dependency code in our code editor, copy its name with the version. To do so, click
288
-
the **Copy to Clipboard** icon.
303
+
1.**Copy Package Name & Version**
289
304
290
-
<p></p>
305
+
To use the dependency in your Custom Action or Custom Widget resource file, go to the package's pub.dev page and click the **Copy to Clipboard** icon next to the package name and version. Then, paste it into the **Pubspec Dependency** section (bottom right) of the FlutterFlow code editor.
See **[example](#add-pubspec-dependency-to-custom-code-example-guide)** for more information.
291
311
292
-

293
312
294
-
<p></p>
295
313
296
314
:::warning
297
315
The current dependency might depend on other dependencies to work. So make sure you also copy the
298
-
name and version of all the additional dependencies to specify in the code editor.
299
-
:::
316
+
name and version of all the additional dependencies to specify in the code
300
317
301
318
You can check if the current dependency has any additional dependencies inside the '*Dependencies'*
302
319
section at the bottom right side.
303
320
321
+
304
322

323
+
:::
305
324
306
-
#### Copying import statement
325
+
2.**Copying Import Statement**
307
326
308
-
An import statement points to where the dependency's code is located. When making a custom widget or
309
-
action, place this statement at the beginning of the code editor.
327
+
An import statement specifies the location of the dependency's code. When creating a custom widget or action, add this statement at the end of the default import statements in the code editor.
310
328
311
-
Open the dependency page and select the installing tab; under the Import it section, you'll find
329
+
Open the dependency page and select the **Installing** tab; under the **Import It** section, you'll find
312
330
the import statement. To copy, click the **Copy to Clipboard** icon.
Example code is always available in the **Example** tab on the package’s pub.dev page. Copy any relevant snippets that demonstrate usage, and paste them into your custom widget or function file. You can then modify the code as needed to fit your project.
315
337
316
-
###Add Pubspec Dependency to Custom Code: Guide
338
+
## Add Pubspec Dependency to Custom Code: Example Guide
317
339
In this example, we are using the
318
340
[**flutter_rating_bar**](https://pub.dev/packages/flutter_rating_bar) dependency to create a
319
341
`ProductRatingBar` custom widget for our
@@ -349,7 +371,7 @@ FlutterFlow:
349
371
<p></p>
350
372
351
373
:::note
352
-
This example demonstrates how to add a [pub.dev](https://pub.dev) package to a Custom Widget snippet, but you can follow the same process for adding a package to Custom Actions. For a deep dive, explore the detailed documentation on **[Custom Widgets](custom-widgets.md)** and [Custom Actions](custom-actions.md).
374
+
This example demonstrates how to add a [**pub.dev**](https://pub.dev) package to a Custom Widget snippet, but you can follow the same process for adding a package to Custom Actions. For a deep dive, explore the detailed documentation on **[Custom Widgets](custom-widgets.md)** and [**Custom Actions**](custom-actions.md).
0 commit comments