Skip to content

Commit 23888d0

Browse files
committed
Refactoring TextCursor.
1 parent 385cf61 commit 23888d0

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

patterns/state/manipulator_state/states/selections/text/text_cursor.dart

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import 'dart:ui';
33
import '../../../shapes/text_shape.dart';
44

55
class TextCursor {
6-
TextCursor(this._shape);
6+
TextCursor(this._textShape) : _xPosition = _textShape.x;
77

88
double get xCoordinate => _xPosition;
99

1010
void changePosition(double x) {
11-
x = x - _shape.x;
11+
x = x - _textShape.x;
1212

13-
final pos = _shape.paragraph.getPositionForOffset(Offset(x, _shape.y));
13+
final pos = _textShape.paragraph.getPositionForOffset(Offset(x, _textShape.y));
1414

1515
_charIndex = pos.offset;
16-
_xPosition = _shape.x;
16+
_xPosition = _textShape.x;
1717

18-
final range = _shape.paragraph.getBoxesForRange(
18+
final range = _textShape.paragraph.getBoxesForRange(
1919
pos.offset - 1,
2020
pos.offset,
2121
);
@@ -41,14 +41,14 @@ class TextCursor {
4141

4242
void moveLeft() {
4343
_charIndex--;
44-
_xPosition = _shape.x;
44+
_xPosition = _textShape.x;
4545

4646
if (_charIndex <= 0) {
4747
_charIndex = 0;
4848
return;
4949
}
5050

51-
final range = _shape.paragraph.getBoxesForRange(_charIndex - 1, _charIndex);
51+
final range = _textShape.paragraph.getBoxesForRange(_charIndex - 1, _charIndex);
5252

5353
if (range.isNotEmpty) {
5454
_xPosition += range.first.right;
@@ -57,30 +57,30 @@ class TextCursor {
5757

5858
void moveRight() {
5959
_charIndex++;
60-
_xPosition = _shape.x;
60+
_xPosition = _textShape.x;
6161

62-
if (_charIndex >= _shape.text.length) {
63-
_charIndex = _shape.text.length;
64-
_xPosition += _shape.width;
62+
if (_charIndex >= _textShape.text.length) {
63+
_charIndex = _textShape.text.length;
64+
_xPosition += _textShape.width;
6565
return;
6666
}
6767

68-
final range = _shape.paragraph.getBoxesForRange(_charIndex - 1, _charIndex);
68+
final range = _textShape.paragraph.getBoxesForRange(_charIndex - 1, _charIndex);
6969

7070
if (range.isNotEmpty) {
7171
_xPosition += range.first.right;
7272
}
7373
}
7474

7575
void _changeText({String char = '', int removeChars = 0}) {
76-
final start = _shape.text.substring(0, _charIndex + removeChars);
77-
final end = _shape.text.length > start.length
78-
? _shape.text.substring(_charIndex)
76+
final start = _textShape.text.substring(0, _charIndex + removeChars);
77+
final end = _textShape.text.length > start.length
78+
? _textShape.text.substring(_charIndex)
7979
: '';
80-
_shape.text = '$start$char$end';
80+
_textShape.text = '$start$char$end';
8181
}
8282

83-
final TextShape _shape;
83+
final TextShape _textShape;
8484
int _charIndex = 0;
85-
double _xPosition = 0;
85+
late double _xPosition = 0;
8686
}

0 commit comments

Comments
 (0)