main.dart 5.7 KB

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