main.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 _buildAppFlowyEditorWithEmptyDocument() {
  70. final editorState = EditorState.empty();
  71. final editor = AppFlowyEditor(
  72. editorState: editorState,
  73. keyEventHandlers: const [],
  74. customBuilders: const {},
  75. );
  76. return editor;
  77. }
  78. Widget _buildAppFlowyEditorWithExample() {
  79. return FutureBuilder<String>(
  80. future: rootBundle.loadString('assets/example.json'),
  81. builder: (context, snapshot) {
  82. if (snapshot.hasData) {
  83. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  84. final editorState = EditorState(document: StateTree.fromJson(data));
  85. editorState.logConfiguration
  86. ..level = LogLevel.all
  87. ..handler = (message) {
  88. debugPrint(message);
  89. };
  90. return _buildAppFlowyEditor(editorState);
  91. } else {
  92. return const Center(
  93. child: CircularProgressIndicator(),
  94. );
  95. }
  96. },
  97. );
  98. }
  99. Widget _buildAppFlowyEditorWithBigDocument() {
  100. return FutureBuilder<String>(
  101. future: rootBundle.loadString('assets/big_document.json'),
  102. builder: (context, snapshot) {
  103. if (snapshot.hasData) {
  104. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  105. return _buildAppFlowyEditor(EditorState(
  106. document: StateTree.fromJson(data),
  107. ));
  108. } else {
  109. return const Center(
  110. child: CircularProgressIndicator(),
  111. );
  112. }
  113. },
  114. );
  115. }
  116. Widget _buildAppFlowyEditor(EditorState editorState) {
  117. return Container(
  118. padding: const EdgeInsets.only(left: 20, right: 20),
  119. child: AppFlowyEditor(
  120. key: editorKey,
  121. editorState: editorState,
  122. keyEventHandlers: const [],
  123. customBuilders: {
  124. // 'image': ImageNodeBuilder(),
  125. 'youtube_link': YouTubeLinkNodeBuilder()
  126. },
  127. ),
  128. );
  129. }
  130. Widget _buildExpandableFab() {
  131. return ExpandableFab(
  132. distance: 112.0,
  133. children: [
  134. ActionButton(
  135. onPressed: () {
  136. if (page == 0) return;
  137. setState(() {
  138. page = 0;
  139. });
  140. },
  141. icon: const Icon(Icons.note_add),
  142. ),
  143. ActionButton(
  144. icon: const Icon(Icons.document_scanner),
  145. onPressed: () {
  146. if (page == 1) return;
  147. setState(() {
  148. page = 1;
  149. });
  150. },
  151. ),
  152. ActionButton(
  153. onPressed: () {
  154. if (page == 2) return;
  155. setState(() {
  156. page = 2;
  157. });
  158. },
  159. icon: const Icon(Icons.text_fields),
  160. ),
  161. ],
  162. );
  163. }
  164. }