The Semantics widget in Flutter is used to provide accessibility information to the Flutter framework for users with disabilities. It allows developers to add semantics, such as labels, hints, and actions, to widgets, making the app more accessible to users who rely on screen readers or other assistive technologies.
Attributes:
container (bool):
- Whether the semantics node is a container node. Container nodes can have children.
label (String):
- A textual label for the semantics node, describing its purpose or role.
value (String):
- A textual description of the current value of the semantics node.
hint (String):
- A textual hint for the semantics node, providing additional context or information about how to interact with it.
onTap (VoidCallback):
- A callback function that is called when the semantics node is tapped.
onLongPress (VoidCallback):
- A callback function that is called when the semantics node is long-pressed.
Example:
import 'package:flutter/material.dart';
class SemanticsExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Semantics Example'),
),
body: Center(
child: Semantics(
label: 'Tap me!',
hint: 'Double tap to select',
onTap: () {
print('Semantics tapped!');
},
child: Container(
width: 200,
height: 200,
color: Colors.blue,
child: Center(
child: Text(
'Tap Me!',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
),
),
);
}
}
void main() {
runApp(MaterialApp(
home: SemanticsExample(),
));
}
In this example:
A Semantics widget is used to wrap a Container widget.
The label attribute is set to "Tap me!", providing a textual label for the container.
The hint attribute is set to "Double tap to select", providing additional context about how to interact with the container.
The onTap attribute is set to a callback function that prints a message when the container is tapped.
When the user interacts with the container, the accessibility information provided by the Semantics widget helps users with disabilities understand the purpose of the container and how to interact with it.