Skip to content

Commit 177b3d0

Browse files
committed
Add references and correct info for page gen code doc.
1 parent 12500fc commit 177b3d0

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

docs/generated-code/pages-generated-code.md

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,37 +91,40 @@ Finally, the `dispose` function in the `ProductListPageModel` class is used to c
9191
The `PageWidget` classes are responsible for creating the UI of individual pages and holding the widget tree as designed in the FlutterFlow canvas. These classes always extend Flutter's `StatefulWidget` class utilizing Flutter's built-in state management through `setState` to handle dynamic updates and interact with the app's lifecycle.
9292
9393
```js
94-
class OrderListPageWidget extends StatefulWidget {
95-
const OrderListPageWidget({
96-
super.key,
97-
98-
});
94+
class ProductListPageWidget extends StatefulWidget {
95+
const ProductListPageWidget({super.key});
9996

100-
final int? productId;
101-
102-
103-
@override
104-
State<OrderListPageWidget> createState() =>
105-
_OrderListPageWidgetState();
97+
@override
98+
State<ProductListPageWidget> createState() => _ProductListPageWidgetState();
10699
}
107100

108101
```
109102
110-
#### Route Awareness
111-
In the generated code, FlutterFlow automatically includes the `RouteAware` mixin in the **State** class. This makes the page aware of changes in the navigator's session history, allowing it to handle lifecycle events such as when the page becomes visible again after being removed.
103+
#### PageModel Initialization
104+
Within the State class, the `PageModel` object is initialized. [This class](#pagemodel-class) serves as a centralized place to manage the page’s state, handle business logic, and interact with the data layer.
112105
113106
```js
114-
class _OrderListPageWidgetState extends State<OrderListPageWidget>
115-
with RouteAware {
107+
class _ProductListPageWidgetState extends State<ProductListPageWidget> {
108+
late ProductListPageModel _model;
109+
110+
@override
111+
void initState() {
112+
super.initState();
113+
_model = createModel(context, () => ProductDetailPageModel());
114+
115+
}
116+
116117
```
117118
118-
#### PageModel Initialization
119-
Additionally, the `PageModel` class is initialized within the state class. This class serves as a centralized place to manage the page’s state, handle business logic, and interact with the data layer.
119+
#### PageModel Dispose
120+
Similarly, the [`dispose` method](#dispose) of the `PageModel` class is invoked from the **overridden** `dispose` method of the widget's **State** class. This ensures that any resources managed by the `PageModel`, such as listeners or controllers, are properly released when the widget is removed from the widget tree.
120121
121122
```js
122-
class _OrderListPageWidgetState extends State<OrderListPageWidget>
123-
with RouteAware {
124-
late OrderListPageModel _model;
123+
@override
124+
void dispose() {
125+
_model.dispose();
126+
super.dispose();
127+
}
125128
```
126129
127130
#### Global Scaffold Key
@@ -136,14 +139,37 @@ return Scaffold(
136139
```
137140
138141
#### Keyboard Dismissal
139-
Moreover, the root widget of every page is a `GestureDetector` with an `onTap` callback that unfocuses the current input field. This ensures that any active keyboard is dismissed when tapping outside an input field, improving the user experience across pages.
142+
Moreover, the root widget of every page is a `GestureDetector` with an `onTap` callback that unfocuses the current input field. This approach ensures that tapping anywhere outside an input field dismisses the keyboard or removes focus, creating a better user experience.
140143
141144
```js
142145
return GestureDetector(
143-
onTap: () => FocusScope.of(context).unfocus(),
144-
child: Scaffold(
145-
...)
146+
onTap: () {
147+
FocusScope.of(context).unfocus();
148+
FocusManager.instance.primaryFocus?.unfocus();
149+
},
150+
...)
146151
```
147152
148153
These functionalities are automatically added by FlutterFlow to ensure seamless navigation and proper keyboard handling across pages.
149154
155+
### onPageLoad Action: Generated Code
156+
157+
When you define actions for the `onPageLoad` action trigger of a Page, 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.
158+
159+
```js
160+
@override
161+
void initState() {
162+
super.initState();
163+
_model = createModel(context, () => ProductListPageModel());
164+
165+
// On page load action.
166+
SchedulerBinding.instance.addPostFrameCallback((_) async {
167+
_model.searchString = null;
168+
safeSetState(() {});
169+
... // more actions
170+
});
171+
172+
}
173+
```
174+
175+
The `addPostFrameCallback` ensures that onPageLoad actions are executed after the widget is fully built and rendered. This avoids issues caused by trying to update the UI before it is ready.

docs/resources/ui/pages/page-lifecycle.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ To add an action to **On Page Load** action trigger, follow the steps:
7474

7575
<iframe src="https://demo.arcade.software/ii0otHqkoRtPY66n4c2y?embed&show_copy_link=true" title="app.flutterflow.io/authentication" frameborder="0" loading="lazy" webkitallowfullscreen mozallowfullscreen allowfullscreen allow="clipboard-write" width="100%" height="600"></iframe>
7676

77+
:::tip[Generated Code]
78+
When you add actions to the **on Page Load** action trigger, they are executed within a `SchedulerBinding.instance.addPostFrameCallback((_)` method. This ensures that the actions run after the widget tree is fully built. For more details, refer to the [**Page: Generated Code**](../../../generated-code/pages-generated-code.md#onpageload-action-generated-code) document.
79+
:::
80+
7781
### On Phone Shake [Action Trigger]
7882

7983
Actions added under this trigger are triggered when the

0 commit comments

Comments
 (0)