main.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'expandable_floating_action_button.dart';
  5. import 'plugin/image_node_widget.dart';
  6. import 'plugin/youtube_link_node_widget.dart';
  7. import 'package:appflowy_editor/appflowy_editor.dart';
  8. void main() {
  9. runApp(const MyApp());
  10. }
  11. class MyApp extends StatelessWidget {
  12. const MyApp({Key? key}) : super(key: key);
  13. // This widget is the root of your application.
  14. @override
  15. Widget build(BuildContext context) {
  16. return MaterialApp(
  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: 'AppFlowyEditor 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. final editorKey = GlobalKey();
  48. int page = 0;
  49. @override
  50. Widget build(BuildContext context) {
  51. return Scaffold(
  52. body: Container(
  53. alignment: Alignment.topCenter,
  54. child: _buildBody(),
  55. ),
  56. floatingActionButton: _buildExpandableFab(),
  57. );
  58. }
  59. Widget _buildBody() {
  60. if (page == 0) {
  61. return _buildAppFlowyEditorWithExample();
  62. } else if (page == 1) {
  63. return _buildAppFlowyEditorWithEmptyDocument();
  64. } else if (page == 2) {
  65. return _buildAppFlowyEditorWithBigDocument();
  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. ],
  101. );
  102. }
  103. Widget _buildAppFlowyEditorWithEmptyDocument() {
  104. final editorState = EditorState.empty();
  105. final editor = AppFlowyEditor(
  106. editorState: editorState,
  107. keyEventHandlers: const [],
  108. customBuilders: const {},
  109. );
  110. return editor;
  111. }
  112. Widget _buildAppFlowyEditorWithExample() {
  113. return FutureBuilder<String>(
  114. future: rootBundle.loadString('assets/example.json'),
  115. builder: (context, snapshot) {
  116. if (snapshot.hasData) {
  117. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  118. return _buildAppFlowyEditor(EditorState(
  119. document: StateTree.fromJson(data),
  120. ));
  121. } else {
  122. return const Center(
  123. child: CircularProgressIndicator(),
  124. );
  125. }
  126. },
  127. );
  128. }
  129. Widget _buildAppFlowyEditorWithBigDocument() {
  130. return FutureBuilder<String>(
  131. future: rootBundle.loadString('assets/big_document.json'),
  132. builder: (context, snapshot) {
  133. if (snapshot.hasData) {
  134. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  135. return _buildAppFlowyEditor(EditorState(
  136. document: StateTree.fromJson(data),
  137. ));
  138. } else {
  139. return const Center(
  140. child: CircularProgressIndicator(),
  141. );
  142. }
  143. },
  144. );
  145. }
  146. Widget _buildAppFlowyEditor(EditorState editorState) {
  147. return Container(
  148. padding: const EdgeInsets.only(left: 20, right: 20),
  149. child: AppFlowyEditor(
  150. key: editorKey,
  151. editorState: editorState,
  152. keyEventHandlers: const [],
  153. customBuilders: {
  154. 'image': ImageNodeBuilder(),
  155. 'youtube_link': YouTubeLinkNodeBuilder()
  156. },
  157. ),
  158. );
  159. }
  160. }