zefyr的引入

  1. dependencies:
  2. flutter:
  3. sdk: flutter
  4. # 因为直接引用包会导致使用的时候有个方法少了一个参数,是版本的问题,这样引入就可以
  5. cupertino_icons: ^0.1.3
  6. zefyr:
  7. git:
  8. url: https://github.com/memspace/zefyr.git
  9. path: packages/zefyr

在项目中使用 zwfyr

  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:quill_delta/quill_delta.dart';
  5. import 'package:zefyr/zefyr.dart';
  6. class EditorPage extends StatefulWidget {
  7. @override
  8. EditorPageState createState() => EditorPageState();
  9. }
  10. class EditorPageState extends State<EditorPage> {
  11. /// Allows to control the editor and the document.
  12. ZefyrController _controller;
  13. /// 焦点节点
  14. FocusNode _focusNode;
  15. @override
  16. void initState() {
  17. super.initState();
  18. _focusNode = FocusNode();
  19. _loadDocument().then((document) {
  20. setState(() {
  21. _controller = ZefyrController(document);
  22. });
  23. });
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. final body = (_controller == null)
  28. ? Center(child: CircularProgressIndicator())
  29. : ZefyrScaffold(
  30. child: ZefyrEditor(
  31. padding: EdgeInsets.all(16),
  32. controller: _controller,
  33. focusNode: _focusNode,
  34. ),
  35. );
  36. return Scaffold(
  37. appBar: AppBar(
  38. title: Text('Editor page'),
  39. actions: <Widget>[
  40. Builder(
  41. builder: (context) => IconButton(
  42. icon: Icon(Icons.save),
  43. onPressed: () => _saveDocument(context),
  44. ),
  45. )
  46. ],
  47. ),
  48. body: body,
  49. );
  50. }
  51. /// 如果需要一个页面有多个可以点击 需要配置多个json
  52. /// 如果.json 文件存在就加载,如果不存在就使用默认的
  53. Future<NotusDocument> _loadDocument() async {
  54. final file = File(Directory.systemTemp.path + '/quick_start.json');
  55. if (await file.exists()) {
  56. final contents = await file
  57. .readAsString()
  58. .then((data) => Future.delayed(Duration(seconds: 1), () => data));
  59. return NotusDocument.fromJson(jsonDecode(contents));
  60. }
  61. /// 默认数据
  62. final delta = Delta()..insert('Zefyr Quick Start\n');
  63. return NotusDocument.fromDelta(delta);
  64. }
  65. /// 保存写入的数据
  66. void _saveDocument(BuildContext context) {
  67. // Notus documents can be easily serialized to JSON by passing to
  68. // `jsonEncode` directly:
  69. final contents = jsonEncode(_controller.document);
  70. // For this example we save our document to a temporary file.
  71. final file = File(Directory.systemTemp.path + '/quick_start.json');
  72. // And show a snack bar on success.
  73. file.writeAsString(contents).then((_) {
  74. Scaffold.of(context).showSnackBar(SnackBar(content: Text('Saved.')));
  75. });
  76. }
  77. }