createTextFinder function
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 substringstartsWith:prefix- Text starts with the specified prefixendsWith:suffix- Text ends with the specified suffixregex: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);
}
}