main.dart 2.9 KB

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