getCursorLineAndChar method

Map<String, int> getCursorLineAndChar()

Returns the current cursor position as a map with 'line' and 'character' keys.

The 'line' key is zero-based, and the 'character' key is also zero-based. If the cursor is at the start of the text, it returns {'line': 0, 'character': 0}.

Implementation

Map<String, int> getCursorLineAndChar() {
  final int offset = selection.baseOffset;
  if (offset < 0) selection = TextSelection.collapsed(offset: 0);
  if (offset > text.length) {
    selection = TextSelection.collapsed(offset: text.length);
  }

  final lines = text.substring(0, offset).split('\n');
  final line = lines.length - 1;
  final character = lines.isNotEmpty ? lines.last.length : 0;
  return {'line': line, 'character': character};
}