main.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:appflowy_editor/appflowy_editor.dart';
  7. import 'expandable_floating_action_button.dart';
  8. void main() {
  9. runApp(const MyApp());
  10. }
  11. class MyApp extends StatelessWidget {
  12. const MyApp({Key? key}) : super(key: key);
  13. @override
  14. Widget build(BuildContext context) {
  15. return MaterialApp(
  16. theme: ThemeData(
  17. primarySwatch: Colors.blue,
  18. ),
  19. home: const MyHomePage(title: 'AppFlowyEditor Example'),
  20. );
  21. }
  22. }
  23. class MyHomePage extends StatefulWidget {
  24. const MyHomePage({Key? key, required this.title}) : super(key: key);
  25. final String title;
  26. @override
  27. State<MyHomePage> createState() => _MyHomePageState();
  28. }
  29. class _MyHomePageState extends State<MyHomePage> {
  30. int _pageIndex = 0;
  31. late EditorState _editorState;
  32. Future<String>? _jsonString;
  33. @override
  34. Widget build(BuildContext context) {
  35. return Scaffold(
  36. body: Container(
  37. alignment: Alignment.topCenter,
  38. child: _buildEditor(context),
  39. ),
  40. floatingActionButton: _buildExpandableFab(),
  41. );
  42. }
  43. Widget _buildEditor(BuildContext context) {
  44. if (_jsonString != null) {
  45. return _buildEditorWithJsonPath(_jsonString!);
  46. }
  47. if (_pageIndex == 0) {
  48. return _buildEditorWithJsonPath(
  49. rootBundle.loadString('assets/example.json'));
  50. } else if (_pageIndex == 1) {
  51. return _buildEditorWithJsonPath(
  52. rootBundle.loadString('assets/big_document.json'));
  53. } else if (_pageIndex == 2) {
  54. return _buildEditorWithEmptyDocument();
  55. }
  56. throw UnimplementedError();
  57. }
  58. Widget _buildEditorWithJsonPath(Future<String> jsonString) {
  59. return FutureBuilder<String>(
  60. future: jsonString,
  61. builder: (_, snapshot) {
  62. if (snapshot.hasData) {
  63. _editorState = EditorState(
  64. document: StateTree.fromJson(
  65. Map<String, Object>.from(
  66. json.decode(snapshot.data!),
  67. ),
  68. ),
  69. );
  70. _editorState.logConfiguration
  71. ..level = LogLevel.all
  72. ..handler = (message) {
  73. debugPrint(message);
  74. };
  75. return Container(
  76. padding: const EdgeInsets.only(left: 20, right: 20),
  77. child: AppFlowyEditor(
  78. editorState: _editorState,
  79. ),
  80. );
  81. } else {
  82. return const Center(
  83. child: CircularProgressIndicator(),
  84. );
  85. }
  86. },
  87. );
  88. }
  89. Widget _buildEditorWithEmptyDocument() {
  90. _editorState = EditorState.empty();
  91. _editorState.logConfiguration
  92. ..level = LogLevel.all
  93. ..handler = (message) {
  94. debugPrint(message);
  95. };
  96. final editor = AppFlowyEditor(
  97. editorState: _editorState,
  98. );
  99. return editor;
  100. }
  101. Widget _buildExpandableFab() {
  102. return ExpandableFab(
  103. distance: 112.0,
  104. children: [
  105. ActionButton(
  106. icon: const Icon(Icons.abc),
  107. onPressed: () => _switchToPage(0),
  108. ),
  109. ActionButton(
  110. icon: const Icon(Icons.abc),
  111. onPressed: () => _switchToPage(1),
  112. ),
  113. ActionButton(
  114. icon: const Icon(Icons.abc),
  115. onPressed: () => _switchToPage(2),
  116. ),
  117. ActionButton(
  118. icon: const Icon(Icons.print),
  119. onPressed: () => _exportDocument(_editorState),
  120. ),
  121. ActionButton(
  122. icon: const Icon(Icons.import_export),
  123. onPressed: () => _importDocument(),
  124. ),
  125. ],
  126. );
  127. }
  128. void _exportDocument(EditorState editorState) async {
  129. final document = editorState.document.toJson();
  130. final json = jsonEncode(document);
  131. final directory = await getTemporaryDirectory();
  132. final path = directory.path;
  133. final file = File('$path/editor.json');
  134. await file.writeAsString(json);
  135. }
  136. void _importDocument() async {
  137. final directory = await getTemporaryDirectory();
  138. final path = directory.path;
  139. final file = File('$path/editor.json');
  140. setState(() {
  141. _jsonString = file.readAsString();
  142. });
  143. }
  144. void _switchToPage(int pageIndex) {
  145. if (pageIndex != _pageIndex) {
  146. setState(() {
  147. _pageIndex = pageIndex;
  148. });
  149. }
  150. }
  151. }