FocusSemanticEvent class

An event to move the accessibility focus.

Using this API is generally not recommended, as it may break a users' expectation of how a11y focus works and therefore should be just very carefully.

One possible use case: For example, the currently focused rendering object is replaced by another rendering object. In general, such design should be avoided if possible. If not, one may want to refocus the newly added rendering object.

One example that is not recommended: When a new popup or dropdown opens, moving the focus in these cases may confuse users and make it less accessible.

The following code snippet shows how one can request focus on a certain widget.
link
class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  final GlobalKey mykey = GlobalKey();

  @override
  void initState() {
    super.initState();
    // Using addPostFrameCallback because changing focus need to wait for the widget to finish rendering.
    WidgetsBinding.instance.addPostFrameCallback((_) {
      mykey.currentContext?.findRenderObject()?.sendSemanticsEvent(const FocusSemanticEvent());
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('example'),
      ),
      body: Column(
        children: <Widget>[
          const Text('Hello World'),
          const SizedBox(height: 50),
          Text('set focus here', key: mykey),
        ],
      ),
    );
  }
}

This currently only supports Android and iOS.

Inheritance

Constructors

FocusSemanticEvent()
Constructs an event that triggers a focus change by the platform.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
type String
The type of this event.
finalinherited

Methods

getDataMap() Map<String, dynamic>
Returns the event's data object.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toMap({int? nodeId}) Map<String, dynamic>
Converts this event to a Map that can be encoded with StandardMessageCodec.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited