TextInputBuffer class
Manages text input state with cursor positioning.
Encapsulates the common pattern found in text-based terminal prompts:
- Text buffer with cursor position
- Character insertion at cursor
- Backspace/delete operations
- Cursor movement (left/right, home/end)
- Optional max length enforcement
Usage:
final input = TextInputBuffer();
// Handle key events
if (input.handleKey(event)) {
// Input was modified, re-render
}
// Or use individual operations
input.insert('a');
input.backspace();
input.moveCursor(-1);
// Access state
final text = input.text;
final cursor = input.cursorPosition;
Key features:
- Cursor-aware text editing
- Efficient StringBuffer-based storage
- Key event handling helper
- Selection support (future-ready)
- Zero boilerplate in prompts
- Available extensions
Constructors
- TextInputBuffer({String initialText = '', int? maxLength})
- Creates a new text input buffer.
Properties
- charAtCursor → String?
-
Character at cursor position, or null if cursor is at end.
no setter
- cursorAtEnd → bool
-
Whether the cursor is at the end.
no setter
- cursorAtStart → bool
-
Whether the cursor is at the start.
no setter
- cursorPosition → int
-
Current cursor position.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isEmpty → bool
-
Whether the buffer is empty.
no setter
- isNotEmpty → bool
-
Whether the buffer is not empty.
no setter
- length → int
-
Length of the current text.
no setter
- maxLength → int?
-
Optional maximum length for the input.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- text → String
-
Current text content.
no setter
- textAfterCursor → String
-
Text after the cursor (including char at cursor).
no setter
- textBeforeCursor → String
-
Text before the cursor.
no setter
Methods
-
append(
String text) → void -
Available on TextInputBuffer, provided by the SimpleTextInput extension
Appends text to the end (ignoring cursor position). -
backspace(
) → bool - Deletes the character before the cursor (backspace).
-
backspaceWord(
) → bool - Deletes word before cursor (Ctrl+Backspace behavior).
-
clear(
) → void - Clears all text and resets cursor to start.
-
delete(
) → bool - Deletes the character at the cursor (delete key).
-
handleKey(
KeyEvent event) → bool - Handles a key event for text input.
-
handleKeyExtended(
KeyEvent event, {bool ctrl = false}) → bool - Handles a key event with extended controls (word movement, etc.).
-
insert(
String char) → bool - Inserts a character at the cursor position.
-
insertText(
String text) → int - Inserts text at the cursor position.
-
moveCursor(
int delta) → void - Moves cursor by delta positions (negative = left, positive = right).
-
moveCursorToEnd(
) → void - Moves cursor to the end.
-
moveCursorToStart(
) → void - Moves cursor to the start.
-
moveCursorWordLeft(
) → void - Moves cursor to the start of the previous word.
-
moveCursorWordRight(
) → void - Moves cursor to the end of the next word.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
removeLast(
) → bool -
Available on TextInputBuffer, provided by the SimpleTextInput extension
Removes the last character (ignoring cursor position). -
setCursorPosition(
int position) → void - Moves cursor to a specific position (clamped).
-
setText(
String newText) → void - Sets the buffer to new text, cursor at end.
-
textWithBlockCursor(
) → TextWithBlockCursor - Returns text formatted for display with inverse-video cursor.
-
textWithCursor(
{String cursorChar = '▌', bool showCursor = true}) → String - Returns the text with a cursor indicator at the current position.
-
toString(
) → String -
A string representation of this object.
override
-
toTextInputBindings(
{void onInput()?}) → KeyBindings -
Available on TextInputBuffer, provided by the TextInputBindingsExtensions extension
Creates text input bindings that delegate to a TextInputBuffer.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited