createTextSelection function

TextSelection? createTextSelection(
  1. String str, {
  2. int? baseOffset,
  3. int? extentOffset,
  4. bool trim = true,
})

Returns a new TextSelection, trimming whitespace characters if specified.

Returns null if the resulting string would be empty.

Implementation

TextSelection? createTextSelection(
  String str, {
  int? baseOffset,
  int? extentOffset,
  bool trim = true,
}) {
  // ignore: unnecessary_null_comparison
  assert(str != null);
  // ignore: unnecessary_null_comparison
  if (str == null || str.isEmpty) return null;
  final len = str.length;
  var first = baseOffset ?? 0;
  var last = extentOffset != null ? math.min(extentOffset, len) - 1 : len - 1;
  if (trim) {
    while (first < len && _shouldSkip(str.codeUnitAt(first))) {
      first++;
    }
    while (last > first && _shouldSkip(str.codeUnitAt(last))) {
      last--;
    }
  }
  if (last < first) return null;
  return TextSelection(baseOffset: first, extentOffset: last + 1);
}