main.dart 5.1 KB

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