main.dart 3.3 KB

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