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