Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

## [0.4.0] #12

Merged
merged 3 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.4.0]

* fix issue about WidgetSpan [https://github.com/fluttercandies/extended_text_field/issues/11]
* fix wrong caret offset

## [0.3.9]

* improve codes base on v1.7.8
Expand Down
30 changes: 15 additions & 15 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,37 @@ packages:
extended_image:
dependency: "direct main"
description:
path: "E:\\Flutter\\FlutterCandies\\dev\\extended_image"
relative: false
source: path
version: "0.5.2"
name: extended_image
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.5.3"
extended_image_library:
dependency: transitive
description:
path: "E:\\Flutter\\FlutterCandies\\dev\\extended_image_library"
relative: false
source: path
name: extended_image_library
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.4"
extended_text:
dependency: "direct main"
description:
path: "E:\\Flutter\\FlutterCandies\\dev\\extended_text"
relative: false
source: path
version: "0.5.9"
name: extended_text
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.6.1"
extended_text_field:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "0.3.8"
version: "0.3.9"
extended_text_library:
dependency: transitive
description:
path: "E:\\Flutter\\FlutterCandies\\dev\\extended_text_library"
relative: false
source: path
name: extended_text_library
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.4.2"
flutter:
dependency: "direct main"
Expand Down
79 changes: 26 additions & 53 deletions lib/src/extended_render_editable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,12 @@ class ExtendedRenderEditable extends RenderBox

final Rect visibleRegion = Offset(0.0, _visibleRegionMinY) & size;

final Offset startOffset = _textPainter.getOffsetForCaret(
TextPosition(offset: selection.start, affinity: selection.affinity),
Rect.zero,
);
final Offset startOffset = _getCaretOffset(
TextPosition(
offset: selection.start,
affinity: selection.affinity,
),
effectiveOffset: effectiveOffset);

// TODO(justinmc): https://github.com/flutter/flutter/issues/31495
// Check if the selection is visible with an approximation because a
Expand All @@ -298,23 +300,9 @@ class ExtendedRenderEditable extends RenderBox
.inflate(visibleRegionSlop)
.contains(startOffset + effectiveOffset);

Offset endOffset = _textPainter.getOffsetForCaret(
TextPosition(offset: selection.end, affinity: selection.affinity),
Rect.zero,
);

if (handleSpecialText &&
selection.end > 0 &&
endOffset == Offset.zero &&
effectiveOffset == Offset.zero) {
var boxs = _textPainter.getBoxesForSelection(TextSelection(
baseOffset: selection.end - 1,
extentOffset: selection.end,
affinity: selection.affinity));
if (boxs.length > 0) {
endOffset = boxs.toList().last.toRect().topRight;
}
}
final Offset endOffset = _getCaretOffset(
TextPosition(offset: selection.end, affinity: selection.affinity),
effectiveOffset: effectiveOffset);

_selectionEndInViewport.value = visibleRegion
.inflate(visibleRegionSlop)
Expand Down Expand Up @@ -1181,8 +1169,7 @@ class ExtendedRenderEditable extends RenderBox

//final Offset paintOffset = _paintOffset;
///zmt
final Offset effectiveOffset =
(_initialOffset ?? Offset.zero) + _paintOffset;
final Offset effectiveOffset = _effectiveOffset;

TextSelection textPainterSelection = selection;
if (handleSpecialText) {
Expand All @@ -1197,13 +1184,11 @@ class ExtendedRenderEditable extends RenderBox
caretHeight = value;
};
final Offset caretOffset = _getCaretOffset(
effectiveOffset,
TextPosition(
offset: textPainterSelection.extentOffset,
affinity: selection.affinity),
TextPosition(
offset: selection.extentOffset, affinity: selection.affinity),
caretHeightCallBack: caretHeightCallBack);
caretHeightCallBack: caretHeightCallBack,
effectiveOffset: effectiveOffset);

