Overview
Selection represents a range of selected text in the editor, while Position represents a single point (cursor location).
Selections are directional:
- Forward: End position is after start position
- Backward: End position is before start position
- Collapsed: Start and end are the same (cursor)
Selection
Constructor
Selection({
required Position start,
required Position end,
})
Named Constructors
// Create a selection within a single node
Selection.single({
required Path path,
required int startOffset,
int? endOffset,
})
// Create a collapsed selection (cursor)
Selection.collapsed(Position position)
// Create from JSON
Selection.fromJson(Map<String, dynamic> json)
// Create an invalid selection
Selection.invalid()
Parameters
The starting position of the selection.
The ending position of the selection.
Path to the node (for Selection.single).
Starting character offset.
Ending character offset. If omitted in Selection.single, creates a collapsed selection.
Properties
Whether start and end are at the same position (cursor).
Whether start and end are in the same node.
Whether the selection is forward (end after start).
Whether the selection is backward (start after end).
Returns a forward-direction version of this selection.
Returns the selection with start and end swapped.
The starting offset in normalized form.
The ending offset in normalized form.
The length of the selection (endIndex - startIndex).
Methods
collapse
Selection collapse({bool atStart = false})
Collapses the selection to a single point.
If true, collapse to start position. If false, collapse to end position.
Returns: A collapsed selection.
Example:
final collapsed = selection.collapse(atStart: true);
copyWith
Selection copyWith({Position? start, Position? end})
Creates a copy with optional position overrides.
shift
Selection shift(int offset)
Shifts both start and end positions by the given offset.
toJson
Map<String, dynamic> toJson()
Serializes the selection to JSON.
Example
import 'package:appflowy_editor/appflowy_editor.dart';
// Create a cursor position
final cursor = Selection.collapsed(
Position(path: [0], offset: 5),
);
// Create a single-node selection
final selection = Selection.single(
path: [0],
startOffset: 0,
endOffset: 5,
);
// Create a multi-node selection
final multiSelection = Selection(
start: Position(path: [0], offset: 10),
end: Position(path: [2], offset: 5),
);
// Check selection properties
if (selection.isCollapsed) {
print('This is a cursor');
}
if (selection.isSingle) {
print('Selection is within one node');
}
print('Selection length: ${selection.length}');
// Normalize a backward selection
final normalized = selection.normalized;
// Collapse to start
final collapsed = selection.collapse(atStart: true);
// Serialize to JSON
final json = selection.toJson();
final fromJson = Selection.fromJson(json);
Position
Constructor
Position({
required Path path,
int offset = 0,
})
Named Constructors
// Create an invalid position
Position.invalid()
// Create from JSON
Position.fromJson(Map<String, dynamic> json)
Parameters
Path to the node (e.g., [0], [1, 2]).
Character offset within the node.
Properties
Methods
copyWith
Position copyWith({Path? path, int? offset})
Creates a copy with optional overrides.
toJson
Map<String, dynamic> toJson()
Serializes the position to JSON.
Example
// Create a position at the start of the first node
final position = Position(path: [0], offset: 0);
// Create a position at offset 10
final position2 = Position(path: [1, 2], offset: 10);
// Copy with modified offset
final newPosition = position.copyWith(offset: 5);
// Serialize
final json = position.toJson();
final fromJson = Position.fromJson(json);
print('Position: path=${position.path}, offset=${position.offset}');
Path Type
A Path is a list of integers representing the location of a node in the document tree:
typedef Path = List<int>;
// Examples:
[0] // First node at root level
[1] // Second node at root level
[0, 0] // First child of first node
[1, 2] // Third child of second node
See Also