Browse Source

updating quick start

Anatoly Pulyaevskiy 5 years ago
parent
commit
e54be6e462

BIN
assets/quick-start-screen-01.png View File


+ 43
- 28
doc/quick-start.md View File

7
 
7
 
8
 ### Installation
8
 ### Installation
9
 
9
 
10
-Add `zefyr` package as a dependency to your `pubspec.yaml`:
10
+> We assume that you already installed [Flutter](https://flutter.dev/docs/get-started/install) and created a [project](https://flutter.dev/docs/get-started/test-drive).
11
+
12
+Add `zefyr` package as a dependency to `pubspec.yaml` of your project:
11
 
13
 
12
 ```yaml
14
 ```yaml
13
 dependencies:
15
 dependencies:
14
   zefyr: [latest_version]
16
   zefyr: [latest_version]
15
 ```
17
 ```
16
 
18
 
17
-And run `flutter packages get` to install. This installs both `zefyr`
18
-and `notus` packages.
19
+And run `flutter packages get`, this installs both `zefyr` and `notus` packages.
19
 
20
 
20
 ### Usage
21
 ### Usage
21
 
22
 
22
-There are 4 main objects you would normally interact with in your code:
23
-
24
-* `NotusDocument`, represents a rich text document and provides high-level methods for manipulating the document's state, like inserting, deleting and formatting of text. Read [documentation](concepts/data-and-document.md) for more details on Notus document model and data format.
25
-* `ZefyrEditor`, a Flutter widget responsible for rendering of rich text on the screen and reacting to user actions.
26
-* `ZefyrController`, ties the above two objects together.
27
-* `ZefyrScaffold`, allows embedding Zefyr toolbar into any custom layout.
28
-
29
-Note that `ZefyrEditor` depends on presence of `ZefyrScaffold` somewhere up the
30
-widget tree.
31
-
32
-Normally you would need to place `ZefyrEditor` inside of a
33
-`StatefulWidget`. Shown below is a minimal setup required to use the
34
-editor:
23
+We start by creating a `StatefulWidget` that will be responsible for handling
24
+all the state and interactions with Zefyr. In this example we'll assume
25
+that there is dedicated editor page in our app:
35
 
26
 
36
 ```dart
27
 ```dart
37
 import 'package:flutter/material.dart';
28
 import 'package:flutter/material.dart';
29
+import 'package:quill_delta/quill_delta.dart';
38
 import 'package:zefyr/zefyr.dart';
30
 import 'package:zefyr/zefyr.dart';
39
 
31
 
40
-class MyWidget extends StatefulWidget {
32
+class EditorPage extends StatefulWidget {
41
   @override
33
   @override
42
-  MyWidgetState createState() => MyWidgetState();
34
+  EditorPageState createState() => EditorPageState();
43
 }
35
 }
44
 
36
 
45
-class MyWidgetState extends State<MyWidget> {
37
+class EditorPageState extends State<EditorPage> {
38
+  /// Allows to control the editor and the document.
46
   ZefyrController _controller;
39
   ZefyrController _controller;
40
+
41
+  /// Zefyr editor like any other input field requires a focus node.
47
   FocusNode _focusNode;
42
   FocusNode _focusNode;
48
 
43
 
49
   @override
44
   @override
50
   void initState() {
45
   void initState() {
51
     super.initState();
46
     super.initState();
52
-    // Create an empty document or load existing if you have one.
53
-    // Here we create an empty document:
54
-    final document = new NotusDocument();
47
+    // Here we must load the document and pass it to Zefyr controller.
48
+    final document = _loadDocument();
55
     _controller = new ZefyrController(document);
49
     _controller = new ZefyrController(document);
56
     _focusNode = new FocusNode();
50
     _focusNode = new FocusNode();
57
   }
51
   }
58
 
52
 
59
   @override
53
   @override
60
   Widget build(BuildContext context) {
54
   Widget build(BuildContext context) {
61
-    return ZefyrScaffold(
62
-      child: ZefyrEditor(
63
-        controller: _controller,
64
-        focusNode: _focusNode,
55
+    // Note that the editor requires special `ZefyrScaffold` widget to be
56
+    // present somewhere up the widget tree.
57
+    return Scaffold(
58
+      appBar: AppBar(title: Text("Editor page")),
59
+      body: ZefyrScaffold(
60
+        child: ZefyrEditor(
61
+          padding: EdgeInsets.all(16),
62
+          controller: _controller,
63
+          focusNode: _focusNode,
64
+        ),
65
       ),
65
       ),
66
     );
66
     );
67
   }
67
   }
68
+
69
+  /// Loads the document to be edited in Zefyr.
70
+  NotusDocument _loadDocument() {
71
+    // For simplicity we hardcode a simple document with one line of text
72
+    // saying "Zefyr Quick Start".
73
+    final Delta delta = Delta()..insert("Zefyr Quick Start\n");
74
+    // Note that delta must always end with newline.
75
+    return NotusDocument.fromDelta(delta);
76
+  }
68
 }
77
 }
69
 ```
78
 ```
70
 
79
 
71
-This above is required minimum to include Zefyr in your Flutter app and start
72
-writing some rich text.
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
82
+it might look when we run the app and navigate to editor page:
83
+
84
+<img src="https://github.com/memspace/zefyr/raw/gitbook/assets/quick-start-screen-01.png" width="375">
85
+
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.

+ 14
- 1
packages/zefyr/example/lib/main.dart View File

2
 // for details. All rights reserved. Use of this source code is governed by a
2
 // for details. All rights reserved. Use of this source code is governed by a
3
 // BSD-style license that can be found in the LICENSE file.
3
 // BSD-style license that can be found in the LICENSE file.
4
 import 'package:flutter/material.dart';
4
 import 'package:flutter/material.dart';
5
+
5
 import 'src/form.dart';
6
 import 'src/form.dart';
6
 import 'src/full_page.dart';
7
 import 'src/full_page.dart';
8
+import 'src/quick_start.dart';
7
 import 'src/view.dart';
9
 import 'src/view.dart';
8
 
10
 
9
 void main() {
11
 void main() {
16
     return MaterialApp(
18
     return MaterialApp(
17
       debugShowCheckedModeBanner: false,
19
       debugShowCheckedModeBanner: false,
18
       title: 'Zefyr Editor',
20
       title: 'Zefyr Editor',
19
-      theme: ThemeData(primarySwatch: Colors.cyan),
21
+//      theme: ThemeData(primarySwatch: Colors.cyan),
20
       home: HomePage(),
22
       home: HomePage(),
21
       routes: {
23
       routes: {
22
         "/fullPage": buildFullPage,
24
         "/fullPage": buildFullPage,
23
         "/form": buildFormPage,
25
         "/form": buildFormPage,
24
         "/view": buildViewPage,
26
         "/view": buildViewPage,
27
+        "/quick-start": buildQuickStart,
25
       },
28
       },
26
     );
29
     );
27
   }
30
   }
37
   Widget buildViewPage(BuildContext context) {
40
   Widget buildViewPage(BuildContext context) {
38
     return ViewScreen();
41
     return ViewScreen();
39
   }
42
   }
43
+
44
+  Widget buildQuickStart(BuildContext context) {
45
+    return EditorPage();
46
+  }
40
 }
