main.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'dart:convert';
  2. import 'package:example/plugin/document_node_widget.dart';
  3. import 'package:example/plugin/selected_text_node_widget.dart';
  4. import 'package:example/plugin/text_with_heading_node_widget.dart';
  5. import 'package:example/plugin/image_node_widget.dart';
  6. import 'package:example/plugin/text_node_widget.dart';
  7. import 'package:example/plugin/text_with_check_box_node_widget.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flowy_editor/flowy_editor.dart';
  10. import 'package:flutter/services.dart';
  11. void main() {
  12. runApp(const MyApp());
  13. }
  14. class MyApp extends StatelessWidget {
  15. const MyApp({Key? key}) : super(key: key);
  16. // This widget is the root of your application.
  17. @override
  18. Widget build(BuildContext context) {
  19. return MaterialApp(
  20. title: 'Flutter Demo',
  21. theme: ThemeData(
  22. // This is the theme of your application.
  23. //
  24. // Try running your application with "flutter run". You'll see the
  25. // application has a blue toolbar. Then, without quitting the app, try
  26. // changing the primarySwatch below to Colors.green and then invoke
  27. // "hot reload" (press "r" in the console where you ran "flutter run",
  28. // or simply save your changes to "hot reload" in a Flutter IDE).
  29. // Notice that the counter didn't reset back to zero; the application
  30. // is not restarted.
  31. primarySwatch: Colors.blue,
  32. ),
  33. home: const MyHomePage(title: 'FlowyEditor Example'),
  34. );
  35. }
  36. }
  37. class MyHomePage extends StatefulWidget {
  38. const MyHomePage({Key? key, required this.title}) : super(key: key);
  39. // This widget is the home page of your application. It is stateful, meaning
  40. // that it has a State object (defined below) that contains fields that affect
  41. // how it looks.
  42. // This class is the configuration for the state. It holds the values (in this
  43. // case the title) provided by the parent (in this case the App widget) and
  44. // used by the build method of the State. Fields in a Widget subclass are
  45. // always marked "final".
  46. final String title;
  47. @override
  48. State<MyHomePage> createState() => _MyHomePageState();
  49. }
  50. class _MyHomePageState extends State<MyHomePage> {
  51. final RenderPlugins renderPlugins = RenderPlugins();
  52. late EditorState _editorState;
  53. @override
  54. void initState() {
  55. super.initState();
  56. renderPlugins
  57. ..register('editor', EditorNodeWidgetBuilder.create)
  58. ..register('text', SelectedTextNodeBuilder.create)
  59. ..register('image', ImageNodeBuilder.create)
  60. ..register('text/with-checkbox', TextWithCheckBoxNodeBuilder.create)
  61. ..register('text/with-heading', TextWithHeadingNodeBuilder.create);
  62. }
  63. @override
  64. Widget build(BuildContext context) {
  65. return Scaffold(
  66. appBar: AppBar(
  67. // Here we take the value from the MyHomePage object that was created by
  68. // the App.build method, and use it to set our appbar title.
  69. title: Text(widget.title),
  70. ),
  71. body: FutureBuilder<String>(
  72. future: rootBundle.loadString('assets/document.json'),
  73. builder: (context, snapshot) {
  74. if (!snapshot.hasData) {
  75. return const Center(
  76. child: CircularProgressIndicator(),
  77. );
  78. } else {
  79. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  80. final document = StateTree.fromJson(data);
  81. _editorState = EditorState(
  82. document: document,
  83. renderPlugins: renderPlugins,
  84. );
  85. return FlowyEditor(
  86. editorState: _editorState,
  87. keyEventHandler: const [],
  88. shortcuts: [
  89. // TODO: this won't work, just a example for now.
  90. {
  91. 'h1': (editorState, eventName) {
  92. debugPrint('shortcut => $eventName');
  93. final selectedNodes = editorState.selectedNodes;
  94. if (selectedNodes.isEmpty) {
  95. return;
  96. }
  97. final textNode = selectedNodes.first as TextNode;
  98. TransactionBuilder(editorState)
  99. ..formatText(textNode, 0, textNode.toRawString().length, {
  100. 'heading': 'h1',
  101. })
  102. ..commit();
  103. }
  104. },
  105. {
  106. 'bold': (editorState, eventName) =>
  107. debugPrint('shortcut => $eventName')
  108. },
  109. {
  110. 'underline': (editorState, eventName) =>
  111. debugPrint('shortcut => $eventName')
  112. },
  113. ],
  114. );
  115. }
  116. },
  117. ),
  118. );
  119. }
  120. }