main.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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: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: 'Flutter Demo Home Page'),
  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. final RenderPlugins renderPlugins = RenderPlugins();
  48. @override
  49. void initState() {
  50. super.initState();
  51. renderPlugins
  52. ..register(
  53. 'text',
  54. TextNodeBuilder.create,
  55. )
  56. ..register(
  57. 'image',
  58. ImageNodeBuilder.create,
  59. );
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. return Scaffold(
  64. appBar: AppBar(
  65. // Here we take the value from the MyHomePage object that was created by
  66. // the App.build method, and use it to set our appbar title.
  67. title: Text(widget.title),
  68. ),
  69. body: FutureBuilder<String>(
  70. future: rootBundle.loadString('assets/document.json'),
  71. builder: (context, snapshot) {
  72. if (!snapshot.hasData) {
  73. return const Center(
  74. child: CircularProgressIndicator(),
  75. );
  76. } else {
  77. final data = Map<String, Object>.from(json.decode(snapshot.data!));
  78. final stateTree = StateTree.fromJson(data);
  79. return renderPlugins.buildWidget(
  80. NodeWidgetContext(
  81. buildContext: context,
  82. node: stateTree.root,
  83. ),
  84. );
  85. }
  86. },
  87. ),
  88. );
  89. }
  90. }