Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

Flutter: How to disable FocusTraversal for non-visible children?

I have a desktop app, with a layout like:

TitleBar()
========================
IndexedStack(children: [Page1(), Page2()])

When on Page 1 (for example) I want the focus traversal to go from the TitleBar > Page1 > TitleBar, but what it does instead is TitleBar > Page1 > Page2 > TitleBar.

Wrapping each child in a FocusScope doesn't work, since I want TitleBar to be able to go into Page1. Wrapping in Focus and setting canRequestFocus does not work. Using multiple TraversalGroups does not work.

Any idea? I need the equivalent of IgnorePointer() for focus events. Then I could have:

TitleBar(),
IndexedStack(
  children: [
    BlockFocus(block: index != 0, child: Page1()),
    BlockFocus(block: index != 1, child: Page2()),

  ]
)

I've tried using TraversalGroup + descendantsAreFocusable=false, but good old Flutter screams at me with an assertion, even tho this is exactly what I want to do:

sorted descendants contains more nodes than it should: ({FocusNode#9a2ed(FocusTraversalGroup)(context: Focus, NOT FOCUSABLE)})
Failed assertion: line 340 pos 7: 'sortedDescendants.length <= scope.traversalDescendants.length && sortedDescendants.toSet().difference(scope.traversalDescendants.toSet()).isEmpty'

The docs on this property are not helpful:

/// {@macro flutter.widgets.Focus.descendantsAreFocusable}
  final bool descendantsAreFocusable;

I've also tried:

FocusScope(skipTraversal: true, child: child)

And it does nothing, still allowing all the nodes to be focused. Getting kinda frustrating that nothing just acts like it should :/ descendantsAreFocusable throws an error for no apparent reason, skipTraversal does not actually skip traversal.

question from:https://stackoverflow.com/questions/65888930/flutter-how-to-disable-focustraversal-for-non-visible-children

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...