main.dart 3.3 KB

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