main.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. ],
  92. ),
  93. );
  94. }
  95. Widget _buildBody() {
  96. if (page == 0) {
  97. return _buildFlowyEditor();
  98. } else if (page == 1) {
  99. return _buildFlowyEditorWithEmptyDocument();
  100. } else if (page == 2) {
  101. return _buildTextField();
  102. }
  103. return Container();
  104. }
  105. Widget _buildFlowyEditorWithEmptyDocument() {
  106. return Container(
  107. padding: const EdgeInsets.only(left: 20, right: 20),
  108. child: FlowyEditor(
  109. key: editorKey,
  110. editorState: EditorState(
  111. document: StateTree(
  112. root: Node(
  113. type: 'editor',
  114. children: LinkedList()
  115. ..add(
  116. TextNode.empty()
  117. ..delta = Delta(
  118. [TextInsert('')],
  119. ),
  120. ),
  121. attributes: {},
  122. ),
  123. ),
  124. ),
  125. keyEventHandlers: const [],
  126. customBuilders: {
  127. 'image': ImageNodeBuilder(),
  128. },
  129. ),
  130. );
  131. }
  132. Widget _buildFlowyEditor() {
  133. return FutureBuilder<String>(
  134. future: rootBundle.loadString('assets/example.json'),
  135. builder: (context, snapshot) {
  136. if (!snapshot.hasData) {
  137. return const Center(
  138. child: CircularProgressIndicator(),
  139. );
  140. } else {
  141. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  142. final document = StateTree.fromJson(data);
  143. _editorState = EditorState(
  144. document: document,
  145. );
  146. return Container(
  147. padding: const EdgeInsets.only(left: 20, right: 20),
  148. child: FlowyEditor(
  149. key: editorKey,
  150. editorState: _editorState,
  151. keyEventHandlers: const [],
  152. customBuilders: {
  153. 'image': ImageNodeBuilder(),
  154. 'youtube_link': YouTubeLinkNodeBuilder()
  155. },
  156. ),
  157. // shortcuts: [
  158. // // TODO: this won't work, just a example for now.
  159. // {
  160. // 'h1': (editorState, eventName) {
  161. // debugPrint('shortcut => $eventName');
  162. // final selectedNodes = editorState.selectedNodes;
  163. // if (selectedNodes.isEmpty) {
  164. // return;
  165. // }
  166. // final textNode = selectedNodes.first as TextNode;
  167. // TransactionBuilder(editorState)
  168. // ..formatText(textNode, 0, textNode.toRawString().length, {
  169. // 'heading': 'h1',
  170. // })
  171. // ..commit();
  172. // }
  173. // },
  174. // {
  175. // 'bold': (editorState, eventName) =>
  176. // debugPrint('shortcut => $eventName')
  177. // },
  178. // {
  179. // 'underline': (editorState, eventName) =>
  180. // debugPrint('shortcut => $eventName')
  181. // },
  182. // ],
  183. );
  184. }
  185. },
  186. );
  187. }
  188. Widget _buildTextField() {
  189. return const Center(
  190. child: TextField(),
  191. );
  192. }
  193. }