main.dart 5.5 KB

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