Add zefyr package as a dependency to pubspec.yaml of your new project:
dependencies:zefyr: [latest_version]
And run flutter packages get. This installs zefyr and all required dependencies, including notus package which implements Zefyr's document model.
Notus package is platform-agnostic and can be used outside of Flutter apps (in web or server-side Dart projects).
03. Create editor page
We start by creating a StatefulWidget that will be responsible for handling all the state and interactions with Zefyr. In this example we'll assume that there is dedicated editor page in our app.
Create a new file lib/src/editor_page.dart and type in (or paste) the following:
import'package:flutter/material.dart';import'package:quill_delta/quill_delta.dart';import'package:zefyr/zefyr.dart';classEditorPageextendsStatefulWidget {@overrideEditorPageStatecreateState() =>EditorPageState();}classEditorPageStateextendsState<EditorPage> {/// Allows to control the editor and the document.ZefyrController _controller;/// Zefyr editor like any other input field requires a focus node.FocusNode _focusNode;@overridevoidinitState() { super.initState();// Here we must load the document and pass it to Zefyr controller.final document =_loadDocument(); _controller =ZefyrController(document); _focusNode =FocusNode(); }@overrideWidgetbuild(BuildContext context) {// Note that the editor requires special `ZefyrScaffold` widget to be// one of its parents.returnScaffold( appBar:AppBar(title:Text("Editor page")), body:ZefyrScaffold( child:ZefyrEditor( padding:EdgeInsets.all(16), controller: _controller, focusNode: _focusNode, ), ), ); }/// Loads the document to be edited in Zefyr.NotusDocument_loadDocument() {// For simplicity we hardcode a simple document with one line of text// saying "Zefyr Quick Start".// (Note that delta must always end with newline.)finalDelta delta =Delta()..insert("Zefyr Quick Start\n");returnNotusDocument.fromDelta(delta); }}
Above example widget creates a page with an AppBar and Zefyr editor in its body. We also initialize our editor with a simple one-line document.
Now we need to wire it up with our app. Open lib/main.dart and replace autogenerated contents with this:
Here is how it might look when we run the app and navigate to editor page:
04. Save document to JSON file
At this point we can already edit the document and apply styles, however if we navigate back from this page our changes will be lost. Let's fix this and add a button which saves the document to the device's file system.
First we need a function to save the document. Update lib/src/editor_page.dart as follows:
// change: add these two lines to imports section at the top of the fileimport'dart:convert'; // access to jsonEncode()import'dart:io'; // access to File and Directory classesclassEditorPageStateextendsState<EditorPage> {// change: add after _loadDocument()void_saveDocument(BuildContext context) {// Notus documents can be easily serialized to JSON by passing to// `jsonEncode` directlyfinal contents =jsonEncode(_controller.document);// For this example we save our document to a temporary file.final file =File(Directory.systemTemp.path +"/quick_start.json");// And show a snack bar on success. file.writeAsString(contents).then((_) {Scaffold.of(context).showSnackBar(SnackBar(content:Text("Saved."))); }); }}
This function converts our document using jsonEncode() function and writes the result to a file quick_start.json in the system's temporary directory.
Note that File.writeAsString is an asynchronous method and returns Dart's Future. This is why we register a completion callback with a call to Future.then.
One more important bit here is that we pass BuildContext argument to _saveDocument. This is required to get access to our page's Scaffold state, so that we can show a SnackBar.
Now we just need to add a button to the AppBar, so we need to modify build method as follows:
classEditorPageStateextendsState<EditorPage> {// change: replace build() method with following@overrideWidgetbuild(BuildContext context) {// Note that the editor requires special `ZefyrScaffold` widget to be// present somewhere up the widget tree.returnScaffold( appBar:AppBar( title:Text("Editor page"),// <<< begin change actions:<Widget>[Builder( builder: (context) =>IconButton( icon:Icon(Icons.save), onPressed: () =>_saveDocument(context), ), ) ],// end change >>> ), body:ZefyrScaffold( child:ZefyrEditor( padding:EdgeInsets.all(16), controller: _controller, focusNode: _focusNode, ), ), ); }}
We have to use Builder here for our icon button because we need BuildContext which has access to Scaffold widget's state.
Now we can reload our app, hit "Save" button and see the snack bar.
05. Load document from JSON file
Since we now have this document saved to a file, let's update our _loadDocument method to load saved file if it exists.
classEditorPageStateextendsState<EditorPage> {// change: replace _loadDocument() method with following/// Loads the document asynchronously from a file if it exists, otherwise /// returns default document.Future<NotusDocument> _loadDocument() async {final file =File(Directory.systemTemp.path +"/quick_start.json");if (await file.exists()) {final contents =await file.readAsString();returnNotusDocument.fromJson(jsonDecode(contents)); }finalDelta delta =Delta()..insert("Zefyr Quick Start\n");returnNotusDocument.fromDelta(delta); }}
We had to convert this method to be async because file system operations are asynchronous. This breaks our initState logic so we need to fix it next. However we can no longer initialize ZefyrController in initState and therefore can't display the editor until document is loaded.
One way to fix this is to show loader animation while we are reading our document from file. But first, we still need to update initState method:
We initialize _controller only when our document is fully loaded from the file system. An important part here is to update _controller field inside of setState call as required by Flutter's StatefulWidget's contract.
The only thing left is to update build() method to show loader animation:
classEditorPageStateextendsState<EditorPage> {// change: replace build() method with following@overrideWidgetbuild(BuildContext context) {// If _controller is null we show Material Design loader, otherwise// display Zefyr editor.finalWidget body = (_controller ==null)?Center(child:CircularProgressIndicator()):ZefyrScaffold( child:ZefyrEditor( padding:EdgeInsets.all(16), controller: _controller, focusNode: _focusNode, ), );returnScaffold( appBar:AppBar( title:Text("Editor page"), actions:<Widget>[Builder( builder: (context) =>IconButton( icon:Icon(Icons.save), onPressed: () =>_saveDocument(context), ), ) ], ), body: body, ); }}
If we save changes now and reload the app we should see something like this:
Note that in your tests you'll likely not notice any loading animation at all. This is because reading a tiny file from disk is too fast. For the above recording we added an artificial delay of 1 second in order to demonstrate loading. If you'd like to replicate this, we'll leave implementation of this task to you as an exercise.