Skip to content

Commit 64d9f2f

Browse files
committed
Changes and tweaks
1 parent 6fc2912 commit 64d9f2f

File tree

2 files changed

+149
-1
lines changed

2 files changed

+149
-1
lines changed

resources/docs/6.x/state-management.md

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [When to Use State Management](#when-to-use-state-management "When to Use State Management")
88
- [Lifecycle](#lifecycle "Lifecycle")
99
- [Updating a State](#updating-a-state "Updating a State")
10+
- [Sending actions](#sending-state-actions "Sending actions")
1011
- [State Actions](#state-actions "State Actions")
1112
- [Building Your First Widget](#building-your-first-widget "Building Your First Widget")
1213

@@ -17,7 +18,11 @@
1718

1819
In Nylo {{$version}}, you can build Widgets that use State Management.
1920

20-
In this section, we will learn about the `NyState`, and `NyPage` class. We'll also dive into some examples.
21+
Nylo provides two classes for State Management:
22+
- `NyState` - This is used for building reusable widgets.
23+
- `NyPage` - This is used for building pages in your application.
24+
25+
In this section you'll learn how to use State Management in your Nylo project.
2126

2227

2328
### Let's first understand State Management
@@ -201,6 +206,146 @@ class HomeController extends Controller {
201206

202207
You can use the `StateAction` class to update the state of any page/widget in your application as long as the widget is state managed.
203208

209+
<a name="sending-state-actions"></a>
210+
<br>
211+
212+
## Sending State Actions
213+
214+
In Nylo, you can send action events to your Widgets.
215+
216+
```
217+
// Send an action to the widget
218+
sendAction('hello_world_in_widget', state: MyWidget.state);
219+
220+
// Another example
221+
sendAction('reset_data', state: HighScore.state);
222+
```
223+
224+
In your widget, you need the following code to handle the action.
225+
226+
``` dart
227+
...
228+
@override
229+
get stateActions => {
230+
"hello_world_in_widget": () {
231+
print('Hello world');
232+
},
233+
"reset_data": () async {
234+
// Example
235+
_textController.clear();
236+
_myData = null;
237+
setState(() {});
238+
},
239+
};
240+
```
241+
242+
This is useful when you want to update the state of a widget from another widget or class.
243+
244+
### NyState - State Actions
245+
246+
First, create your state managed widget.
247+
248+
``` bash
249+
metro make:state_managed_widget my_widget
250+
```
251+
252+
This will create a new state managed widget called `MyWidget` in the `lib/resources/widgets/` directory.
253+
254+
If you open that file, you'll be able to define your state actions.
255+
256+
``` dart
257+
class _MyWidgetState extends NyState<MyWidget> {
258+
...
259+
260+
@override
261+
get stateActions => {
262+
"print_hello_world": () {
263+
print('Hello from the widget');
264+
},
265+
"reset_data": () {
266+
// Example
267+
_textController.clear();
268+
_myData = null;
269+
setState(() {});
270+
},
271+
};
272+
```
273+
274+
Finally, you can send the action from anywhere in your application.
275+
276+
``` dart
277+
sendAction('print_hello_world', state: MyWidget.state);
278+
279+
// prints 'Hello from the widget'
280+
281+
sendAction('reset_data', state: MyWidget.state);
282+
283+
// Reset data in widget
284+
```
285+
286+
### NyPage - State Actions
287+
288+
First, create your state managed page.
289+
290+
``` bash
291+
metro make:page my_page
292+
```
293+
294+
This will create a new state managed page called `MyPage` in the `lib/resources/pages/` directory.
295+
296+
If you open that file, you'll be able to define your state actions.
297+
298+
``` dart
299+
class _MyPageState extends NyPage<MyPage> {
300+
...
301+
302+
@override
303+
bool get stateManaged => true;
304+
305+
@override
306+
get stateActions => {
307+
"test_page_action": () {
308+
print('Hello from the page');
309+
},
310+
"reset_data": () {
311+
// Example
312+
_textController.clear();
313+
_myData = null;
314+
setState(() {});
315+
},
316+
};
317+
```
318+
319+
Finally, you can send the action from anywhere in your application.
320+
321+
``` dart
322+
sendAction('test_page_action', state: MyPage.state);
323+
324+
// prints 'Hello from the page'
325+
```
326+
327+
You can also define your state actions using the `whenStateAction` method.
328+
329+
``` dart
330+
@override
331+
stateUpdated(dynamic data) async {
332+
super.stateUpdated(data);
333+
...
334+
whenStateAction({
335+
"reset_badge": () {
336+
// Reset the badge count
337+
_count = 0;
338+
}
339+
});
340+
}
341+
```
342+
343+
Then you can send the action from anywhere in your application.
344+
345+
``` dart
346+
sendAction('reset_badge', state: MyPage.state);
347+
```
348+
204349
<a name="building-your-first-widget"></a>
205350
<br>
206351

resources/docs/6.x/what-is-nylo.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,6 @@ Here's a shout out to the contributors who have helped build {{ config('app.name
7979
- <a href="https://github.com/abdulawalarif" target="_blank">abdulawalarif</a>
8080
- <a href="https://github.com/lepresk" target="_blank">lepresk</a>
8181
- <a href="https://github.com/joshua1996" target="_blank">joshua1996</a>
82+
- <a href="https://github.com/stensonb" target="_blank">stensonb</a>
83+
- <a href="https://github.com/ruwiss" target="_blank">ruwiss</a>
84+
- <a href="https://github.com/rytisder" target="_blank">rytisder</a>

0 commit comments

Comments
 (0)