TextCursorTracker class
Tracks text cursor (caret) position in a TextField or similar widget.
Provides a simple API to get the exact pixel position of the text cursor, useful for positioning menus, tooltips, or other UI relative to where the user is typing.
Usage
class _MyWidgetState extends State<MyWidget> {
final _textController = TextEditingController();
final _cursorTracker = TextCursorTracker();
void _onSlashTyped() {
// Get cursor rect in local coordinates
final cursorRect = _cursorTracker.cursorRect;
if (cursorRect != null) {
// Position a menu below the cursor
showMenu(at: cursorRect.bottomLeft);
}
}
@override
Widget build(BuildContext context) {
return TextField(
key: _cursorTracker.key, // Attach the tracker's key
controller: _textController,
);
}
}
For Palettes
When used inside a palette, combine with PaletteSelf to get screen coordinates:
final localRect = _cursorTracker.cursorRect;
if (localRect != null) {
final screenPos = await PaletteSelf.localToScreen(localRect.bottomLeft);
PaletteMessenger.send('show-menu', {'x': screenPos.dx, 'y': screenPos.dy});
}
- Available extensions
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasRenderEditable → bool
-
Whether a RenderEditable was found (TextField is properly set up).
no setter
- isAttached → bool
-
Whether the tracker is attached to a mounted widget.
no setter
-
key
→ GlobalKey<
State< StatefulWidget> > -
The GlobalKey to attach to the TextField.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
getCursorAnchorInView(
TextEditingController controller, {CursorAnchor anchor = CursorAnchor.bottomLeft}) → Offset? - Get a specific anchor point of the cursor in view coordinates.
-
getCursorPosition(
TextEditingController controller) → Offset? - Get the current cursor position.
-
getCursorPositionAt(
int offset) → Offset? - Get the cursor position (top-left of cursor rect) at a specific offset.
-
getCursorRect(
TextEditingController controller) → Rect? - Get the cursor rect at the current selection/cursor position.
-
getCursorRectAt(
int offset) → Rect? - Get the cursor rect at a specific text offset.
-
getCursorRectInView(
TextEditingController controller) → Rect? - Get the cursor rect in coordinates relative to the Flutter view (window).
-
getCursorScreenPosition(
TextEditingController controller, ScreenRect windowBounds, {CursorAnchor anchor = CursorAnchor.bottomLeft}) → Offset? - Get a cursor anchor point in screen coordinates.
-
getCursorScreenRect(
TextEditingController controller, ScreenRect windowBounds) → ScreenRect? - Get the cursor rect in screen coordinates.
-
getSelectionRectInView(
TextEditingController controller) → Rect? -
Available on TextCursorTracker, provided by the TextSelectionBounds extension
Get the bounding rect for the current text selection. -
getSelectionScreenRect(
TextEditingController controller, ScreenRect windowBounds) → ScreenRect? -
Available on TextCursorTracker, provided by the TextSelectionBounds extension
Get selection bounds in screen coordinates. -
hasSelection(
TextEditingController controller) → bool -
Available on TextCursorTracker, provided by the TextSelectionBounds extension
Whether there is an active (non-collapsed) selection. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited