-
Notifications
You must be signed in to change notification settings - Fork 96
Pages & Components Generated Code Docs #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
12500fc
Add page widget class info
PoojaB26 177b3d0
Add references and correct info for page gen code doc.
PoojaB26 4863e80
Add diagrams for generated methods & classes for model class.
PoojaB26 2023c4d
Merge branch 'refs/heads/main' into pooja/gen-code-pages-comp
PoojaB26 9fe41ee
fix refs
PoojaB26 996415c
fix refs
PoojaB26 c61ab35
Merge branch 'main' into pooja/gen-code-pages-comp
PoojaB26 596a22d
Merge branch 'refs/heads/main' into pooja/gen-code-pages-comp
PoojaB26 c826a92
minor code snippet adjustments
pinkeshmars 11e8444
Apply suggestions from code review
PoojaB26 780dfce
revise diagrams
PoojaB26 ca3d66c
Merge branch 'refs/heads/main' into pooja/gen-code-pages-comp
PoojaB26 c924ef7
add hyperlink
PoojaB26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: Components | ||
slug: /generated-code/component-model | ||
sidebar_position: 6 | ||
--- | ||
|
||
# Generated Code: Components | ||
|
||
Similar to a [**Page**](pages-generated-code.md), when creating a **[component](../resources/ui/components/intro-components.md)** in FlutterFlow, it automatically generates two files: a `Widget` class and a `Model` class. | ||
|
||
:::info[Prerequisites] | ||
This guide uses examples from the generated code of the **[EcommerceFlow demo app](https://bit.ly/ff-docs-demo-v1)**. To view the generated code directly, check out the **[Github repository](https://github.com/FlutterFlow/sample-apps/tree/main/ecommerce_flow)**. | ||
::: | ||
|
||
## ComponentModel class | ||
|
||
`ComponentModel` classes are responsible for managing the state and behavior of individual components used within a page. These classes extend the `FlutterFlowModel` class, providing a consistent structure and shared functionality across all component models. This ensures that each component's state is isolated and reusable, making the app easier to maintain and scale. | ||
|
||
The lifecycle of a `ComponentModel` and its associated widget class follows the same structure as a page. For more details, refer to the documentation on **[Generated Pages](pages-generated-code.md)**. | ||
|
||
### onComponentLoad Action: Generated Code | ||
|
||
When you define actions for the `onComponentLoad` action trigger of a component, these actions are added inside an `addPostFrameCallback` method within the page's `initState` method. This ensures that the actions are executed only after the initial widget tree is built. | ||
|
||
```js | ||
@override | ||
void initState() { | ||
super.initState(); | ||
_model = createModel(context, () => ProductListPageModel()); | ||
|
||
// On component load action. | ||
SchedulerBinding.instance.addPostFrameCallback((_) async { | ||
await _model.updateTotalCost(context); | ||
safeSetState(() {}); | ||
}); | ||
|
||
} | ||
``` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- | ||
title: FlutterFlow Model | ||
slug: /generated-code/flutterflow-model | ||
sidebar_position: 4 | ||
--- | ||
|
||
# FlutterFlow Model | ||
|
||
The `FlutterFlowModel` class is an abstract class used in FlutterFlow to provide a unified and extensible structure for managing state and behavior of widgets (both pages and components). It encapsulates **initialization, state management,** and **disposal** logic, making it easier to handle the lifecycle of widgets and their models. | ||
|
||
FlutterFlow automatically generates the `flutter_flow_model.dart` file, which contains the `FlutterFlowModel` class and utility methods like `wrapWithModel()` and `createModel()`. | ||
|
||
The diagram below illustrates how these utility classes and methods are utilized in a widget or model class: | ||
|
||
|
||
 | ||
|
||
When a component is added to your page (and every component you create [generates both a widget and a model class)](component-gen-code.md), the flow below explains how the utility classes are used when there is a child component: | ||
|
||
 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In '![page-component-generated.png]' image, I made some alignment fixes in Figma directly. You can check and replace if it looks good to you. |
||
|
||
<p></p> | ||
|
||
Here’s a breakdown of the lifecycle of `FlutterFlowModel` class: | ||
|
||
## Initialization | ||
Ensures the model is initialized **only once** and is tied to the `BuildContext` and the widget it is associated with. | ||
|
||
```js | ||
abstract class FlutterFlowModel<W extends Widget> { | ||
// Initialization methods | ||
bool _isInitialized = false; | ||
void initState(BuildContext context); | ||
void _init(BuildContext context) { | ||
if (!_isInitialized) { | ||
initState(context); | ||
_isInitialized = true; | ||
} | ||
if (context.widget is W) _widget = context.widget as W; | ||
_context = context; | ||
} | ||
``` | ||
|
||
|
||
## Widget & Context references | ||
|
||
Provides references to the associated widget and its `BuildContext`. | ||
|
||
```js | ||
// The widget associated with this model. This is useful for accessing the | ||
// parameters of the widget, for example. | ||
W? _widget; | ||
W? get widget => _widget; | ||
|
||
// The context associated with this model. | ||
BuildContext? _context; | ||
BuildContext? get context => _context; | ||
``` | ||
|
||
`_widget` and `_context` (private fields) store the widget and context references. `widget` and `context` (getters) are the public accessors for `_widget` and `_context`. | ||
|
||
## Disposal | ||
|
||
Manages the cleanup of resources when the model or widget is disposed. | ||
|
||
```js | ||
bool disposeOnWidgetDisposal = true; | ||
void dispose(); | ||
void maybeDispose() { | ||
if (disposeOnWidgetDisposal) { | ||
dispose(); | ||
} | ||
// Remove reference to widget for garbage collection purposes. | ||
_widget = null; | ||
} | ||
``` | ||
The `disposeOnWidgetDisposal` determines whether the model should be disposed when the widget is removed. This defaults to `true` for **pages** and `false` for **components** (as parent models typically manage their child components). | ||
|
||
The `maybeDispose()` checks `disposeOnWidgetDisposal` before disposing. It removes the widget reference to aid garbage collection. | ||
|
||
## Updates and Change Notification | ||
|
||
Allows the model to notify the associated widget or parent component/page when updates occur. | ||
|
||
```js | ||
// Whether to update the containing page / component on updates. | ||
bool updateOnChange = false; | ||
// Function to call when the model receives an update. | ||
VoidCallback _updateCallback = () {}; | ||
void onUpdate() => updateOnChange ? _updateCallback() : () {}; | ||
|
||
FlutterFlowModel setOnUpdate({ | ||
bool updateOnChange = false, | ||
required VoidCallback onUpdate, | ||
}) => | ||
this | ||
.._updateCallback = onUpdate | ||
..updateOnChange = updateOnChange; | ||
|
||
// Update the containing page when this model received an update. | ||
void updatePage(VoidCallback callback) { | ||
callback(); | ||
_updateCallback(); | ||
} | ||
``` | ||
|
||
## wrapWithModel() | ||
|
||
The `wrapWithModel()` method in FlutterFlow links a model to a widget and its child widgets, allowing them to access and manage state. It wraps the widget with a Provider, making the model available throughout the widget tree. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In '![page-generated.png]' image, I made some alignment fixes in Figma directly. You can check and replace if it looks good to you.