代码之家  ›  专栏  ›  技术社区  ›  Benno Richters

通过颤振测试找到一个TextSpan

  •  0
  • Benno Richters  · 技术社区  · 5 年前

    RichText(
      text: TextSpan(
        children: [
          TextSpan(text: 'aaa '),
          TextSpan(
            text: 'bbb ',
            recognizer: TapGestureRecognizer()
              ..onTap = () { 
                // How to reach this code in a widget test?
              },
          ),
          TextSpan(text: 'ccc'),
        ],
      ),
    )
    
    1 回复  |  直到 5 年前
        1
  •  7
  •   Kahou    5 年前

    CommonFinders byWidgetPredicate method

    InlineSpan visitChildren method

    查找文本范围:

    final finder = find.byWidgetPredicate(
      (widget) => widget is RichText && tapTextSpan(widget, "bbb "),
    );
    
    bool findTextAndTap(InlineSpan visitor, String text) {
      if (visitor is TextSpan && visitor.text == text) {
        (visitor.recognizer as TapGestureRecognizer).onTap();
    
        return false;
      }
    
      return true;
    }
    
    bool tapTextSpan(RichText richText, String text) {
      final isTapped = !richText.text.visitChildren(
        (visitor) => findTextAndTap(visitor, text),
      );
    
      return isTapped;
    }