final Offset start =
Offset(0.0, caretHeight ?? preferredLineHeight) + caretOffset;
Expand Down Expand Up @@ -1659,9 +1644,9 @@ class ExtendedRenderEditable extends RenderBox
ValueChanged<double> caretHeightCallBack = (value) {
caretHeight = value;
};
final Offset caretOffset = _getCaretOffset(
effectiveOffset, textPosition, textInputPosition,
caretHeightCallBack: caretHeightCallBack);
final Offset caretOffset = _getCaretOffset(textPosition,
caretHeightCallBack: caretHeightCallBack,
effectiveOffset: effectiveOffset);

Rect caretRect = _caretPrototype.shift(caretOffset);
if (_cursorOffset != null) caretRect = caretRect.shift(_cursorOffset);
Expand Down Expand Up @@ -1718,35 +1703,20 @@ class ExtendedRenderEditable extends RenderBox
}
}

Offset _getCaretOffset(Offset effectiveOffset, TextPosition textPosition,
TextPosition textInputPosition,
{ValueChanged<double> caretHeightCallBack}) {
Offset _getCaretOffset(TextPosition textPosition,
{ValueChanged<double> caretHeightCallBack, Offset effectiveOffset}) {
effectiveOffset ??= this._effectiveOffset;

///zmt
if (handleSpecialText) {
var textSpan = text.getSpanForPosition(TextPosition(
offset: textPosition.offset, affinity: TextAffinity.upstream));
if (textSpan != null && textSpan is ExtendedWidgetSpan) {
int index = _placeholderSpans.indexOf(textSpan);
if (index >= 0 && index < _textPainter.inlinePlaceholderBoxes.length) {
var box = _textPainter.inlinePlaceholderBoxes[index];
var rect = box.toRect();
if (textInputPosition.offset >= textSpan.end) {
caretHeightCallBack?.call(rect.height);
return rect.topRight;
} else if (textInputPosition.offset >= textSpan.start) {
caretHeightCallBack?.call(rect.height);
return rect.topLeft;
}
}
}

///if first index, check by first span
var offset = textPosition.offset;
if (offset == 0) {
offset = 1;
}

///last or has ExtendedWidgetSpan

var boxs = _textPainter.getBoxesForSelection(TextSelection(
baseOffset: offset - 1,
extentOffset: offset,
Expand All @@ -1755,9 +1725,9 @@ class ExtendedRenderEditable extends RenderBox
var rect = boxs.toList().last.toRect();
caretHeightCallBack?.call(rect.height);
if (textPosition.offset == 0) {
return rect.topLeft;
return rect.topLeft + effectiveOffset;
} else {
return rect.topRight;
return rect.topRight + effectiveOffset;
}
}
}
Expand Down Expand Up @@ -1932,7 +1902,7 @@ class ExtendedRenderEditable extends RenderBox
final double scale = textParentData.scale;
context.pushTransform(
needsCompositing,
offset + textParentData.offset,
effectiveOffset + textParentData.offset,
Matrix4.diagonal3Values(scale, scale, scale),
(PaintingContext context, Offset offset) {
context.paintChild(
Expand Down Expand Up @@ -1983,10 +1953,13 @@ class ExtendedRenderEditable extends RenderBox
}

Offset _initialOffset;
Offset get _effectiveOffset => (_initialOffset ?? Offset.zero) + _paintOffset;

@override
void paint(PaintingContext context, Offset offset) {
///zmt
_initialOffset = offset;

_layoutText(constraints.maxWidth);
if (_hasVisualOverflow)
context.pushClipRect(
Expand Down
2 changes: 0 additions & 2 deletions lib/src/extended_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

import 'extended_text_selection.dart';

/// Signature for the [ExtendedTextField.buildCounter] callback.
typedef InputCounterWidgetBuilder = Widget Function(
/// The build context for the TextField
Expand Down
6 changes: 3 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ packages:
extended_text_library:
dependency: "direct main"
description:
path: "E:\\Flutter\\FlutterCandies\\dev\\extended_text_library"
relative: false
source: path
name: extended_text_library
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.4.2"
flutter:
dependency: "direct main"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: extended_text_field
description: extended official text field to quickly build special text like inline image, @somebody, custom background etc.
version: 0.3.9
version: 0.4.0
author: zmtzawqlp <[email protected]>
homepage: https://github.com/fluttercandies/extended_text_field

Expand Down