Procházet zdrojové kódy

updating quick start

Anatoly Pulyaevskiy před 4 roky
rodič
revize
e54be6e462

binární
assets/quick-start-screen-01.png Zobrazit soubor


+ 43
- 28
doc/quick-start.md Zobrazit soubor

@@ -7,66 +7,81 @@ Zefyr project consists of two packages:
7 7
 
8 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 14
 ```yaml
13 15
 dependencies:
14 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 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 27
 ```dart
37 28
 import 'package:flutter/material.dart';
29
+import 'package:quill_delta/quill_delta.dart';
38 30
 import 'package:zefyr/zefyr.dart';
39 31
 
40
-class MyWidget extends StatefulWidget {
32
+class EditorPage extends StatefulWidget {
41 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 39
   ZefyrController _controller;
40
+
41
+  /// Zefyr editor like any other input field requires a focus node.
47 42
   FocusNode _focusNode;
48 43
 
49 44
   @override
50 45
   void initState() {
51 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 49
     _controller = new ZefyrController(document);
56 50
     _focusNode = new FocusNode();
57 51
   }
58 52
 
59 53
   @override
60 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 Zobrazit soubor

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

+ 50
- 0
packages/zefyr/example/lib/src/quick_start.dart Zobrazit soubor

@@ -0,0 +1,50 @@
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
+}