Skip to content

Commit 9cebde9

Browse files
blueneogeogitbook-bot
authored andcommitted
GitBook: [master] 3 pages modified
1 parent 375b53d commit 9cebde9

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* [Shortcuts](guide/overview.md)
2121
* [Styling with CSS](guide/styling-with-css.md)
2222
* [Writing Reactive code](guide/writing-reactive-code.md)
23-
* [Untitled](guide/untitled.md)
23+
* [Styling per platform](guide/untitled.md)
2424

2525
## Shortcuts Reference
2626

guide/untitled.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# Untitled
1+
# Styling per platform
22

guide/writing-reactive-code.md

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Our task has a name and can be done:
4444
{% code-tabs-item title="lib/model/task.dart" %}
4545
```dart
4646
import 'package:meta/meta.dart';
47-
import 'package:scoped_model/scoped_model.dart';
47+
import 'package:flutter_view_tools/flutter_view_tools.dart';
4848
4949
class Task extends Model {
5050
@@ -62,7 +62,7 @@ Our application has a model that contains the list of tasks we want to keep:
6262
{% code-tabs %}
6363
{% code-tabs-item title="lib/model/appmodel.dart" %}
6464
```dart
65-
import 'package:scoped_model/scoped_model.dart';
65+
import 'package:flutter_view_tools/flutter_view_tools.dart';
6666
import 'package:todolist/model/task.dart';
6767
6868
class AppModel extends Model {
@@ -93,7 +93,7 @@ Our view model starts out simple, for now it only needs a reference to the app,
9393
{% code-tabs-item title="lib/pages/taskspage/taskspage-model.dart" %}
9494
```dart
9595
import 'package:meta/meta.dart';
96-
import 'package:scoped_model/scoped_model.dart';
96+
import 'package:flutter_view_tools/flutter_view_tools.dart';
9797
import 'package:todolist/model/app-model.dart';
9898
9999
class TasksPageModel extends Model {
@@ -443,7 +443,7 @@ At line 4 we have added a computed [**text-decoration**](../reference/css-proper
443443
{% code-tabs-item title="lib/pages/taskspage/taskspage-model.dart" %}
444444
```dart
445445
import 'package:meta/meta.dart';
446-
import 'package:scoped_model/scoped_model.dart';
446+
import 'package:flutter_view_tools/flutter_view_tools.dart';
447447
import 'package:todolist/model/app-model.dart';
448448
449449
class TasksPageModel extends Model {
@@ -463,11 +463,81 @@ Now completed tasks look more completed:
463463

464464
![done means done!](../.gitbook/assets/screen-shot-2018-12-19-at-4.10.57-pm.png)
465465

466+
## Monitoring the state lifecycle
467+
468+
You may need to _**initialize**_ some things in your **view-model** when the state of your layout starts, and _**free**_ those resources when the state is disposed of. In that case, **use a** [**lifecycle**](../reference/tag-shortcuts.md#lifecycle) **widget**. It is part of the flutter-view-tools library.
469+
470+
For example, let's say your **view** has a [**ListView**](https://docs.flutter.io/flutter/widgets/ListView-class.html) widget with a long list and you want to be able to load more items when you scroll near the bottom.
471+
472+
This requires we create a [**ScrollController**](https://docs.flutter.io/flutter/widgets/ScrollController-class.html) in the **view-model** and pass it to the [**ListView**](https://docs.flutter.io/flutter/widgets/ListView-class.html) ****in the **view**. We can then listen to the [**ScrollController**](https://docs.flutter.io/flutter/widgets/ScrollController-class.html) and react when the scrollposition is very low. Finally, when the **view** is disposed of, we also want our listener to be removed.
473+
474+
In the todo app we can do this as follows in the **view**:
475+
476+
{% code-tabs %}
477+
{% code-tabs-item title="tasks-page.pug" %}
478+
```css
479+
#body(as='body')
480+
lifecycle(:on-init='model.init' :on-dispose='model.dispose')
481+
list-view(:controller='model.scrollController')
482+
task-entry(for='task in model.app.tasks' :task='task' :model='model')
483+
```
484+
{% endcode-tabs-item %}
485+
{% endcode-tabs %}
486+
487+
At line **2** we add the [**lifecycle**](../reference/tag-shortcuts.md#lifecycle) widget with handlers for the init and dispose events, and pass them along to the **view-model**.
488+
489+
At line **3** we pass the scrollController of the view-model to the list, so we can monitor the scrolling position.
490+
491+
The **view-model** needs to store the scrollController, initialize it and dispose of it:
492+
493+
{% code-tabs %}
494+
{% code-tabs-item title="lib/pages/taskspage/taskspage-model.dart" %}
495+
```dart
496+
import 'package:meta/meta.dart';
497+
import 'package:flutter/widgets.dart'
498+
import 'package:flutter_view_tools/flutter_view_tools.dart';
499+
import 'package:todolist/model/app-model.dart';
500+
501+
class TasksPageModel extends Model {
502+
TasksPageModel({@required this.app})
503+
: scrollController = ScrollController(initialScrollOffset: 0.0, keepScrollOffset: true);
504+
505+
final AppModel app;
506+
final flutter.ScrollController scrollController;
507+
508+
init() {
509+
this.scrollController.addListener(this.onScroll)
510+
}
511+
512+
dispose() {
513+
this.scrollController.removeListener(this.onScroll)
514+
}
515+
516+
onScroll() {
517+
if (this.scrollController.position.extentAfter < 500) {
518+
// if not already loading, load more entries here!
519+
}
520+
}
521+
522+
taskTextDecoration(Task task) {
523+
return task.done ? TextDecoration.lineThrough : TextDecoration.none;
524+
}
525+
}
526+
```
527+
{% endcode-tabs-item %}
528+
{% endcode-tabs %}
529+
530+
At line 8 the **scrollController** is set up.
531+
532+
At lines **13** and **17** we add the **init** and **dispose** methods that are called by the lifecycle widget. These will start and stop listening to our scrollController.
533+
534+
Finally at line **21** we have an **onScroll** method that actually checks the scroll position and loads more entries.
535+
466536
## See the full example
467537

468-
This covers the basics of how to make any app react to your model changes.
538+
This covers the basics of how to structure a reactive app with flutter-view, ScopedModel and the flutter-view-tools.
469539

470-
Of course our todo app is still incomplete,however with this you should be able to understand the full implementation. See [the full example](../get-started/examples.md#todolist) with [source code](https://github.com/flutter-view/examples/tree/master/todolist) for a full implementation of this todo app. Notable additions there are:
540+
Of course our todo app is still incomplete, however you now should be able to understand the full implementation. See [the full example](../get-started/examples.md#todolist) with [source code](https://github.com/flutter-view/examples/tree/master/todolist) for a full implementation of this todo app. Notable additions there are:
471541

472542
* an add-task-dialog view that lets you enter a new task
473543
* being able to check tasks as done

0 commit comments

Comments
 (0)