main.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import 'dart:collection';
  2. import 'dart:convert';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:example/expandable_floating_action_button.dart';
  6. import 'package:example/plugin/image_node_widget.dart';
  7. import 'package:example/plugin/youtube_link_node_widget.dart';
  8. import 'package:flowy_editor/flowy_editor.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. 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. final editorKey = GlobalKey();
  49. int page = 0;
  50. @override
  51. Widget build(BuildContext context) {
  52. return Scaffold(
  53. body: Container(
  54. alignment: Alignment.topCenter,
  55. child: _buildBody(),
  56. ),
  57. floatingActionButton: _buildExpandableFab(),
  58. );
  59. }
  60. Widget _buildBody() {
  61. if (page == 0) {
  62. return _buildFlowyEditorWithExample();
  63. } else if (page == 1) {
  64. return _buildFlowyEditorWithEmptyDocument();
  65. } else if (page == 2) {
  66. return _buildTextField();
  67. } else if (page == 3) {
  68. return _buildFlowyEditorWithBigDocument();
  69. }
  70. return Container();
  71. }
  72. Widget _buildExpandableFab() {
  73. return ExpandableFab(
  74. distance: 112.0,
  75. children: [
  76. ActionButton(
  77. onPressed: () {
  78. if (page == 0) return;
  79. setState(() {
  80. page = 0;
  81. });
  82. },
  83. icon: const Icon(Icons.note_add),
  84. ),
  85. ActionButton(
  86. icon: const Icon(Icons.document_scanner),
  87. onPressed: () {
  88. if (page == 1) return;
  89. setState(() {
  90. page = 1;
  91. });
  92. },
  93. ),
  94. ActionButton(
  95. onPressed: () {
  96. if (page == 2) return;
  97. setState(() {
  98. page = 2;
  99. });
  100. },
  101. icon: const Icon(Icons.text_fields),
  102. ),
  103. ActionButton(
  104. onPressed: () {
  105. if (page == 3) return;
  106. setState(() {
  107. page = 3;
  108. });
  109. },
  110. icon: const Icon(Icons.email),
  111. ),
  112. ],
  113. );
  114. }
  115. Widget _buildFlowyEditorWithEmptyDocument() {
  116. return _buildFlowyEditor(
  117. EditorState(
  118. document: StateTree(
  119. root: Node(
  120. type: 'editor',
  121. children: LinkedList()
  122. ..add(
  123. TextNode.empty()
  124. ..delta = Delta(
  125. [TextInsert('')],
  126. ),
  127. ),
  128. attributes: {},
  129. ),
  130. ),
  131. ),
  132. );
  133. }
  134. Widget _buildFlowyEditorWithExample() {
  135. return FutureBuilder<String>(
  136. future: rootBundle.loadString('assets/example.json'),
  137. builder: (context, snapshot) {
  138. if (snapshot.hasData) {
  139. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  140. return _buildFlowyEditor(EditorState(
  141. document: StateTree.fromJson(data),
  142. ));
  143. } else {
  144. return const Center(
  145. child: CircularProgressIndicator(),
  146. );
  147. }
  148. },
  149. );
  150. }
  151. Widget _buildFlowyEditorWithBigDocument() {
  152. return FutureBuilder<String>(
  153. future: rootBundle.loadString('assets/big_document.json'),
  154. builder: (context, snapshot) {
  155. if (snapshot.hasData) {
  156. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  157. return _buildFlowyEditor(EditorState(
  158. document: StateTree.fromJson(data),
  159. ));
  160. } else {
  161. return const Center(
  162. child: CircularProgressIndicator(),
  163. );
  164. }
  165. },
  166. );
  167. }
  168. Widget _buildFlowyEditor(EditorState editorState) {
  169. return Container(
  170. padding: const EdgeInsets.only(left: 20, right: 20),
  171. child: FlowyEditor(
  172. key: editorKey,
  173. editorState: editorState,
  174. keyEventHandlers: const [],
  175. customBuilders: {
  176. 'image': ImageNodeBuilder(),
  177. 'youtube_link': YouTubeLinkNodeBuilder()
  178. },
  179. ),
  180. );
  181. }
  182. Widget _buildTextField() {
  183. return const Center(
  184. child: TextField(),
  185. );
  186. }
  187. }