main.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import 'dart:convert';
  2. import 'package:example/expandable_floating_action_button.dart';
  3. import 'package:example/plugin/image_node_widget.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flowy_editor/flowy_editor.dart';
  6. import 'package:flutter/services.dart';
  7. void main() {
  8. runApp(const MyApp());
  9. }
  10. class MyApp extends StatelessWidget {
  11. const MyApp({Key? key}) : super(key: key);
  12. // This widget is the root of your application.
  13. @override
  14. Widget build(BuildContext context) {
  15. return MaterialApp(
  16. title: 'Flutter Demo',
  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: 'FlowyEditor 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. late EditorState _editorState;
  48. int page = 0;
  49. @override
  50. Widget build(BuildContext context) {
  51. return Scaffold(
  52. appBar: AppBar(
  53. // Here we take the value from the MyHomePage object that was created by
  54. // the App.build method, and use it to set our appbar title.
  55. title: Text(widget.title),
  56. ),
  57. body: _buildBody(),
  58. floatingActionButton: ExpandableFab(
  59. distance: 112.0,
  60. children: [
  61. ActionButton(
  62. onPressed: () {
  63. if (page == 0) return;
  64. setState(() {
  65. page = 0;
  66. });
  67. },
  68. icon: const Icon(Icons.note_add),
  69. ),
  70. ActionButton(
  71. onPressed: () {
  72. if (page == 1) return;
  73. setState(() {
  74. page = 1;
  75. });
  76. },
  77. icon: const Icon(Icons.text_fields),
  78. ),
  79. ],
  80. ),
  81. );
  82. }
  83. Widget _buildBody() {
  84. if (page == 0) {
  85. return _buildFlowyEditor();
  86. } else if (page == 1) {
  87. return _buildTextField();
  88. }
  89. return Container();
  90. }
  91. Widget _buildFlowyEditor() {
  92. return FutureBuilder<String>(
  93. future: rootBundle.loadString('assets/example.json'),
  94. builder: (context, snapshot) {
  95. if (!snapshot.hasData) {
  96. return const Center(
  97. child: CircularProgressIndicator(),
  98. );
  99. } else {
  100. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  101. final document = StateTree.fromJson(data);
  102. _editorState = EditorState(
  103. document: document,
  104. );
  105. return FlowyEditor(
  106. editorState: _editorState,
  107. keyEventHandlers: const [],
  108. customBuilders: {
  109. 'image': ImageNodeBuilder(),
  110. },
  111. shortcuts: [
  112. // TODO: this won't work, just a example for now.
  113. {
  114. 'h1': (editorState, eventName) {
  115. debugPrint('shortcut => $eventName');
  116. final selectedNodes = editorState.selectedNodes;
  117. if (selectedNodes.isEmpty) {
  118. return;
  119. }
  120. final textNode = selectedNodes.first as TextNode;
  121. TransactionBuilder(editorState)
  122. ..formatText(textNode, 0, textNode.toRawString().length, {
  123. 'heading': 'h1',
  124. })
  125. ..commit();
  126. }
  127. },
  128. {
  129. 'bold': (editorState, eventName) =>
  130. debugPrint('shortcut => $eventName')
  131. },
  132. {
  133. 'underline': (editorState, eventName) =>
  134. debugPrint('shortcut => $eventName')
  135. },
  136. ],
  137. );
  138. }
  139. },
  140. );
  141. }
  142. Widget _buildTextField() {
  143. return const Center(
  144. child: TextField(),
  145. );
  146. }
  147. }