Skip to main content

prefer-gestures-semantics-role

effort: 4m
pro+

Warns when a custom tappable widget is not wrapped in Semantics.

Wrapping custom tappable widgets in Semantics is required to ensure proper role announcement.

Example​

❌ Bad:

Widget _build(bool flag) {
// LINT: Prefer wrapping custom tappable widgets in 'Semantics' to ensure proper role announcement.
final widget = InkResponse(onTap: () {});

if (flag) {
return Semantics(button: true, child: widget);
}

return widget;
}

Widget _conditionalBuild(bool flag) {
// LINT: Prefer wrapping custom tappable widgets in 'Semantics' to ensure proper role announcement.
Widget widget = InkResponse(onTap: () {});

if (flag) {
widget = Semantics(button: true, child: widget);
} else {
widget = Column(children: [widget]);
}

return widget;
}

✅ Good:

Widget _build(bool flag) {
final widget = InkResponse(onTap: () {});

return Semantics(button: true, child: widget);
}

Widget _conditionalBuild(bool flag) {
Widget widget = InkResponse(onTap: () {});

if (flag) {
widget = Semantics(button: true, child: widget);
} else {
widget = Column(children: [Semantics(button: true, child: widget)]);
}

return widget;
}