pub struct Focus {
pub key: Option<KeyRef>,
pub parent_node: Option<FocusNodeRef>,
pub child: WidgetRef,
pub focus_node: Option<FocusNodeRef>,
pub autofocus: bool,
pub on_focus_change: Option<ValueChanged<bool>>,
pub include_semantics: bool,
/* private fields */
}Expand description
A widget that manages a FocusNode to allow keyboard focus to be given to this widget and
its descendants.
When the focus is gained or lost, on_focus_change is called.
For key events, on_key_event is called if
AnyFocusNode::has_focus is true for this widget’s focus_node,
unless a focused descendant’s on_key_event callback returned
KeyEventResult::Handled when called.
This widget does not provide any visual indication that the focus has changed. Any desired
visual changes should be made when on_focus_change is called.
To access the FocusNode of the nearest ancestor Focus widget and establish a
relationship that will rebuild the widget when the focus changes, use the Focus::of and
FocusScope::of static methods.
Managing a FocusNode means managing its lifecycle, listening for changes in focus, and
re-parenting it when needed to keep the focus hierarchy in sync with the widget hierarchy.
This widget does all of those things for you.
If the Focus::new constructor is used, then this widget will manage any given
focus_node by overwriting the appropriate values of the node with the
values of this widget whenever the Focus widget is updated.
If Focus::with_external_focus_node is used instead, then the values returned by
on_key_event, skip_traversal,
can_request_focus, and
descendants_are_focusable will be the values in the
external focus node, and the external focus node’s values will not be overwritten when the
widget is updated.
To collect a sub-tree of nodes into an exclusive group that restricts focus traversal to the
group, use a FocusScope.
Fields§
§key: Option<KeyRef>See Widget::key.
parent_node: Option<FocusNodeRef>The optional parent node to use when reparenting the
focus_node for this Focus widget.
If parent_node is None, then Focus::maybe_of is used to
find the parent in the widget tree, which is typically what is desired, since it is
easier to reason about the focus tree if it mirrors the shape of the widget tree.
Set this property if the focus tree needs to have a different shape than the widget
tree. This is typically in cases where a dialog is in an Overlay (or another part of
the widget tree), and focus should behave as if the widgets in the overlay are
descendants of the given parent_node for purposes of focus.
Defaults to None.
child: WidgetRefThe child widget of this Focus.
focus_node: Option<FocusNodeRef>An optional focus node to use as the focus node for this widget.
If one is not supplied, then one will be automatically allocated, owned, and managed by
this widget. The widget will be focusable even if a
focus_node is not supplied. If supplied, the given node will be
hosted by this widget, but not owned. See FocusNode for more information on what
being hosted and/or owned implies.
A non-null focus_node must be supplied if using the
Focus::with_external_focus_node constructor.
autofocus: boolTrue if this widget will be selected as the initial focus when no other node in its scope is currently focused.
Ideally, there is only one widget with autofocus set in each FocusScope. If there is
more than one widget with autofocus set, then the first one added to the tree will get
focus.
Defaults to false.
on_focus_change: Option<ValueChanged<bool>>Handler called when the focus changes.
Called with true if this widget’s node gains focus, and false if it loses focus.
include_semantics: boolInclude semantics information in this widget.
Defaults to true.
Implementations§
Source§impl Focus
impl Focus
Sourcepub fn new<K>(child: impl IntoWidget<K>) -> Focus
pub fn new<K>(child: impl IntoWidget<K>) -> Focus
Creates a widget that manages a FocusNode.
Sourcepub fn with_external_focus_node<K>(
child: impl IntoWidget<K>,
focus_node: FocusNodeRef,
) -> Focus
pub fn with_external_focus_node<K>( child: impl IntoWidget<K>, focus_node: FocusNodeRef, ) -> Focus
Creates a Focus widget that uses the given focus_node as the source of truth for
attributes on the node, rather than the attributes of this widget.
Sourcepub fn parent_node(self, parent_node: FocusNodeRef) -> Focus
pub fn parent_node(self, parent_node: FocusNodeRef) -> Focus
Dart Focus(parentNode:).
Sourcepub fn focus_node(self, focus_node: FocusNodeRef) -> Focus
pub fn focus_node(self, focus_node: FocusNodeRef) -> Focus
Dart Focus(focusNode:).
Sourcepub fn on_focus_change(
self,
on_focus_change: impl Fn(&mut App, bool) + 'static,
) -> Focus
pub fn on_focus_change( self, on_focus_change: impl Fn(&mut App, bool) + 'static, ) -> Focus
Dart Focus(onFocusChange:).
Sourcepub fn on_key_event(self, on_key_event: FocusOnKeyEventCallback) -> Focus
pub fn on_key_event(self, on_key_event: FocusOnKeyEventCallback) -> Focus
Dart Focus(onKeyEvent:).
Sourcepub fn can_request_focus(self, can_request_focus: bool) -> Focus
pub fn can_request_focus(self, can_request_focus: bool) -> Focus
Dart Focus(canRequestFocus:).
Sourcepub fn skip_traversal(self, skip_traversal: bool) -> Focus
pub fn skip_traversal(self, skip_traversal: bool) -> Focus
Dart Focus(skipTraversal:).
Sourcepub fn descendants_are_focusable(self, descendants_are_focusable: bool) -> Focus
pub fn descendants_are_focusable(self, descendants_are_focusable: bool) -> Focus
Dart Focus(descendantsAreFocusable:).
Sourcepub fn descendants_are_traversable(
self,
descendants_are_traversable: bool,
) -> Focus
pub fn descendants_are_traversable( self, descendants_are_traversable: bool, ) -> Focus
Dart Focus(descendantsAreTraversable:).
Sourcepub fn include_semantics(self, include_semantics: bool) -> Focus
pub fn include_semantics(self, include_semantics: bool) -> Focus
Dart Focus(includeSemantics:).
Sourcepub fn debug_label(self, debug_label: impl Into<String>) -> Focus
pub fn debug_label(self, debug_label: impl Into<String>) -> Focus
Dart Focus(debugLabel:).
Sourcepub fn get_on_key_event(&self, app: &App) -> Option<FocusOnKeyEventCallback>
pub fn get_on_key_event(&self, app: &App) -> Option<FocusOnKeyEventCallback>
A handler for keys that are pressed when this object or one of its children has focus.
Key events are first given to the FocusNode that has primary focus, and if its
on_key_event returns KeyEventResult::Ignored, then they are given to each ancestor
node up the focus hierarchy in turn. If an event reaches the root of the hierarchy, it
is discarded.
Sourcepub fn get_can_request_focus(&self, app: &App) -> bool
pub fn get_can_request_focus(&self, app: &App) -> bool
If true, this widget may request the primary focus.
Defaults to true. Set to false if you want the FocusNode this widget manages to do
nothing when AnyFocusNode::request_focus is called on it. Does not affect the
children of this node, and AnyFocusNode::has_focus can still return true if this
node is the ancestor of the primary focus.
Sourcepub fn get_skip_traversal(&self, app: &mut App) -> bool
pub fn get_skip_traversal(&self, app: &mut App) -> bool
Sets the AnyFocusNode::skip_traversal flag on the focus node so that it won’t be
visited by the FocusTraversalPolicy.
Sourcepub fn get_descendants_are_focusable(&self, app: &App) -> bool
pub fn get_descendants_are_focusable(&self, app: &App) -> bool
If false, will make this widget’s descendants unfocusable.
Defaults to true. Does not affect focusability of this node (just its descendants): for
that, use AnyFocusNode::can_request_focus.
Sourcepub fn get_descendants_are_traversable(&self, app: &App) -> bool
pub fn get_descendants_are_traversable(&self, app: &App) -> bool
If false, will make this widget’s descendants untraversable.
Defaults to true. Does not affect traversability of this node (just its descendants):
for that, use AnyFocusNode::skip_traversal.
Sourcepub fn get_debug_label(&self, app: &App) -> Option<String>
pub fn get_debug_label(&self, app: &App) -> Option<String>
A debug label for this widget.
Sourcepub fn of(
app: &mut App,
context: BuildContext,
scope_ok: bool,
create_dependency: bool,
) -> AnyFocusNode
pub fn of( app: &mut App, context: BuildContext, scope_ok: bool, create_dependency: bool, ) -> AnyFocusNode
Returns the focus node of the Focus that most tightly encloses the given
BuildContext.
If no Focus node is found before reaching the nearest FocusScope widget, or
there is no Focus widget in the context, then this method panics.
If create_dependency is true, calling this function creates a dependency that will
rebuild the given context when the focus node gains or loses focus.
Sourcepub fn maybe_of(
app: &mut App,
context: BuildContext,
scope_ok: bool,
create_dependency: bool,
) -> Option<AnyFocusNode>
pub fn maybe_of( app: &mut App, context: BuildContext, scope_ok: bool, create_dependency: bool, ) -> Option<AnyFocusNode>
Returns the focus node of the Focus that most tightly encloses the given
BuildContext.
If no Focus node is found before reaching the nearest FocusScope widget, or
there is no Focus widget in scope, then this method returns None.
If create_dependency is true, calling this function creates a dependency that will
rebuild the given context when the focus node gains or loses focus.
Sourcepub fn is_at(app: &mut App, context: BuildContext) -> bool
pub fn is_at(app: &mut App, context: BuildContext) -> bool
Returns true if the nearest enclosing Focus widget’s node is focused.
A convenience method to allow build methods to write Focus::is_at(app, context) to get
whether or not the nearest Focus above them in the widget hierarchy currently has
the input focus.
Returns false if no Focus widget is found before reaching the nearest
FocusScope, or if the root of the focus tree is reached without finding a Focus
widget.
Calling this function creates a dependency that will rebuild the given context when the focus changes.
Trait Implementations§
Source§impl StatefulWidget for Focus
impl StatefulWidget for Focus
Source§type State = FocusState
type State = FocusState
Source§fn debug_fill_properties(
&self,
app: &App,
properties: &mut DiagnosticPropertiesBuilder,
)
fn debug_fill_properties( &self, app: &App, properties: &mut DiagnosticPropertiesBuilder, )
debugFillProperties: the properties a dump shows for this widget, in Dart’s
order. Dart’s Widget base only sets the dense tree style, which the framework does.Source§fn create_state(&self) -> FocusState
fn create_state(&self) -> FocusState
Source§fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
Source§fn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
Widget::type_name. A stateful form another widget kind becomes (an animated or
status transition widget) names the widget it wraps.Auto Trait Implementations§
impl !RefUnwindSafe for Focus
impl !Send for Focus
impl !Sync for Focus
impl !UnwindSafe for Focus
impl Forget for Focus
impl Freeze for Focuswhere
*const RcInner<dyn Key> is !null: Freeze,
PhantomData<RcInner<dyn Key>>: Freeze,
NonZero<u32>: Freeze,
&'static FocusNodeVTable: Freeze,
*const RcInner<Keep> is !null: Freeze,
PhantomData<RcInner<Keep>>: Freeze,
*const RcInner<dyn Widget> is !null: Freeze,
PhantomData<RcInner<dyn Widget>>: Freeze,
*const RcInner<dyn Fn(&mut App, bool)> is !null: Freeze,
PhantomData<RcInner<dyn Fn(&mut App, bool)>>: Freeze,
*const RcInner<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult> is !null: Freeze,
PhantomData<RcInner<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult>>: Freeze,
*const u8 is !null: Freeze,
PhantomData<u8>: Freeze,
impl Move for Focus
impl Unpin for Focus
impl UnsafeUnpin for Focuswhere
*const RcInner<dyn Key> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Key>>: UnsafeUnpin,
NonZeroU32Inner: UnsafeUnpin,
&'static FocusNodeVTable: UnsafeUnpin,
*const RcInner<Keep> is !null: UnsafeUnpin,
PhantomData<RcInner<Keep>>: UnsafeUnpin,
*const RcInner<dyn Widget> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Widget>>: UnsafeUnpin,
*const RcInner<dyn Fn(&mut App, bool)> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Fn(&mut App, bool)>>: UnsafeUnpin,
*const RcInner<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult> is !null: UnsafeUnpin,
PhantomData<RcInner<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult>>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<W> IntoWidget<StatefulKind> for Wwhere
W: StatefulWidget,
impl<W> IntoWidget<StatefulKind> for Wwhere
W: StatefulWidget,
Source§fn into_widget(self) -> Rc<dyn Widget>
fn into_widget(self) -> Rc<dyn Widget>
creation_location.