Skip to content

Commit 3111a84

Browse files
Pages & Components Generated Code Docs (#252)
* Add page widget class info * Add references and correct info for page gen code doc. * Add diagrams for generated methods & classes for model class. * fix refs * fix refs * minor code snippet adjustments * Apply suggestions from code review Co-authored-by: pinkeshmars <[email protected]> * revise diagrams * add hyperlink --------- Co-authored-by: Pinkesh <[email protected]> Co-authored-by: pinkeshmars <[email protected]>
1 parent c451424 commit 3111a84

File tree

9 files changed

+337
-69
lines changed

9 files changed

+337
-69
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: Components
3+
slug: /generated-code/component-model
4+
sidebar_position: 6
5+
---
6+
7+
# Generated Code: Components
8+
9+
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.
10+
11+
:::info[Prerequisites]
12+
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)**.
13+
:::
14+
15+
## ComponentModel class
16+
17+
`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.
18+
19+
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)**.
20+
21+
### onComponentLoad Action: Generated Code
22+
23+
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.
24+
25+
```js
26+
@override
27+
void initState() {
28+
super.initState();
29+
_model = createModel(context, () => ProductListPageModel());
30+
31+
// On component load action.
32+
SchedulerBinding.instance.addPostFrameCallback((_) async {
33+
await _model.updateTotalCost(context);
34+
safeSetState(() {});
35+
});
36+
37+
}
38+
```
39+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: FlutterFlow Model
3+
slug: /generated-code/flutterflow-model
4+
sidebar_position: 4
5+
---
6+
7+
# FlutterFlow Model
8+
9+
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.
10+
11+
FlutterFlow automatically generates the `flutter_flow_model.dart` file, which contains the `FlutterFlowModel` class and utility methods like `wrapWithModel()` and `createModel()`.
12+
13+
The diagram below illustrates how these utility classes and methods are utilized in a widget or model class:
14+
15+
16+
![page-generated.png](imgs/page-generated.png)
17+
18+
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:
19+
20+
![page-component-generated.png](imgs/page-component-generated.png)
21+
22+
<p></p>
23+
24+
Here’s a breakdown of the lifecycle of `FlutterFlowModel` class:
25+
26+
## Initialization
27+
Ensures the model is initialized **only once** and is tied to the `BuildContext` and the widget it is associated with.
28+
29+
```js
30+
abstract class FlutterFlowModel<W extends Widget> {
31+
// Initialization methods
32+
bool _isInitialized = false;
33+
void initState(BuildContext context);
34+
void _init(BuildContext context) {
35+
if (!_isInitialized) {
36+
initState(context);
37+
_isInitialized = true;
38+
}
39+
if (context.widget is W) _widget = context.widget as W;
40+
_context = context;
41+
}
42+
```
43+
44+
45+
## Widget & Context references
46+
47+
Provides references to the associated widget and its `BuildContext`.
48+
49+
```js
50+
// The widget associated with this model. This is useful for accessing the
51+
// parameters of the widget, for example.
52+
W? _widget;
53+
W? get widget => _widget;
54+
55+
// The context associated with this model.
56+
BuildContext? _context;
57+
BuildContext? get context => _context;
58+
```
59+
60+
`_widget` and `_context` (private fields) store the widget and context references. `widget` and `context` (getters) are the public accessors for `_widget` and `_context`.
61+
62+
## Disposal
63+
64+
Manages the cleanup of resources when the model or widget is disposed.
65+
66+
```js
67+
bool disposeOnWidgetDisposal = true;
68+
void dispose();
69+
void maybeDispose() {
70+
if (disposeOnWidgetDisposal) {
71+
dispose();
72+
}
73+
// Remove reference to widget for garbage collection purposes.
74+
_widget = null;
75+
}
76+
```
77+
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).
78+
79+
The `maybeDispose()` checks `disposeOnWidgetDisposal` before disposing. It removes the widget reference to aid garbage collection.
80+
81+
## Updates and Change Notification
82+
83+
Allows the model to notify the associated widget or parent component/page when updates occur.
84+
85+
```js
86+
// Whether to update the containing page / component on updates.
87+
bool updateOnChange = false;
88+
// Function to call when the model receives an update.
89+
VoidCallback _updateCallback = () {};
90+
void onUpdate() => updateOnChange ? _updateCallback() : () {};
91+
92+
FlutterFlowModel setOnUpdate({
93+
bool updateOnChange = false,
94+
required VoidCallback onUpdate,
95+
}) =>
96+
this
97+
.._updateCallback = onUpdate
98+
..updateOnChange = updateOnChange;
99+
100+
// Update the containing page when this model received an update.
101+
void updatePage(VoidCallback callback) {
102+
callback();
103+
_updateCallback();
104+
}
105+
```
106+
107+
## wrapWithModel()
108+
109+
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.
130 KB
Loading
77.4 KB
Loading
96.8 KB
Loading

docs/generated-code/page-model.md

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)