main.dart 3.5 KB

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