Skip to content

Setting accessibility Focus via code #147402

@rafaldziuryk

Description

@rafaldziuryk

Steps to reproduce

  1. Have 2 widgets - e.x.
    focusable widget with FocusNode
    button that request focus at first widget on callback.

Expected results

Accessibility focus moves to first widget

Actual results

Accessibility focus moves to first widget only once at Android and not moves any at iOS.

Additionally when we have two pair of focusable widget and button to request focus then on Android we can request focus at button1, button2, button1, button2 - and it works. iOS not work

Code sample

Code sample
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appTitle = 'Focus demo';

    return MaterialApp(
      title: appTitle,
      home: Scaffold(
        appBar: AppBar(
          title: Text(appTitle),
        ),
        body: FocusTest(),
      ),
    );
  }
}

class FocusTest extends StatefulWidget {
  @override
  FocusTestState createState() {
    return FocusTestState();
  }
}

class FocusTestState extends State<FocusTest> {
  final focusNode1 = FocusNode();
  final focusNode2 = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Semantics(
              focusable: true,
              focused: focusNode1.hasFocus,
              child: ElevatedButton(
                  focusNode: focusNode1,
                  child: Text("Focusable widget 1 with focus node"),
                  onPressed: () => print("test 1"))),
          Semantics(
              focusable: true,
              focused: focusNode2.hasFocus,
              child: ElevatedButton(
                  focusNode: focusNode2,
                  child: Text("Focusable widget 2 with focus node"),
                  onPressed: () => print("test 2"))),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16.0),
            child: Semantics(
              focusable: true,
              label: 'Focus 1',
              child: ElevatedButton(
                onPressed: () {
                  focusNode1.requestFocus();
                  print(focusNode1.hasFocus);
                  print(focusNode1.hasPrimaryFocus);
                },
                child: Text('Focus 1'),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16.0),
            child: Semantics(
              focusable: true,
              label: 'Focus 2',
              child: ElevatedButton(
                onPressed: () {
                  focusNode2.requestFocus();
                  print(focusNode2.hasFocus);
                  print(focusNode2.hasPrimaryFocus);
                },
                child: Text('Focus 2'),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel stable, 3.19.5, on macOS 14.1 23B74 darwin-arm64, locale en)
    ! Warning: `dart` on your path resolves to /opt/homebrew/Cellar/dart/3.1.5/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/rafaldziuryk/fvm/versions/3.19.5. Consider adding /Users/rafaldziuryk/fvm/versions/3.19.5/bin to the front of your path.
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.1)
[✓] Connected device (4 available)
    ! Error: Browsing on the local area network for iPhone (strugackis) 13. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in triagePresently being triaged by the triage teamwaiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions