main.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import 'dart:collection';
  2. import 'dart:convert';
  3. import 'package:example/expandable_floating_action_button.dart';
  4. import 'package:example/plugin/image_node_widget.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flowy_editor/flowy_editor.dart';
  7. import 'package:flutter/services.dart';
  8. void main() {
  9. runApp(const MyApp());
  10. }
  11. class MyApp extends StatelessWidget {
  12. const MyApp({Key? key}) : super(key: key);
  13. // This widget is the root of your application.
  14. @override
  15. Widget build(BuildContext context) {
  16. return MaterialApp(
  17. title: 'Flutter Demo',
  18. theme: ThemeData(
  19. // This is the theme of your application.
  20. //
  21. // Try running your application with "flutter run". You'll see the
  22. // application has a blue toolbar. Then, without quitting the app, try
  23. // changing the primarySwatch below to Colors.green and then invoke
  24. // "hot reload" (press "r" in the console where you ran "flutter run",
  25. // or simply save your changes to "hot reload" in a Flutter IDE).
  26. // Notice that the counter didn't reset back to zero; the application
  27. // is not restarted.
  28. primarySwatch: Colors.blue,
  29. ),
  30. home: const MyHomePage(title: 'FlowyEditor Example'),
  31. );
  32. }
  33. }
  34. class MyHomePage extends StatefulWidget {
  35. const MyHomePage({Key? key, required this.title}) : super(key: key);
  36. // This widget is the home page of your application. It is stateful, meaning
  37. // that it has a State object (defined below) that contains fields that affect
  38. // how it looks.
  39. // This class is the configuration for the state. It holds the values (in this
  40. // case the title) provided by the parent (in this case the App widget) and
  41. // used by the build method of the State. Fields in a Widget subclass are
  42. // always marked "final".
  43. final String title;
  44. @override
  45. State<MyHomePage> createState() => _MyHomePageState();
  46. }
  47. class _MyHomePageState extends State<MyHomePage> {
  48. late EditorState _editorState;
  49. final editorKey = GlobalKey();
  50. int page = 0;
  51. @override
  52. Widget build(BuildContext context) {
  53. return Scaffold(
  54. appBar: AppBar(
  55. // Here we take the value from the MyHomePage object that was created by
  56. // the App.build method, and use it to set our appbar title.
  57. title: Text(widget.title),
  58. ),
  59. body: _buildBody(),
  60. floatingActionButton: ExpandableFab(
  61. distance: 112.0,
  62. children: [
  63. ActionButton(
  64. onPressed: () {
  65. if (page == 0) return;
  66. setState(() {
  67. page = 0;
  68. });
  69. },
  70. icon: const Icon(Icons.note_add),
  71. ),
  72. ActionButton(
  73. icon: const Icon(Icons.document_scanner),
  74. onPressed: () {
  75. if (page == 1) return;
  76. setState(() {
  77. page = 1;
  78. });
  79. },
  80. ),
  81. ActionButton(
  82. onPressed: () {
  83. if (page == 2) return;
  84. setState(() {
  85. page = 2;
  86. });
  87. },
  88. icon: const Icon(Icons.text_fields),
  89. ),
  90. ],
  91. ),
  92. );
  93. }
  94. Widget _buildBody() {
  95. if (page == 0) {
  96. return _buildFlowyEditor();
  97. } else if (page == 1) {
  98. return _buildFlowyEditorWithEmptyDocument();
  99. } else if (page == 2) {
  100. return _buildTextField();
  101. }
  102. return Container();
  103. }
  104. Widget _buildFlowyEditorWithEmptyDocument() {
  105. return Container(
  106. padding: const EdgeInsets.only(left: 20, right: 20),
  107. child: FlowyEditor(
  108. key: editorKey,
  109. editorState: EditorState(
  110. document: StateTree(
  111. root: Node(
  112. type: 'editor',
  113. children: LinkedList()
  114. ..add(
  115. TextNode.empty()
  116. ..delta = Delta(
  117. [TextInsert('')],
  118. ),
  119. ),
  120. attributes: {},
  121. ),
  122. ),
  123. ),
  124. keyEventHandlers: const [],
  125. customBuilders: {
  126. 'image': ImageNodeBuilder(),
  127. },
  128. ),
  129. );
  130. }
  131. Widget _buildFlowyEditor() {
  132. return FutureBuilder<String>(
  133. future: rootBundle.loadString('assets/example.json'),
  134. builder: (context, snapshot) {
  135. if (!snapshot.hasData) {
  136. return const Center(
  137. child: CircularProgressIndicator(),
  138. );
  139. } else {
  140. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  141. final document = StateTree.fromJson(data);
  142. _editorState = EditorState(
  143. document: document,
  144. );
  145. return Container(
  146. padding: const EdgeInsets.only(left: 20, right: 20),
  147. child: FlowyEditor(
  148. key: editorKey,
  149. editorState: _editorState,
  150. keyEventHandlers: const [],
  151. customBuilders: {
  152. 'image': ImageNodeBuilder(),
  153. },
  154. ),
  155. // shortcuts: [
  156. // // TODO: this won't work, just a example for now.
  157. // {
  158. // 'h1': (editorState, eventName) {
  159. // debugPrint('shortcut => $eventName');
  160. // final selectedNodes = editorState.selectedNodes;
  161. // if (selectedNodes.isEmpty) {
  162. // return;
  163. // }
  164. // final textNode = selectedNodes.first as TextNode;
  165. // TransactionBuilder(editorState)
  166. // ..formatText(textNode, 0, textNode.toRawString().length, {
  167. // 'heading': 'h1',
  168. // })
  169. // ..commit();
  170. // }
  171. // },
  172. // {
  173. // 'bold': (editorState, eventName) =>
  174. // debugPrint('shortcut => $eventName')
  175. // },
  176. // {
  177. // 'underline': (editorState, eventName) =>
  178. // debugPrint('shortcut => $eventName')
  179. // },
  180. // ],
  181. );
  182. }
  183. },
  184. );
  185. }
  186. Widget _buildTextField() {
  187. return const Center(
  188. child: TextField(),
  189. );
  190. }
  191. }