47
 }
41
 
48
 
42
 class HomePage extends StatelessWidget {
49
 class HomePage extends StatelessWidget {
71
             color: Colors.lightBlue,
78
             color: Colors.lightBlue,
72
             textColor: Colors.white,
79
             textColor: Colors.white,
73
           ),
80
           ),
81
+          FlatButton(
82
+            onPressed: () => nav.pushNamed('/quick-start'),
83
+            child: Text('Quick Start Tutorial'),
84
+            color: Colors.lightBlue,
85
+            textColor: Colors.white,
86
+          ),
74
           Expanded(child: Container()),
87
           Expanded(child: Container()),
75
         ],
88
         ],
76
       ),
89
       ),

+ 50
- 0
packages/zefyr/example/lib/src/quick_start.dart View File

1
+import 'package:flutter/material.dart';
2
+import 'package:quill_delta/quill_delta.dart';
3
+import 'package:zefyr/zefyr.dart';
4
+
5
+class EditorPage extends StatefulWidget {
6
+  @override
7
+  EditorPageState createState() => EditorPageState();
8
+}
9
+
10
+class EditorPageState extends State<EditorPage> {
11
+  /// Allows to control the editor and the document.
12
+  ZefyrController _controller;
13
+
14
+  /// Zefyr editor like any other input field requires a focus node.
15
+  FocusNode _focusNode;
16
+
17
+  @override
18
+  void initState() {
19
+    super.initState();
20
+    // Here we must load the document and pass it to Zefyr controller.
21
+    final document = _loadDocument();
22
+    _controller = new ZefyrController(document);
23
+    _focusNode = new FocusNode();
24
+  }
25
+
26
+  @override
27
+  Widget build(BuildContext context) {
28
+    // Note that the editor requires special `ZefyrScaffold` widget to be
29
+    // present somewhere up the widget tree.
30
+    return Scaffold(
31
+      appBar: AppBar(title: Text("Editor page")),
32
+      body: ZefyrScaffold(
33
+        child: ZefyrEditor(
34
+          padding: EdgeInsets.all(16),
35
+          controller: _controller,
36
+          focusNode: _focusNode,
37
+        ),
38
+      ),
39
+    );
40
+  }
41
+
42
+  /// Loads the document to be edited in Zefyr.
43
+  NotusDocument _loadDocument() {
44
+    // For simplicity we hardcode a simple document with one line of text
45
+    // saying "Zefyr Quick Start".
46
+    final Delta delta = Delta()..insert("Zefyr Quick Start\n");
47
+    // Note that delta must always end with newline.
48
+    return NotusDocument.fromDelta(delta);
49
+  }
50
+}