main.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/old_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('image', ImageNodeBuilder.create)
  61. ..register('text/with-checkbox', TextWithCheckBoxNodeBuilder.create)
  62. ..register('text/with-heading', TextWithHeadingNodeBuilder.create);
  63. }
  64. @override
  65. Widget build(BuildContext context) {
  66. return Scaffold(
  67. appBar: AppBar(
  68. // Here we take the value from the MyHomePage object that was created by
  69. // the App.build method, and use it to set our appbar title.
  70. title: Text(widget.title),
  71. ),
  72. body: _buildBody(),
  73. floatingActionButton: 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. onPressed: () {
  87. if (page == 1) return;
  88. setState(() {
  89. page = 1;
  90. });
  91. },
  92. icon: const Icon(Icons.text_fields),
  93. ),
  94. ],
  95. ),
  96. );
  97. }
  98. Widget _buildBody() {
  99. if (page == 0) {
  100. return _buildFlowyEditor();
  101. } else if (page == 1) {
  102. return _buildTextfield();
  103. }
  104. return Container();
  105. }
  106. Widget _buildFlowyEditor() {
  107. return FutureBuilder<String>(
  108. future: rootBundle.loadString('assets/document.json'),
  109. builder: (context, snapshot) {
  110. if (!snapshot.hasData) {
  111. return const Center(
  112. child: CircularProgressIndicator(),
  113. );
  114. } else {
  115. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  116. final document = StateTree.fromJson(data);
  117. _editorState = EditorState(
  118. document: document,
  119. renderPlugins: renderPlugins,
  120. );
  121. return FlowyEditor(
  122. editorState: _editorState,
  123. keyEventHandlers: const [],
  124. shortcuts: [
  125. // TODO: this won't work, just a example for now.
  126. {
  127. 'h1': (editorState, eventName) {
  128. debugPrint('shortcut => $eventName');
  129. final selectedNodes = editorState.selectedNodes;
  130. if (selectedNodes.isEmpty) {
  131. return;
  132. }
  133. final textNode = selectedNodes.first as TextNode;
  134. TransactionBuilder(editorState)
  135. ..formatText(textNode, 0, textNode.toRawString().length, {
  136. 'heading': 'h1',
  137. })
  138. ..commit();
  139. }
  140. },
  141. {
  142. 'bold': (editorState, eventName) =>
  143. debugPrint('shortcut => $eventName')
  144. },
  145. {
  146. 'underline': (editorState, eventName) =>
  147. debugPrint('shortcut => $eventName')
  148. },
  149. ],
  150. );
  151. }
  152. },
  153. );
  154. }
  155. Widget _buildTextfield() {
  156. return const Center(
  157. child: TextField(),
  158. );
  159. }
  160. }