dart 如何在Flutter小部件测试中测试语义属性

gdrx4gfi  于 12个月前  发布在  Flutter
关注(0)|答案(1)|浏览(211)

如何在Flutter小部件测试中测试语义属性?
我想测试一个特定的小部件是否具有预期的语义标签。
这是一个widget:

class Tag extends StatelessWidget {
  const Tag({
    required this.semanticsLabel,
    required this.name,
    required this.onPressed,
    super.key,
  });

  final String name;

  final VoidCallback onPressed;

  final String semanticsLabel;

  @override
  Widget build(BuildContext context) {
    return Semantics(
      label: semanticsLabel,
      button: true,
      child: ExcludeSemantics(
          child: OutlinedButton(
            onPressed: onPressed,
            child: Text(
              name,
            ),
          ),
        ),
      );
    }
  }

字符串

9jyewag0

9jyewag01#

标签的测试用例可能如下所示:

final tagCategorySemantics = tester.getSemantics(find.byType(Tag));
 expect(tagCategorySemantics.label, 'Semantic Tag Label');

字符串
如果你也有一个语义动作,它可以像这样测试:

final semanticsOwner =
          RendererBinding.instance.rootPipelineOwner.semanticsOwner;

final tagSemantics = tester.getSemantics(find.byType(Tag));

semanticsOwner?.performAction(
        tagSemantics.id,
        SemanticsAction.didGainAccessibilityFocus,
      );

相关问题