main.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'expandable_floating_action_button.dart';
  6. // import 'plugin/image_node_widget.dart';
  7. import 'plugin/youtube_link_node_widget.dart';
  8. import 'package:appflowy_editor/appflowy_editor.dart';
  9. import 'package:path_provider/path_provider.dart';
  10. void main() {
  11. runApp(const MyApp());
  12. }
  13. class MyApp extends StatelessWidget {
  14. const MyApp({Key? key}) : super(key: key);
  15. // This widget is the root of your application.
  16. @override
  17. Widget build(BuildContext context) {
  18. return MaterialApp(
  19. theme: ThemeData(
  20. // This is the theme of your application.
  21. //
  22. // Try running your application with "flutter run". You'll see the
  23. // application has a blue toolbar. Then, without quitting the app, try
  24. // changing the primarySwatch below to Colors.green and then invoke
  25. // "hot reload" (press "r" in the console where you ran "flutter run",
  26. // or simply save your changes to "hot reload" in a Flutter IDE).
  27. // Notice that the counter didn't reset back to zero; the application
  28. // is not restarted.
  29. primarySwatch: Colors.blue,
  30. ),
  31. home: const MyHomePage(title: 'AppFlowyEditor Example'),
  32. );
  33. }
  34. }
  35. class MyHomePage extends StatefulWidget {
  36. const MyHomePage({Key? key, required this.title}) : super(key: key);
  37. // This widget is the home page of your application. It is stateful, meaning
  38. // that it has a State object (defined below) that contains fields that affect
  39. // how it looks.
  40. // This class is the configuration for the state. It holds the values (in this
  41. // case the title) provided by the parent (in this case the App widget) and
  42. // used by the build method of the State. Fields in a Widget subclass are
  43. // always marked "final".
  44. final String title;
  45. @override
  46. State<MyHomePage> createState() => _MyHomePageState();
  47. }
  48. class _MyHomePageState extends State<MyHomePage> {
  49. final editorKey = GlobalKey();
  50. int page = 0;
  51. EditorState? _editorState;
  52. @override
  53. Widget build(BuildContext context) {
  54. return Scaffold(
  55. body: Container(
  56. alignment: Alignment.topCenter,
  57. child: _buildBody(),
  58. ),
  59. floatingActionButton: _buildExpandableFab(),
  60. );
  61. }
  62. Widget _buildBody() {
  63. if (page == 0) {
  64. return _buildAppFlowyEditorWithExample();
  65. } else if (page == 1) {
  66. return _buildAppFlowyEditorWithEmptyDocument();
  67. } else if (page == 2) {
  68. return _buildAppFlowyEditorWithBigDocument();
  69. }
  70. return Container();
  71. }
  72. Widget _buildAppFlowyEditorWithEmptyDocument() {
  73. final editorState = EditorState.empty();
  74. final editor = AppFlowyEditor(
  75. editorState: editorState,
  76. keyEventHandlers: const [],
  77. customBuilders: const {},
  78. );
  79. return editor;
  80. }
  81. Widget _buildAppFlowyEditorWithExample() {
  82. return FutureBuilder<String>(
  83. future: rootBundle.loadString('assets/example.json'),
  84. builder: (context, snapshot) {
  85. if (snapshot.hasData) {
  86. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  87. final editorState = EditorState(document: StateTree.fromJson(data));
  88. editorState.logConfiguration
  89. ..level = LogLevel.all
  90. ..handler = (message) {
  91. debugPrint(message);
  92. };
  93. _editorState = editorState;
  94. return _buildAppFlowyEditor(editorState);
  95. } else {
  96. return const Center(
  97. child: CircularProgressIndicator(),
  98. );
  99. }
  100. },
  101. );
  102. }
  103. Widget _buildAppFlowyEditorWithBigDocument() {
  104. return FutureBuilder<String>(
  105. future: rootBundle.loadString('assets/big_document.json'),
  106. builder: (context, snapshot) {
  107. if (snapshot.hasData) {
  108. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  109. return _buildAppFlowyEditor(EditorState(
  110. document: StateTree.fromJson(data),
  111. ));
  112. } else {
  113. return const Center(
  114. child: CircularProgressIndicator(),
  115. );
  116. }
  117. },
  118. );
  119. }
  120. Widget _buildAppFlowyEditor(EditorState editorState) {
  121. return Container(
  122. padding: const EdgeInsets.only(left: 20, right: 20),
  123. child: AppFlowyEditor(
  124. key: editorKey,
  125. editorState: editorState,
  126. keyEventHandlers: const [],
  127. customBuilders: {
  128. // 'image': ImageNodeBuilder(),
  129. 'youtube_link': YouTubeLinkNodeBuilder()
  130. },
  131. ),
  132. );
  133. }
  134. void _exportDocument(EditorState editorState) async {
  135. // await FileSaver.instance.saveAs(String name, Uint8List bytes, String ext, MimeType);
  136. final document = editorState.document.toJson();
  137. debugPrint(document.toString());
  138. final json = jsonEncode(document);
  139. debugPrint(json);
  140. final directory = await getTemporaryDirectory();
  141. final path = directory.path;
  142. debugPrint(path);
  143. final file = File('$path/temp.json');
  144. await file.writeAsString(json);
  145. }
  146. Widget _buildExpandableFab() {
  147. return ExpandableFab(
  148. distance: 112.0,
  149. children: [
  150. ActionButton(
  151. onPressed: () {
  152. if (page == 0) return;
  153. setState(() {
  154. page = 0;
  155. });
  156. },
  157. icon: const Icon(Icons.note_add),
  158. ),
  159. ActionButton(
  160. icon: const Icon(Icons.document_scanner),
  161. onPressed: () {
  162. if (page == 1) return;
  163. setState(() {
  164. page = 1;
  165. });
  166. },
  167. ),
  168. ActionButton(
  169. onPressed: () {
  170. if (page == 2) return;
  171. setState(() {
  172. page = 2;
  173. });
  174. },
  175. icon: const Icon(Icons.text_fields),
  176. ),
  177. ActionButton(
  178. onPressed: () {
  179. _exportDocument(_editorState!);
  180. },
  181. icon: const Icon(Icons.print),
  182. ),
  183. ],
  184. );
  185. }
  186. }