zefyr

editor.dart 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
  2. // for details. All rights reserved. Use of this source code is governed by a
  3. // BSD-style license that can be found in the LICENSE file.
  4. import 'package:flutter/widgets.dart';
  5. import 'controller.dart';
  6. import 'editable_text.dart';
  7. import 'image.dart';
  8. import 'scaffold.dart';
  9. import 'scope.dart';
  10. import 'theme.dart';
  11. import 'toolbar.dart';
  12. /// Widget for editing Zefyr documents.
  13. class ZefyrEditor extends StatefulWidget {
  14. const ZefyrEditor({
  15. Key key,
  16. @required this.controller,
  17. @required this.focusNode,
  18. this.autofocus: true,
  19. this.enabled: true,
  20. this.padding: const EdgeInsets.symmetric(horizontal: 16.0),
  21. this.toolbarDelegate,
  22. this.imageDelegate,
  23. this.physics,
  24. }) : super(key: key);
  25. final ZefyrController controller;
  26. final FocusNode focusNode;
  27. final bool autofocus;
  28. final bool enabled;
  29. final ZefyrToolbarDelegate toolbarDelegate;
  30. final ZefyrImageDelegate imageDelegate;
  31. final ScrollPhysics physics;
  32. /// Padding around editable area.
  33. final EdgeInsets padding;
  34. @override
  35. _ZefyrEditorState createState() => new _ZefyrEditorState();
  36. }
  37. class _ZefyrEditorState extends State<ZefyrEditor> {
  38. ZefyrImageDelegate _imageDelegate;
  39. ZefyrScope _scope;
  40. ZefyrThemeData _themeData;
  41. GlobalKey<ZefyrToolbarState> _toolbarKey;
  42. ZefyrScaffoldState _scaffold;
  43. bool get hasToolbar => _toolbarKey != null;
  44. void showToolbar() {
  45. assert(_toolbarKey == null);
  46. _toolbarKey = GlobalKey();
  47. _scaffold.showToolbar(buildToolbar);
  48. }
  49. void hideToolbar() {
  50. if (_toolbarKey == null) return;
  51. _scaffold.hideToolbar();
  52. _toolbarKey = null;
  53. }
  54. Widget buildToolbar(BuildContext context) {
  55. return ZefyrTheme(
  56. data: _themeData,
  57. child: ZefyrToolbar(
  58. key: _toolbarKey,
  59. editor: _scope,
  60. delegate: widget.toolbarDelegate,
  61. ),
  62. );
  63. }
  64. void _handleChange() {
  65. if (_scope.focusOwner == FocusOwner.none) {
  66. hideToolbar();
  67. } else if (!hasToolbar) {
  68. showToolbar();
  69. } else {
  70. // TODO: is there a nicer way to do this?
  71. WidgetsBinding.instance.addPostFrameCallback((_) {
  72. _toolbarKey?.currentState?.markNeedsRebuild();
  73. });
  74. }
  75. }
  76. @override
  77. void initState() {
  78. super.initState();
  79. _imageDelegate = widget.imageDelegate ?? new ZefyrDefaultImageDelegate();
  80. }
  81. @override
  82. void didUpdateWidget(ZefyrEditor oldWidget) {
  83. super.didUpdateWidget(oldWidget);
  84. _scope.controller = widget.controller;
  85. _scope.focusNode = widget.focusNode;
  86. if (widget.imageDelegate != oldWidget.imageDelegate) {
  87. _imageDelegate = widget.imageDelegate ?? new ZefyrDefaultImageDelegate();
  88. _scope.imageDelegate = _imageDelegate;
  89. }
  90. }
  91. @override
  92. void didChangeDependencies() {
  93. super.didChangeDependencies();
  94. final parentTheme = ZefyrTheme.of(context, nullOk: true);
  95. final fallbackTheme = ZefyrThemeData.fallback(context);
  96. _themeData = (parentTheme != null)
  97. ? fallbackTheme.merge(parentTheme)
  98. : fallbackTheme;
  99. if (_scope == null) {
  100. _scope = ZefyrScope.editable(
  101. imageDelegate: _imageDelegate,
  102. controller: widget.controller,
  103. focusNode: widget.focusNode,
  104. focusScope: FocusScope.of(context),
  105. );
  106. _scope.addListener(_handleChange);
  107. } else {
  108. final focusScope = FocusScope.of(context);
  109. _scope.focusScope = focusScope;
  110. }
  111. final scaffold = ZefyrScaffold.of(context);
  112. if (_scaffold != scaffold) {
  113. bool didHaveToolbar = hasToolbar;
  114. hideToolbar();
  115. _scaffold = scaffold;
  116. if (didHaveToolbar) showToolbar();
  117. }
  118. }
  119. @override
  120. void dispose() {
  121. hideToolbar();
  122. _scope.removeListener(_handleChange);
  123. _scope.dispose();
  124. super.dispose();
  125. }
  126. @override
  127. Widget build(BuildContext context) {
  128. Widget editable = new ZefyrEditableText(
  129. controller: _scope.controller,
  130. focusNode: _scope.focusNode,
  131. imageDelegate: _scope.imageDelegate,
  132. autofocus: widget.autofocus,
  133. enabled: widget.enabled,
  134. padding: widget.padding,
  135. physics: widget.physics,
  136. );
  137. return ZefyrTheme(
  138. data: _themeData,
  139. child: ZefyrScopeAccess(
  140. scope: _scope,
  141. child: editable,
  142. ),
  143. );
  144. }
  145. }