zefyr的引入
dependencies:
flutter:
sdk: flutter
# 因为直接引用包会导致使用的时候有个方法少了一个参数,是版本的问题,这样引入就可以
cupertino_icons: ^0.1.3
zefyr:
git:
url: https://github.com/memspace/zefyr.git
path: packages/zefyr
在项目中使用 zwfyr
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:quill_delta/quill_delta.dart';
import 'package:zefyr/zefyr.dart';
class EditorPage extends StatefulWidget {
@override
EditorPageState createState() => EditorPageState();
}
class EditorPageState extends State<EditorPage> {
/// Allows to control the editor and the document.
ZefyrController _controller;
/// 焦点节点
FocusNode _focusNode;
@override
void initState() {
super.initState();
_focusNode = FocusNode();
_loadDocument().then((document) {
setState(() {
_controller = ZefyrController(document);
});
});
}
@override
Widget build(BuildContext context) {
final body = (_controller == null)
? Center(child: CircularProgressIndicator())
: ZefyrScaffold(
child: ZefyrEditor(
padding: EdgeInsets.all(16),
controller: _controller,
focusNode: _focusNode,
),
);
return Scaffold(
appBar: AppBar(
title: Text('Editor page'),
actions: <Widget>[
Builder(
builder: (context) => IconButton(
icon: Icon(Icons.save),
onPressed: () => _saveDocument(context),
),
)
],
),
body: body,
);
}
/// 如果需要一个页面有多个可以点击 需要配置多个json
/// 如果.json 文件存在就加载,如果不存在就使用默认的
Future<NotusDocument> _loadDocument() async {
final file = File(Directory.systemTemp.path + '/quick_start.json');
if (await file.exists()) {
final contents = await file
.readAsString()
.then((data) => Future.delayed(Duration(seconds: 1), () => data));
return NotusDocument.fromJson(jsonDecode(contents));
}
/// 默认数据
final delta = Delta()..insert('Zefyr Quick Start\n');
return NotusDocument.fromDelta(delta);
}
/// 保存写入的数据
void _saveDocument(BuildContext context) {
// Notus documents can be easily serialized to JSON by passing to
// `jsonEncode` directly:
final 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.')));
});
}
}