Skip to content

FocusScopeNode.descendantsAreFocusable set to false after disabling and enabling canRequestFocus #147256

@knopp

Description

@knopp

Happens with current main (3.22.0-16.0.pre.26).

Widget test to reproduce:

  testWidgets('FocusScope descendantsAreFocusable', (WidgetTester tester) async {
    final focusScopeNode = FocusScopeNode();
    await tester.pumpWidget(
      FocusScope(
        node: focusScopeNode,
        child: const SizedBox.shrink(),
      ),
    );
    expect(focusScopeNode.descendantsAreFocusable, isTrue);

    focusScopeNode.canRequestFocus = false;
    expect(focusScopeNode.descendantsAreFocusable, isFalse);
 
    focusScopeNode.canRequestFocus = true;
    expect(focusScopeNode.descendantsAreFocusable, isTrue);

    // Disabling canRequestFocus and updating the widget will permanently disable descendantsAreFocusable.
    focusScopeNode.canRequestFocus = false;

    // After this pump, setting canRequestFocus back will not restore
    // descendantsAreFocusable to true.
    await tester.pumpWidget(
      FocusScope(
        node: focusScopeNode,
        child: const SizedBox.shrink(),
      ),
    );

    focusScopeNode.canRequestFocus = true;

    // Test failure:
    // This should be back to true, but in FocusNode state didUpdateWidget the 
    // descendantsAreFocusable on the focusScopeNode got set to false.
    expect(focusScopeNode.descendantsAreFocusable, isTrue);
  });

This break focus in _ModalScopeState which sets canRequestFocus on the scope node and then rebuilds widget, which in turns sets the descendantsAreFocusable to false.

@gspencergoog, any thoughts? The focus code is quite convoluted, in this case the _FocusState.didUpdateWidget actually overrides property on FocusScopeNode, which is quite surprising.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our testsf: focusFocus traversal, gaining or losing focusfound in release: 3.19Found to occur in 3.19found in release: 3.22Found to occur in 3.22frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions