placeCaretInSuperTextField method

Future<void> placeCaretInSuperTextField(
  1. int offset, [
  2. Finder? superTextFieldFinder,
  3. TextAffinity affinity = TextAffinity.downstream
])

Taps to place a caret at the given offset.

By default, this method expects a single SuperTextField in the widget tree and finds it byType. To specify one SuperTextField among many, pass a superTextFieldFinder.

Implementation

Future<void> placeCaretInSuperTextField(int offset,
    [Finder? superTextFieldFinder, TextAffinity affinity = TextAffinity.downstream]) async {
  final fieldFinder =
      SuperTextFieldInspector.findInnerPlatformTextField(superTextFieldFinder ?? find.byType(SuperTextField));
  final match = fieldFinder.evaluate().single.widget;
  bool found = false;
  final scrollDelta = SuperTextFieldInspector.findScrollOffset(superTextFieldFinder)!;
  final scrollOffset =
      SuperTextFieldInspector.isSingleLine(superTextFieldFinder) ? Offset(scrollDelta, 0) : Offset(0, scrollDelta);

  if (match is SuperDesktopTextField) {
    final didTap = await _tapAtTextPositionOnDesktop(
        state<SuperDesktopTextFieldState>(fieldFinder), offset, affinity, scrollOffset);
    if (!didTap) {
      throw Exception("The desired text offset wasn't tappable in SuperTextField: $offset");
    }
    found = true;
  } else if (match is SuperAndroidTextField) {
    final didTap = await _tapAtTextPositionOnAndroid(
        state<SuperAndroidTextFieldState>(fieldFinder), offset, affinity, scrollOffset);
    if (!didTap) {
      throw Exception("The desired text offset wasn't tappable in SuperTextField: $offset");
    }
    found = true;
  } else if (match is SuperIOSTextField) {
    final didTap =
        await _tapAtTextPositionOnIOS(state<SuperIOSTextFieldState>(fieldFinder), offset, affinity, scrollOffset);
    if (!didTap) {
      throw Exception("The desired text offset wasn't tappable in SuperTextField: $offset");
    }
    found = true;
  }

  if (found) {
    await pumpAndSettle(kTapTimeout);
  } else {
    throw Exception("Couldn't find a SuperTextField with the given Finder: $fieldFinder");
  }
}