|
| 1 | +--- |
| 2 | +title: The Visibility widget is no longer focusable by default when maintainState is enabled |
| 3 | +description: >- |
| 4 | + The Visibility widget by default no longer implicitly retains focusability |
| 5 | + for its child when maintainState is enabled. |
| 6 | +--- |
| 7 | + |
| 8 | +## Summary |
| 9 | +This change was introduced to fix an issue |
| 10 | +where an `IndexedStack`s hidden children would be focusable with keyboard events |
| 11 | +(see [issue](https://github.com/flutter/flutter/issues/114213)) |
| 12 | +due to the underlying `Visibility` widgets default behavior. |
| 13 | + |
| 14 | +## Description of change |
| 15 | +The core change is the `Visibility` widget is no longer focusable by default |
| 16 | +when `maintainState` is enabled. |
| 17 | +A new flag, `maintainFocusability`, must be set to true with `maintainState` |
| 18 | +for a hidden widget to remain focusable. |
| 19 | + |
| 20 | +## Migration guide |
| 21 | +If your app has a `Visibility` widget that does not set `maintainState` to true, |
| 22 | +then no changes are required. |
| 23 | + |
| 24 | +If your app has a `Visibility` widget that sets `maintainState` to true |
| 25 | +and you relied on the previous default behavior |
| 26 | +that allowed you to focus your hidden widget, |
| 27 | +you will need to set `maintainFocusability` to true. |
| 28 | + |
| 29 | +Code before migration: |
| 30 | + |
| 31 | +```dart |
| 32 | +child: Visibility( |
| 33 | + maintainState: true, |
| 34 | + child: SomeWidget(), |
| 35 | +) |
| 36 | +``` |
| 37 | + |
| 38 | +Code after migration: |
| 39 | + |
| 40 | +```dart |
| 41 | +child: Visibility( |
| 42 | + maintainState: true, |
| 43 | + maintainFocusability: true, |
| 44 | + child: SomeWidget(), |
| 45 | +) |
| 46 | +``` |
| 47 | + |
| 48 | +## Timeline |
| 49 | + |
| 50 | +Landed in version: 3.34.0-pre<br> |
| 51 | +In stable release: 3.35 |
| 52 | + |
| 53 | +## References |
| 54 | + |
| 55 | +API documentation: |
| 56 | + |
| 57 | +* [`Visibility`]({{site.api}}/flutter/widgets/Visibility-class.html) |
| 58 | + |
| 59 | +Relevant issues: |
| 60 | + |
| 61 | +* [Issue 114213]({{site.repo.flutter}}/issues/114213) |
| 62 | + |
| 63 | +Relevant PRs: |
| 64 | + |
| 65 | +* [PR 159133: Add flag to exclude focus for hidden children in Visibility, maintainFocusability. Set maintainFocusability to false in IndexedStack]({{site.repo.flutter}}/pull/159133) |
0 commit comments