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. debugShowCheckedModeBanner: false,
  17. theme: ThemeData(
  18. primarySwatch: Colors.blue,
  19. ),
  20. home: const MyHomePage(title: 'AppFlowyEditor Example'),
  21. );
  22. }
  23. }
  24. class MyHomePage extends StatefulWidget {
  25. const MyHomePage({Key? key, required this.title}) : super(key: key);
  26. final String title;
  27. @override
  28. State<MyHomePage> createState() => _MyHomePageState();
  29. }
  30. class _MyHomePageState extends State<MyHomePage> {
  31. int _pageIndex = 0;
  32. late EditorState _editorState;
  33. Future<String>? _jsonString;
  34. @override
  35. Widget build(BuildContext context) {
  36. return Scaffold(
  37. extendBodyBehindAppBar: true,
  38. body: _buildEditor(context),
  39. floatingActionButton: _buildExpandableFab(),
  40. );
  41. }
  42. Widget _buildEditor(BuildContext context) {
  43. if (_jsonString != null) {
  44. return _buildEditorWithJsonString(_jsonString!);
  45. }
  46. if (_pageIndex == 0) {
  47. return _buildEditorWithJsonString(
  48. rootBundle.loadString('assets/example.json'),
  49. );
  50. } else if (_pageIndex == 1) {
  51. return _buildEditorWithJsonString(
  52. rootBundle.loadString('assets/big_document.json'),
  53. );
  54. } else if (_pageIndex == 2) {
  55. return _buildEditorWithJsonString(
  56. Future.value(
  57. jsonEncode(EditorState.empty().document.toJson()),
  58. ),
  59. );
  60. }
  61. throw UnimplementedError();
  62. }
  63. Widget _buildEditorWithJsonString(Future<String> jsonString) {
  64. return FutureBuilder<String>(
  65. future: jsonString,
  66. builder: (_, snapshot) {
  67. if (snapshot.hasData) {
  68. _editorState = EditorState(
  69. document: StateTree.fromJson(
  70. Map<String, Object>.from(
  71. json.decode(snapshot.data!),
  72. ),
  73. ),
  74. );
  75. _editorState.logConfiguration
  76. ..level = LogLevel.all
  77. ..handler = (message) {
  78. debugPrint(message);
  79. };
  80. return SizedBox(
  81. width: MediaQuery.of(context).size.width,
  82. child: AppFlowyEditor(
  83. editorState: _editorState,
  84. editorStyle: const EditorStyle.defaultStyle(),
  85. ),
  86. );
  87. } else {
  88. return const Center(
  89. child: CircularProgressIndicator(),
  90. );
  91. }
  92. },
  93. );
  94. }
  95. Widget _buildExpandableFab() {
  96. return ExpandableFab(
  97. distance: 112.0,
  98. children: [
  99. ActionButton(
  100. icon: const Icon(Icons.abc),
  101. onPressed: () => _switchToPage(0),
  102. ),
  103. ActionButton(
  104. icon: const Icon(Icons.abc),
  105. onPressed: () => _switchToPage(1),
  106. ),
  107. ActionButton(
  108. icon: const Icon(Icons.abc),
  109. onPressed: () => _switchToPage(2),
  110. ),
  111. ActionButton(
  112. icon: const Icon(Icons.print),
  113. onPressed: () => {_exportDocument(_editorState)}),
  114. ActionButton(
  115. icon: const Icon(Icons.import_export),
  116. onPressed: () => _importDocument(),
  117. ),
  118. ],
  119. );
  120. }
  121. void _exportDocument(EditorState editorState) async {
  122. final document = editorState.document.toJson();
  123. final json = jsonEncode(document);
  124. final directory = await getTemporaryDirectory();
  125. final path = directory.path;
  126. final file = File('$path/editor.json');
  127. await file.writeAsString(json);
  128. if (mounted) {
  129. ScaffoldMessenger.of(context).showSnackBar(
  130. SnackBar(
  131. content: Text('The document is saved to the ${file.path}'),
  132. ),
  133. );
  134. }
  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. }