main.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. body: Container(
  38. alignment: Alignment.topCenter,
  39. child: _buildEditor(context),
  40. ),
  41. floatingActionButton: _buildExpandableFab(),
  42. );
  43. }
  44. Widget _buildEditor(BuildContext context) {
  45. if (_jsonString != null) {
  46. return _buildEditorWithJsonString(_jsonString!);
  47. }
  48. if (_pageIndex == 0) {
  49. return _buildEditorWithJsonString(
  50. rootBundle.loadString('assets/example.json'),
  51. );
  52. } else if (_pageIndex == 1) {
  53. return _buildEditorWithJsonString(
  54. rootBundle.loadString('assets/big_document.json'),
  55. );
  56. } else if (_pageIndex == 2) {
  57. return _buildEditorWithJsonString(
  58. Future.value(
  59. jsonEncode(EditorState.empty().document.toJson()),
  60. ),
  61. );
  62. }
  63. throw UnimplementedError();
  64. }
  65. Widget _buildEditorWithJsonString(Future<String> jsonString) {
  66. return FutureBuilder<String>(
  67. future: jsonString,
  68. builder: (_, snapshot) {
  69. if (snapshot.hasData) {
  70. _editorState = EditorState(
  71. document: StateTree.fromJson(
  72. Map<String, Object>.from(
  73. json.decode(snapshot.data!),
  74. ),
  75. ),
  76. );
  77. _editorState.logConfiguration
  78. ..level = LogLevel.all
  79. ..handler = (message) {
  80. debugPrint(message);
  81. };
  82. return Container(
  83. padding: const EdgeInsets.all(20),
  84. child: AppFlowyEditor(
  85. editorState: _editorState,
  86. ),
  87. );
  88. } else {
  89. return const Center(
  90. child: CircularProgressIndicator(),
  91. );
  92. }
  93. },
  94. );
  95. }
  96. Widget _buildExpandableFab() {
  97. return ExpandableFab(
  98. distance: 112.0,
  99. children: [
  100. ActionButton(
  101. icon: const Icon(Icons.abc),
  102. onPressed: () => _switchToPage(0),
  103. ),
  104. ActionButton(
  105. icon: const Icon(Icons.abc),
  106. onPressed: () => _switchToPage(1),
  107. ),
  108. ActionButton(
  109. icon: const Icon(Icons.abc),
  110. onPressed: () => _switchToPage(2),
  111. ),
  112. ActionButton(
  113. icon: const Icon(Icons.print),
  114. onPressed: () => {_exportDocument(_editorState)}),
  115. ActionButton(
  116. icon: const Icon(Icons.import_export),
  117. onPressed: () => _importDocument(),
  118. ),
  119. ],
  120. );
  121. }
  122. void _exportDocument(EditorState editorState) async {
  123. final document = editorState.document.toJson();
  124. final json = jsonEncode(document);
  125. final directory = await getTemporaryDirectory();
  126. final path = directory.path;
  127. final file = File('$path/editor.json');
  128. await file.writeAsString(json);
  129. if (mounted) {
  130. ScaffoldMessenger.of(context).showSnackBar(
  131. SnackBar(
  132. content: Text('The document is saved to the ${file.path}'),
  133. ),
  134. );
  135. }
  136. }
  137. void _importDocument() async {
  138. final directory = await getTemporaryDirectory();
  139. final path = directory.path;
  140. final file = File('$path/editor.json');
  141. setState(() {
  142. _jsonString = file.readAsString();
  143. });
  144. }
  145. void _switchToPage(int pageIndex) {
  146. if (pageIndex != _pageIndex) {
  147. setState(() {
  148. _pageIndex = pageIndex;
  149. });
  150. }
  151. }
  152. }