|
@@ -78,10 +78,84 @@ class EditorPageState extends State<EditorPage> {
|
78
|
78
|
```
|
79
|
79
|
|
80
|
80
|
In the above example we created a page with an AppBar and Zefyr editor in its
|
81
|
|
-body. We also initialize editor with a simple one-line document. Here is how
|
|
81
|
+body. We also initialize our editor with a simple one-line document. Here is how
|
82
|
82
|
it might look when we run the app and navigate to editor page:
|
83
|
83
|
|
84
|
84
|
<img src="https://github.com/memspace/zefyr/raw/gitbook/assets/quick-start-screen-01.png" width="375">
|
85
|
85
|
|
86
|
86
|
At this point we can already edit the document and apply styles, however if
|
87
|
|
-we navigate back from this page our changes will be lost. Let's fix this.
|
|
87
|
+we navigate back from this page our changes will be lost. Let's fix this and
|
|
88
|
+add a button which saves the document to device's file system.
|
|
89
|
+
|
|
90
|
+First we need a function to save the document:
|
|
91
|
+
|
|
92
|
+```dart
|
|
93
|
+class EditorPageState extends State<EditorPage> {
|
|
94
|
+
|
|
95
|
+ // ... add after _loadDocument()
|
|
96
|
+
|
|
97
|
+ void _saveDocument(BuildContext context) {
|
|
98
|
+ // Notus documents can be easily serialized to JSON by passing to
|
|
99
|
+ // `jsonEncode` directly:
|
|
100
|
+ final contents = jsonEncode(_controller.document);
|
|
101
|
+ // For this example we save our document to a temporary file.
|
|
102
|
+ final file = File(Directory.systemTemp.path + "/quick_start.json");
|
|
103
|
+ // And show a snack bar on success.
|
|
104
|
+ file.writeAsString(contents).then((_) {
|
|
105
|
+ Scaffold.of(context).showSnackBar(SnackBar(content: Text("Saved.")));
|
|
106
|
+ });
|
|
107
|
+ }
|
|
108
|
+}
|
|
109
|
+```
|
|
110
|
+
|
|
111
|
+> Notice that we pass `BuildContext` to `_saveDocument`. This is required
|
|
112
|
+> to get access to our page's `Scaffold` state, so that we can show `SnackBar`.
|
|
113
|
+
|
|
114
|
+Now we just need to add a button to the AppBar, so we need to modify `build`
|
|
115
|
+method as follows:
|
|
116
|
+
|
|
117
|
+```dart
|
|
118
|
+class EditorPageState extends State<EditorPage> {
|
|
119
|
+
|
|
120
|
+ // ... replace build method with following
|
|
121
|
+
|
|
122
|
+ @override
|
|
123
|
+ Widget build(BuildContext context) {
|
|
124
|
+ // Note that the editor requires special `ZefyrScaffold` widget to be
|
|
125
|
+ // present somewhere up the widget tree.
|
|
126
|
+ return Scaffold(
|
|
127
|
+ appBar: AppBar(
|
|
128
|
+ title: Text("Editor page"),
|
|
129
|
+ // <<< begin change
|
|
130
|
+ actions: <Widget>[
|
|
131
|
+ Builder(
|
|
132
|
+ builder: (context) => IconButton(
|
|
133
|
+ icon: Icon(Icons.save),
|
|
134
|
+ onPressed: () => _saveDocument(context),
|
|
135
|
+ ),
|
|
136
|
+ )
|
|
137
|
+ ],
|
|
138
|
+ // end change >>>
|
|
139
|
+ ),
|
|
140
|
+ body: ZefyrScaffold(
|
|
141
|
+ child: ZefyrEditor(
|
|
142
|
+ padding: EdgeInsets.all(16),
|
|
143
|
+ controller: _controller,
|
|
144
|
+ focusNode: _focusNode,
|
|
145
|
+ ),
|
|
146
|
+ ),
|
|
147
|
+ );
|
|
148
|
+ }
|
|
149
|
+}
|
|
150
|
+```
|
|
151
|
+
|
|
152
|
+We have to use `Builder` here for our icon button because we need access to
|
|
153
|
+build context within the scope of this page's Scaffold. Everything else here
|
|
154
|
+should be straightforward.
|
|
155
|
+
|
|
156
|
+Now we can reload our app, hit "Save" button and see the snack bar.
|
|
157
|
+
|
|
158
|
+<img src="https://github.com/memspace/zefyr/raw/gitbook/assets/quick-start-screen-02.png" width="375">
|
|
159
|
+
|
|
160
|
+Since we now have this document saved to a file, let's update our
|
|
161
|
+`_loadDocument` method to load saved file if it exists.
|