Fix for errors on a non scrollable editor (580) (#620)

pull/627/head
Nicolas Dion-Bouchard 3 years ago committed by GitHub
parent 2f8c8fed36
commit 111a249462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      lib/src/widgets/editor.dart
  2. 5
      lib/src/widgets/raw_editor.dart
  3. 23
      lib/src/widgets/raw_editor/raw_editor_state_selection_delegate_mixin.dart

@ -721,6 +721,7 @@ class RenderEditor extends RenderEditableContainerBox
required TextDirection textDirection, required TextDirection textDirection,
required bool hasFocus, required bool hasFocus,
required this.selection, required this.selection,
required this.scrollable,
required LayerLink startHandleLayerLink, required LayerLink startHandleLayerLink,
required LayerLink endHandleLayerLink, required LayerLink endHandleLayerLink,
required EdgeInsetsGeometry padding, required EdgeInsetsGeometry padding,
@ -750,6 +751,7 @@ class RenderEditor extends RenderEditableContainerBox
final CursorCont _cursorController; final CursorCont _cursorController;
final bool floatingCursorDisabled; final bool floatingCursorDisabled;
final bool scrollable;
Document document; Document document;
TextSelection selection; TextSelection selection;
@ -1125,17 +1127,18 @@ class RenderEditor extends RenderEditableContainerBox
@override @override
void performLayout() { void performLayout() {
assert(() { assert(() {
if (!constraints.hasBoundedHeight) return true; if (!scrollable || !constraints.hasBoundedHeight) return true;
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('RenderEditableContainerBox must have ' ErrorSummary('RenderEditableContainerBox must have '
'unlimited space along its main axis.'), 'unlimited space along its main axis when it is scrollable.'),
ErrorDescription('RenderEditableContainerBox does not clip or' ErrorDescription('RenderEditableContainerBox does not clip or'
' resize its children, so it must be ' ' resize its children, so it must be '
'placed in a parent that does not constrain the main ' 'placed in a parent that does not constrain the main '
'axis.'), 'axis.'),
ErrorHint( ErrorHint(
'You probably want to put the RenderEditableContainerBox inside a ' 'You probably want to put the RenderEditableContainerBox inside a '
'RenderViewport with a matching main axis.') 'RenderViewport with a matching main axis or disable the '
'scrollable property.')
]); ]);
}()); }());
assert(() { assert(() {

@ -295,6 +295,7 @@ class RawEditorState extends EditorState
document: _doc, document: _doc,
selection: widget.controller.selection, selection: widget.controller.selection,
hasFocus: _hasFocus, hasFocus: _hasFocus,
scrollable: widget.scrollable,
cursorController: _cursorCont, cursorController: _cursorCont,
textDirection: _textDirection, textDirection: _textDirection,
startHandleLayerLink: _startHandleLayerLink, startHandleLayerLink: _startHandleLayerLink,
@ -333,6 +334,7 @@ class RawEditorState extends EditorState
document: _doc, document: _doc,
selection: widget.controller.selection, selection: widget.controller.selection,
hasFocus: _hasFocus, hasFocus: _hasFocus,
scrollable: widget.scrollable,
textDirection: _textDirection, textDirection: _textDirection,
startHandleLayerLink: _startHandleLayerLink, startHandleLayerLink: _startHandleLayerLink,
endHandleLayerLink: _endHandleLayerLink, endHandleLayerLink: _endHandleLayerLink,
@ -934,6 +936,7 @@ class _Editor extends MultiChildRenderObjectWidget {
required this.document, required this.document,
required this.textDirection, required this.textDirection,
required this.hasFocus, required this.hasFocus,
required this.scrollable,
required this.selection, required this.selection,
required this.startHandleLayerLink, required this.startHandleLayerLink,
required this.endHandleLayerLink, required this.endHandleLayerLink,
@ -951,6 +954,7 @@ class _Editor extends MultiChildRenderObjectWidget {
final Document document; final Document document;
final TextDirection textDirection; final TextDirection textDirection;
final bool hasFocus; final bool hasFocus;
final bool scrollable;
final TextSelection selection; final TextSelection selection;
final LayerLink startHandleLayerLink; final LayerLink startHandleLayerLink;
final LayerLink endHandleLayerLink; final LayerLink endHandleLayerLink;
@ -969,6 +973,7 @@ class _Editor extends MultiChildRenderObjectWidget {
document: document, document: document,
textDirection: textDirection, textDirection: textDirection,
hasFocus: hasFocus, hasFocus: hasFocus,
scrollable: scrollable,
selection: selection, selection: selection,
startHandleLayerLink: startHandleLayerLink, startHandleLayerLink: startHandleLayerLink,
endHandleLayerLink: endHandleLayerLink, endHandleLayerLink: endHandleLayerLink,

@ -63,7 +63,9 @@ mixin RawEditorStateSelectionDelegateMixin on EditorState
final localRect = renderEditor.getLocalRectForCaret(position); final localRect = renderEditor.getLocalRectForCaret(position);
final targetOffset = _getOffsetToRevealCaret(localRect, position); final targetOffset = _getOffsetToRevealCaret(localRect, position);
scrollController.jumpTo(targetOffset.offset); if (scrollController.hasClients) {
scrollController.jumpTo(targetOffset.offset);
}
renderEditor.showOnScreen(rect: targetOffset.rect); renderEditor.showOnScreen(rect: targetOffset.rect);
} }
@ -77,7 +79,9 @@ mixin RawEditorStateSelectionDelegateMixin on EditorState
// `renderEditable.preferredLineHeight`, before the target scroll offset is // `renderEditable.preferredLineHeight`, before the target scroll offset is
// calculated. // calculated.
RevealedOffset _getOffsetToRevealCaret(Rect rect, TextPosition position) { RevealedOffset _getOffsetToRevealCaret(Rect rect, TextPosition position) {
if (!scrollController.position.allowImplicitScrolling) { // Make sure scrollController is attached
if (scrollController.hasClients &&
!scrollController.position.allowImplicitScrolling) {
return RevealedOffset(offset: scrollController.offset, rect: rect); return RevealedOffset(offset: scrollController.offset, rect: rect);
} }
@ -102,12 +106,17 @@ mixin RawEditorStateSelectionDelegateMixin on EditorState
// No overscrolling when encountering tall fonts/scripts that extend past // No overscrolling when encountering tall fonts/scripts that extend past
// the ascent. // the ascent.
final targetOffset = (additionalOffset + scrollController.offset).clamp( var targetOffset = additionalOffset;
scrollController.position.minScrollExtent, if (scrollController.hasClients) {
scrollController.position.maxScrollExtent, targetOffset = (additionalOffset + scrollController.offset).clamp(
); scrollController.position.minScrollExtent,
scrollController.position.maxScrollExtent,
);
}
final offsetDelta = scrollController.offset - targetOffset; final offsetDelta =
(scrollController.hasClients ? scrollController.offset : 0) -
targetOffset;
return RevealedOffset( return RevealedOffset(
rect: rect.shift(unitOffset * offsetDelta), offset: targetOffset); rect: rect.shift(unitOffset * offsetDelta), offset: targetOffset);
} }

Loading…
Cancel
Save