hasWidgetProp<T> method

WidgetMatcher<W> hasWidgetProp<T>({
  1. required NamedWidgetProp<W, T?> prop,
  2. required MatchProp<T> match,
})

Asserts that a widget's property meets a specific condition.

This method is useful for making assertions on properties directly within the widget.

Example usage:

spot<Checkbox>().existsOnce().hasWidgetProp(
  prop: widgetProp('value', (widget) => widget.value),
  match: (value) => value.isTrue(),
);

Implementation

WidgetMatcher<W> hasWidgetProp<T>({
  required NamedWidgetProp<W, T?> prop,
  required MatchProp<T> match,
}) {
  void condition(Subject<Element> subject) {
    final value = subject.context.nest<T?>(
      () => ['$W', "with prop '${prop.name}'"],
      (element) {
        final value = getWidgetProp(prop);
        return Extracted.value(value);
      },
    ).hideNullability();
    match(value);
  }

  final failure = softCheckHideNull(element, condition);
  failure.throwPropertyCheckFailure(condition, element);
  return this;
}