main.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import 'dart:convert';
  2. import 'package:example/expandable_floating_action_button.dart';
  3. import 'package:example/plugin/document_node_widget.dart';
  4. import 'package:example/plugin/selected_text_node_widget.dart';
  5. import 'package:example/plugin/text_with_heading_node_widget.dart';
  6. import 'package:example/plugin/image_node_widget.dart';
  7. import 'package:example/plugin/text_node_widget.dart';
  8. import 'package:example/plugin/text_with_check_box_node_widget.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flowy_editor/flowy_editor.dart';
  11. import 'package:flutter/services.dart';
  12. void main() {
  13. runApp(const MyApp());
  14. }
  15. class MyApp extends StatelessWidget {
  16. const MyApp({Key? key}) : super(key: key);
  17. // This widget is the root of your application.
  18. @override
  19. Widget build(BuildContext context) {
  20. return MaterialApp(
  21. title: 'Flutter Demo',
  22. theme: ThemeData(
  23. // This is the theme of your application.
  24. //
  25. // Try running your application with "flutter run". You'll see the
  26. // application has a blue toolbar. Then, without quitting the app, try
  27. // changing the primarySwatch below to Colors.green and then invoke
  28. // "hot reload" (press "r" in the console where you ran "flutter run",
  29. // or simply save your changes to "hot reload" in a Flutter IDE).
  30. // Notice that the counter didn't reset back to zero; the application
  31. // is not restarted.
  32. primarySwatch: Colors.blue,
  33. ),
  34. home: const MyHomePage(title: 'FlowyEditor Example'),
  35. );
  36. }
  37. }
  38. class MyHomePage extends StatefulWidget {
  39. const MyHomePage({Key? key, required this.title}) : super(key: key);
  40. // This widget is the home page of your application. It is stateful, meaning
  41. // that it has a State object (defined below) that contains fields that affect
  42. // how it looks.
  43. // This class is the configuration for the state. It holds the values (in this
  44. // case the title) provided by the parent (in this case the App widget) and
  45. // used by the build method of the State. Fields in a Widget subclass are
  46. // always marked "final".
  47. final String title;
  48. @override
  49. State<MyHomePage> createState() => _MyHomePageState();
  50. }
  51. class _MyHomePageState extends State<MyHomePage> {
  52. final RenderPlugins renderPlugins = RenderPlugins();
  53. late EditorState _editorState;
  54. int page = 0;
  55. @override
  56. void initState() {
  57. super.initState();
  58. renderPlugins
  59. ..register('editor', EditorNodeWidgetBuilder.create)
  60. ..register('text', SelectedTextNodeBuilder.create)
  61. ..register('image', ImageNodeBuilder.create)
  62. ..register('text/with-checkbox', TextWithCheckBoxNodeBuilder.create)
  63. ..register('text/with-heading', TextWithHeadingNodeBuilder.create);
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return Scaffold(
  68. appBar: AppBar(
  69. // Here we take the value from the MyHomePage object that was created by
  70. // the App.build method, and use it to set our appbar title.
  71. title: Text(widget.title),
  72. ),
  73. body: _buildBody(),
  74. floatingActionButton: ExpandableFab(
  75. distance: 112.0,
  76. children: [
  77. ActionButton(
  78. onPressed: () {
  79. if (page == 0) return;
  80. setState(() {
  81. page = 0;
  82. });
  83. },
  84. icon: const Icon(Icons.note_add),
  85. ),
  86. ActionButton(
  87. onPressed: () {
  88. if (page == 1) return;
  89. setState(() {
  90. page = 1;
  91. });
  92. },
  93. icon: const Icon(Icons.text_fields),
  94. ),
  95. ],
  96. ),
  97. );
  98. }
  99. Widget _buildBody() {
  100. if (page == 0) {
  101. return _buildFlowyEditor();
  102. } else if (page == 1) {
  103. return _buildTextfield();
  104. }
  105. return Container();
  106. }
  107. Widget _buildFlowyEditor() {
  108. return FutureBuilder<String>(
  109. future: rootBundle.loadString('assets/document.json'),
  110. builder: (context, snapshot) {
  111. if (!snapshot.hasData) {
  112. return const Center(
  113. child: CircularProgressIndicator(),
  114. );
  115. } else {
  116. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  117. final document = StateTree.fromJson(data);
  118. _editorState = EditorState(
  119. document: document,
  120. renderPlugins: renderPlugins,
  121. );
  122. return FlowyEditor(
  123. editorState: _editorState,
  124. keyEventHandlers: const [],
  125. shortcuts: [
  126. // TODO: this won't work, just a example for now.
  127. {
  128. 'h1': (editorState, eventName) {
  129. debugPrint('shortcut => $eventName');
  130. final selectedNodes = editorState.selectedNodes;
  131. if (selectedNodes.isEmpty) {
  132. return;
  133. }
  134. final textNode = selectedNodes.first as TextNode;
  135. TransactionBuilder(editorState)
  136. ..formatText(textNode, 0, textNode.toRawString().length, {
  137. 'heading': 'h1',
  138. })
  139. ..commit();
  140. }
  141. },
  142. {
  143. 'bold': (editorState, eventName) =>
  144. debugPrint('shortcut => $eventName')
  145. },
  146. {
  147. 'underline': (editorState, eventName) =>
  148. debugPrint('shortcut => $eventName')
  149. },
  150. ],
  151. );
  152. }
  153. },
  154. );
  155. }
  156. Widget _buildTextfield() {
  157. return const Center(
  158. child: TextField(),
  159. );
  160. }
  161. }