소스 검색

updating quick start

Anatoly Pulyaevskiy 5 년 전
부모
커밋
4dcf24cc46
3개의 변경된 파일108개의 추가작업 그리고 5개의 파일을 삭제
  1. BIN
      assets/quick-start-screen-02.png
  2. 76
    2
      doc/quick-start.md
  3. 32
    3
      packages/zefyr/example/lib/src/quick_start.dart

BIN
assets/quick-start-screen-02.png 파일 보기


+ 76
- 2
doc/quick-start.md 파일 보기

@@ -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.

+ 32
- 3
packages/zefyr/example/lib/src/quick_start.dart 파일 보기

@@ -1,3 +1,6 @@
1
+import 'dart:convert';
2
+import 'dart:io';
3
+
1 4
 import 'package:flutter/material.dart';
2 5
 import 'package:quill_delta/quill_delta.dart';
3 6
 import 'package:zefyr/zefyr.dart';
@@ -28,7 +31,17 @@ class EditorPageState extends State<EditorPage> {
28 31
     // Note that the editor requires special `ZefyrScaffold` widget to be
29 32
     // present somewhere up the widget tree.
30 33
     return Scaffold(
31
-      appBar: AppBar(title: Text("Editor page")),
34
+      appBar: AppBar(
35
+        title: Text("Editor page"),
36
+        actions: <Widget>[
37
+          Builder(
38
+            builder: (context) => IconButton(
39
+              icon: Icon(Icons.save),
40
+              onPressed: () => _saveDocument(context),
41
+            ),
42
+          )
43
+        ],
44
+      ),
32 45
       body: ZefyrScaffold(
33 46
         child: ZefyrEditor(
34 47
           padding: EdgeInsets.all(16),
@@ -43,8 +56,24 @@ class EditorPageState extends State<EditorPage> {
43 56
   NotusDocument _loadDocument() {
44 57
     // For simplicity we hardcode a simple document with one line of text
45 58
     // saying "Zefyr Quick Start".
46
-    final Delta delta = Delta()..insert("Zefyr Quick Start\n");
59
+//    final Delta delta = Delta()..insert("Zefyr Quick Start\n");
47 60
     // Note that delta must always end with newline.
48
-    return NotusDocument.fromDelta(delta);
61
+//    return NotusDocument.fromDelta(delta);
62
+
63
+    final file = File(Directory.systemTemp.path + "/quick_start.json");
64
+    final contents = file.readAsStringSync();
65
+    return NotusDocument.fromJson(jsonDecode(contents));
66
+  }
67
+
68
+  void _saveDocument(BuildContext context) {
69
+    // Notus documents can be easily serialized to JSON by passing to
70
+    // `jsonEncode` directly:
71
+    final contents = jsonEncode(_controller.document);
72
+    // For this example we save our document to a temporary file.
73
+    final file = File(Directory.systemTemp.path + "/quick_start.json");
74
+    // And show a snack bar on success.
75
+    file.writeAsString(contents).then((_) {
76
+      Scaffold.of(context).showSnackBar(SnackBar(content: Text("Saved.")));
77
+    });
49 78
   }
50 79
 }