createTextFinder function

Finder createTextFinder(
  1. String textSelector
)

Creates a flexible text finder based on the text selector format.

Supports multiple text matching strategies:

  • text:exact - Exact text match (default behavior)
  • contains:partial - Text contains the specified substring
  • startsWith:prefix - Text starts with the specified prefix
  • endsWith:suffix - Text ends with the specified suffix
  • regex:pattern - Text matches the regex pattern

Implementation

Finder createTextFinder(String textSelector) {
  if (textSelector.startsWith('contains:')) {
    String searchText = textSelector.substring(9);
    return find.textContaining(searchText);
  } else if (textSelector.startsWith('startsWith:')) {
    String prefix = textSelector.substring(11);
    return find.byWidgetPredicate((widget) {
      if (widget is Text) {
        final String? data = widget.data;
        return data != null && data.startsWith(prefix);
      }
      return false;
    });
  } else if (textSelector.startsWith('endsWith:')) {
    String suffix = textSelector.substring(9);
    return find.byWidgetPredicate((widget) {
      if (widget is Text) {
        final String? data = widget.data;
        return data != null && data.endsWith(suffix);
      }
      return false;
    });
  } else if (textSelector.startsWith('regex:')) {
    String pattern = textSelector.substring(6);
    RegExp regex = RegExp(pattern);
    return find.byWidgetPredicate((widget) {
      if (widget is Text) {
        final String? data = widget.data;
        return data != null && regex.hasMatch(data);
      }
      return false;
    });
  } else if (textSelector.startsWith('text:')) {
    // Explicit exact match
    String exactText = textSelector.substring(5);
    return find.text(exactText);
  } else {
    // Default to exact match for backward compatibility
    return find.text(textSelector);
  }
}