main.dart 7.4 KB

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