Explorar el Código

Moved editor toolbar to overlay

Anatoly Pulyaevskiy hace 6 años
padre
commit
c0237fc7d9

+ 1
- 0
packages/zefyr/lib/src/widgets/buttons.dart Ver fichero

@@ -424,6 +424,7 @@ class _LinkButtonState extends State<LinkButton> {
424 424
   }
425 425
 
426 426
   Widget buildOverlay(BuildContext context) {
427
+
427 428
     final toolbar = ZefyrToolbar.of(context);
428 429
     final style = toolbar.editor.selectionStyle;
429 430
 

+ 81
- 13
packages/zefyr/lib/src/widgets/editor.dart Ver fichero

@@ -184,6 +184,37 @@ class _ZefyrEditorState extends State<ZefyrEditor> {
184 184
   final FocusNode _toolbarFocusNode = new FocusNode();
185 185
   ZefyrImageDelegate _imageDelegate;
186 186
   ZefyrEditorScope _scope;
187
+  ZefyrThemeData _themeData;
188
+
189
+  OverlayEntry _toolbar;
190
+  OverlayState _overlay;
191
+
192
+  void showToolbar() {
193
+    _toolbar = new OverlayEntry(
194
+      builder: (context) => _ZefyrToolbarContainer(
195
+            theme: _themeData,
196
+            toolbar: ZefyrToolbar(
197
+              focusNode: _toolbarFocusNode,
198
+              editor: _scope,
199
+              delegate: widget.toolbarDelegate,
200
+            ),
201
+          ),
202
+    );
203
+    _overlay.insert(_toolbar);
204
+  }
205
+
206
+  void hideToolbar() {
207
+    _toolbar?.remove();
208
+    _toolbar = null;
209
+  }
210
+
211
+  void _handleChange() {
212
+    if (_scope.focusOwner == FocusOwner.none) {
213
+      hideToolbar();
214
+    } else if (_toolbar == null) {
215
+      showToolbar();
216
+    }
217
+  }
187 218
 
188 219
   @override
189 220
   void initState() {
@@ -195,6 +226,7 @@ class _ZefyrEditorState extends State<ZefyrEditor> {
195 226
       controller: widget.controller,
196 227
       focusNode: widget.focusNode,
197 228
     );
229
+    _scope.addListener(_handleChange);
198 230
   }
199 231
 
200 232
   @override
@@ -208,8 +240,28 @@ class _ZefyrEditorState extends State<ZefyrEditor> {
208 240
     }
209 241
   }
210 242
 
243
+  @override
244
+  void didChangeDependencies() {
245
+    super.didChangeDependencies();
246
+
247
+    final parentTheme = ZefyrTheme.of(context, nullOk: true);
248
+    final fallbackTheme = ZefyrThemeData.fallback(context);
249
+    _themeData = (parentTheme != null)
250
+        ? fallbackTheme.merge(parentTheme)
251
+        : fallbackTheme;
252
+
253
+    final overlay = Overlay.of(context, debugRequiredFor: widget);
254
+    if (_overlay != overlay) {
255
+      hideToolbar();
256
+      _overlay = overlay;
257
+      // TODO: update toolbar.
258
+    }
259
+  }
260
+
211 261
   @override
212 262
   void dispose() {
263
+    hideToolbar();
264
+    _scope.removeListener(_handleChange);
213 265
     _scope.dispose();
214 266
     _toolbarFocusNode.dispose();
215 267
     super.dispose();
@@ -228,21 +280,18 @@ class _ZefyrEditorState extends State<ZefyrEditor> {
228 280
 
229 281
     final children = <Widget>[];
230 282
     children.add(Expanded(child: editable));
231
-    final toolbar = ZefyrToolbar(
232
-      editor: _scope,
233
-      focusNode: _toolbarFocusNode,
234
-      delegate: widget.toolbarDelegate,
235
-    );
236
-    children.add(toolbar);
237
-
238
-    final parentTheme = ZefyrTheme.of(context, nullOk: true);
239
-    final fallbackTheme = ZefyrThemeData.fallback(context);
240
-    final actualTheme = (parentTheme != null)
241
-        ? fallbackTheme.merge(parentTheme)
242
-        : fallbackTheme;
283
+    if (_toolbar != null) {
284
+      children.add(SizedBox(height: ZefyrToolbar.kToolbarHeight));
285
+    }
286
+//    final toolbar = ZefyrToolbar(
287
+//      editor: _scope,
288
+//      focusNode: _toolbarFocusNode,
289
+//      delegate: widget.toolbarDelegate,
290
+//    );
291
+//    children.add(toolbar);
243 292
 
244 293
     return ZefyrTheme(
245
-      data: actualTheme,
294
+      data: _themeData,
246 295
       child: _ZefyrEditorScope(
247 296
         scope: _scope,
248 297
         child: Column(children: children),
@@ -250,3 +299,22 @@ class _ZefyrEditorState extends State<ZefyrEditor> {
250 299
     );
251 300
   }
252 301
 }
302
+
303
+class _ZefyrToolbarContainer extends StatelessWidget {
304
+  final ZefyrThemeData theme;
305
+  final Widget toolbar;
306
+
307
+  const _ZefyrToolbarContainer({Key key, this.theme, this.toolbar})
308
+      : super(key: key);
309
+
310
+  @override
311
+  Widget build(BuildContext context) {
312
+    final media = MediaQuery.of(context);
313
+    return Positioned(
314
+      bottom: media.viewInsets.bottom,
315
+      left: 0.0,
316
+      right: 0.0,
317
+      child: ZefyrTheme(data: theme, child: toolbar),
318
+    );
319
+  }
320
+}

+ 0
- 3
packages/zefyr/lib/src/widgets/selection.dart Ver fichero

@@ -82,7 +82,6 @@ class _ZefyrSelectionOverlayState extends State<ZefyrSelectionOverlay>
82 82
               editable: editable,
83 83
               controls: widget.controls,
84 84
               delegate: this,
85
-              visible: true,
86 85
             ),
87 86
           ),
88 87
     );
@@ -423,13 +422,11 @@ class _SelectionToolbar extends StatefulWidget {
423 422
     @required this.editable,
424 423
     @required this.controls,
425 424
     @required this.delegate,
426
-    @required this.visible,
427 425
   }) : super(key: key);
428 426
 
429 427
   final ZefyrEditableTextScope editable;
430 428
   final TextSelectionControls controls;
431 429
   final TextSelectionDelegate delegate;
432
-  final bool visible;
433 430
 
434 431
   @override
435 432
   _SelectionToolbarState createState() => new _SelectionToolbarState();