|
7 | 7 | - [When to Use State Management](#when-to-use-state-management "When to Use State Management") |
8 | 8 | - [Lifecycle](#lifecycle "Lifecycle") |
9 | 9 | - [Updating a State](#updating-a-state "Updating a State") |
| 10 | +- [Sending actions](#sending-state-actions "Sending actions") |
10 | 11 | - [State Actions](#state-actions "State Actions") |
11 | 12 | - [Building Your First Widget](#building-your-first-widget "Building Your First Widget") |
12 | 13 |
|
|
17 | 18 |
|
18 | 19 | In Nylo {{$version}}, you can build Widgets that use State Management. |
19 | 20 |
|
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. |
21 | 26 |
|
22 | 27 |
|
23 | 28 | ### Let's first understand State Management |
@@ -201,6 +206,146 @@ class HomeController extends Controller { |
201 | 206 |
|
202 | 207 | 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. |
203 | 208 |
|
| 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 | + |
204 | 349 | <a name="building-your-first-widget"></a> |
205 | 350 | <br> |
206 | 351 |
|
|
0 commit comments