Skip to content

Add requestFocus to showModalBottomSheet and showDialog and any other equivalents #162920

@benji-farquhar

Description

@benji-farquhar

Use case

I had a strange bug where when i closed the ModalBottomSheet/Dialog, it would focus my Autocomplete, which i've designed to open the results box on focus. So it would open the search box after closing the dialog/bottomsheet.

I realise most people won't have that form control, but still, man it was hard to find out what was focusing it, and i'm sure some other people don't want their first control focused upon dialog/bottomsheet close.

I found out it was this code inside routes.dart, and can be turned off by setting shouldRequestFocus to false.

  // This should be called to wrap any changes to route.isCurrent, route.canPop,
  // and route.offstage.
  void _routeSetState(VoidCallback fn) {
    if (widget.route.isCurrent && !_shouldIgnoreFocusRequest && _shouldRequestFocus) {
      widget.route.navigator!.focusNode.enclosingScope?.setFirstFocus(focusScopeNode);
    }
    setState(fn);
  }

ModalBottomSheetRoute accepts a requestFocus param which Future<T?> showModalBottomSheet<T>({ instantiates, but showModalBottomSheet doesn't have requestFocus as a param.

Proposal

Add requestFocus to showModalBottomSheet and all other types of popups that may want the same treatment (dialogs at the least).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions