1
1
import 'package:example/demos/example_editor/_example_document.dart' ;
2
- import 'package:example/demos/example_editor/example_editor.dart' ;
3
2
import 'package:flutter/material.dart' ;
4
3
import 'package:flutter_gen/gen_l10n/app_localizations.dart' ;
5
4
import 'package:flutter_localizations/flutter_localizations.dart' ;
@@ -31,7 +30,7 @@ void main() {
31
30
runApp (
32
31
MaterialApp (
33
32
home: Scaffold (
34
- body: ExampleEditor (),
33
+ body: _Demo (),
35
34
),
36
35
supportedLocales: const [
37
36
Locale ('en' , '' ),
@@ -48,8 +47,164 @@ void main() {
48
47
);
49
48
}
50
49
50
+ class _Demo extends StatefulWidget {
51
+ const _Demo ();
52
+
53
+ @override
54
+ State <_Demo > createState () => _DemoState ();
55
+ }
56
+
57
+ class _DemoState extends State <_Demo > {
58
+ late MutableDocument _document;
59
+ late MutableDocumentComposer _composer;
60
+ late Editor _docEditor;
61
+
62
+ @override
63
+ void initState () {
64
+ super .initState ();
65
+ _document = createInitialDocument ();
66
+ _composer = MutableDocumentComposer ();
67
+ _docEditor = createDefaultDocumentEditor (document: _document, composer: _composer);
68
+ }
69
+
70
+ @override
71
+ void dispose () {
72
+ _composer.dispose ();
73
+ super .dispose ();
74
+ }
75
+
76
+ @override
77
+ Widget build (BuildContext context) {
78
+ return Row (
79
+ children: [
80
+ Expanded (
81
+ child: _StandardEditor (
82
+ document: _document,
83
+ composer: _composer,
84
+ editor: _docEditor,
85
+ ),
86
+ ),
87
+ _buildToolbar (),
88
+ ],
89
+ );
90
+ }
91
+
92
+ Widget _buildToolbar () {
93
+ return Row (
94
+ mainAxisSize: MainAxisSize .min,
95
+ children: [
96
+ _EditorHistoryPanel (editor: _docEditor),
97
+ Container (
98
+ width: 24 ,
99
+ height: double .infinity,
100
+ color: const Color (0xFF2F2F2F ),
101
+ child: Column (),
102
+ ),
103
+ ],
104
+ );
105
+ }
106
+ }
107
+
108
+ class _EditorHistoryPanel extends StatefulWidget {
109
+ const _EditorHistoryPanel ({
110
+ required this .editor,
111
+ });
112
+
113
+ final Editor editor;
114
+
115
+ @override
116
+ State <_EditorHistoryPanel > createState () => _EditorHistoryPanelState ();
117
+ }
118
+
119
+ class _EditorHistoryPanelState extends State <_EditorHistoryPanel > {
120
+ final _scrollController = ScrollController ();
121
+ late EditListener _editListener;
122
+
123
+ @override
124
+ void initState () {
125
+ super .initState ();
126
+
127
+ _editListener = FunctionalEditListener (_onEditorChange);
128
+ widget.editor.addListener (_editListener);
129
+ }
130
+
131
+ @override
132
+ void didUpdateWidget (_EditorHistoryPanel oldWidget) {
133
+ super .didUpdateWidget (oldWidget);
134
+
135
+ if (widget.editor != oldWidget.editor) {
136
+ oldWidget.editor.removeListener (_editListener);
137
+ widget.editor.addListener (_editListener);
138
+ }
139
+ }
140
+
141
+ @override
142
+ void dispose () {
143
+ _scrollController.dispose ();
144
+ widget.editor.removeListener (_editListener);
145
+ super .dispose ();
146
+ }
147
+
148
+ void _onEditorChange (changes) {
149
+ setState (() {
150
+ // Build the latest list of changes.
151
+ });
152
+
153
+ // Always scroll to bottom of transaction list.
154
+ WidgetsBinding .instance.addPostFrameCallback ((_) {
155
+ _scrollController.position.jumpTo (_scrollController.position.maxScrollExtent);
156
+ });
157
+ }
158
+
159
+ @override
160
+ Widget build (BuildContext context) {
161
+ return Theme (
162
+ data: ThemeData (
163
+ brightness: Brightness .dark,
164
+ ),
165
+ child: Container (
166
+ width: 300 ,
167
+ height: double .infinity,
168
+ color: const Color (0xFF333333 ),
169
+ child: SingleChildScrollView (
170
+ controller: _scrollController,
171
+ child: Padding (
172
+ padding: const EdgeInsets .symmetric (vertical: 24.0 ),
173
+ child: Column (
174
+ children: [
175
+ for (final history in widget.editor.history)
176
+ ListTile (
177
+ title: Text ("${history .changes .length } changes" ),
178
+ titleTextStyle: TextStyle (
179
+ fontSize: 16 ,
180
+ ),
181
+ subtitle: Text ("${history .changes .map ((event ) => event .describe ()).join ("\n " )}" ),
182
+ subtitleTextStyle: TextStyle (
183
+ color: Colors .white.withOpacity (0.5 ),
184
+ fontSize: 10 ,
185
+ height: 1.4 ,
186
+ ),
187
+ visualDensity: VisualDensity .compact,
188
+ ),
189
+ ],
190
+ ),
191
+ ),
192
+ ),
193
+ ),
194
+ );
195
+ }
196
+ }
197
+
51
198
class _StandardEditor extends StatefulWidget {
52
- const _StandardEditor ();
199
+ const _StandardEditor ({
200
+ required this .document,
201
+ required this .composer,
202
+ required this .editor,
203
+ });
204
+
205
+ final MutableDocument document;
206
+ final MutableDocumentComposer composer;
207
+ final Editor editor;
53
208
54
209
@override
55
210
State <_StandardEditor > createState () => _StandardEditorState ();
@@ -58,20 +213,13 @@ class _StandardEditor extends StatefulWidget {
58
213
class _StandardEditorState extends State <_StandardEditor > {
59
214
final GlobalKey _docLayoutKey = GlobalKey ();
60
215
61
- late MutableDocument _doc;
62
- late MutableDocumentComposer _composer;
63
- late Editor _docEditor;
64
-
65
216
late FocusNode _editorFocusNode;
66
217
67
218
late ScrollController _scrollController;
68
219
69
220
@override
70
221
void initState () {
71
222
super .initState ();
72
- _doc = createInitialDocument ();
73
- _composer = MutableDocumentComposer ();
74
- _docEditor = createDefaultDocumentEditor (document: _doc, composer: _composer);
75
223
_editorFocusNode = FocusNode ();
76
224
_scrollController = ScrollController ();
77
225
}
@@ -80,19 +228,27 @@ class _StandardEditorState extends State<_StandardEditor> {
80
228
void dispose () {
81
229
_scrollController.dispose ();
82
230
_editorFocusNode.dispose ();
83
- _composer.dispose ();
84
231
super .dispose ();
85
232
}
86
233
87
234
@override
88
235
Widget build (BuildContext context) {
89
236
return SuperEditor (
90
- editor: _docEditor ,
91
- document: _doc ,
92
- composer: _composer ,
237
+ editor: widget.editor ,
238
+ document: widget.document ,
239
+ composer: widget.composer ,
93
240
focusNode: _editorFocusNode,
94
241
scrollController: _scrollController,
95
242
documentLayoutKey: _docLayoutKey,
243
+ stylesheet: defaultStylesheet.copyWith (
244
+ addRulesAfter: [
245
+ taskStyles,
246
+ ],
247
+ ),
248
+ componentBuilders: [
249
+ TaskComponentBuilder (widget.editor),
250
+ ...defaultComponentBuilders,
251
+ ],
96
252
);
97
253
}
98
254
}
0 commit comments