Ask the assistant
0/400
Your question is processed by an AI service and kept to improve answers. Do not enter personal data.

    Focus and keyboard

    Elements for working without a mouse: what has focus now, in what order Tab goes through fields, which key combinations do what, and what happens on the system "back" button. Also here is Actions : named actions that any descendant can call.

    Focus

    Publishes into a variable whether focus is on the child or somewhere inside it — in a field, on a button, on a link. This is how you highlight the block someone is typing in.

    Type: single-child

    Focus @isFocused:false
      DecoratedBox
        #decoration color:{isFocused ? 0xFFEEF2FF : 0xFFFFFFFF}
          #borderRadius circular 12
        TextField
          #decoration hintText:"Search"
    Parameter Type Default Description
    isFocused @ variable — Receives true while focus is inside and false when it leaves. Declared on the element itself: Focus @isFocused:false
    autofocus bool false Take focus when it appears. On the site the first field inside gets focus

    FocusScope

    A separate focus area: everything inside is traversed as one group, and the whole of it can be made unfocusable.

    Type: single-child

    FocusScope autofocus:true
      Column spacing:8
        TextField
          #decoration labelText:"Name"
        TextField
          #decoration labelText:"Email"
    Parameter Type Default Description
    autofocus bool false Take focus when it appears
    canRequestFocus bool true false — neither the area itself nor anything inside it gets focus
    skipTraversal bool false true — Tab skips the area itself (the elements inside stay reachable)

    FocusTraversalGroup

    Sets the order in which Tab goes through the elements inside.

    Type: single-child

    FocusTraversalGroup policy:ordered
      Column spacing:8
        FocusTraversalOrder order:2
          TextField
            #decoration labelText:"Second"
        FocusTraversalOrder order:1
          TextField
            #decoration labelText:"First"
    Parameter Type Default Description
    policy string readingOrder Order: readingOrder — as it reads (left to right, top to bottom); widgetOrder — as written in the markup; ordered (or numerical ) — by the FocusTraversalOrder numbers
    descendantsAreFocusable bool true false — nothing inside gets focus
    descendantsAreTraversable bool true false — Tab does not go inside (focus can still be set with a click)

    FocusTraversalOrder

    The child's number in the Tab order: a smaller number comes earlier. Works inside FocusTraversalGroup policy:ordered .

    Type: single-child

    @done:false
    FocusTraversalGroup policy:ordered
      Row spacing:8
        FocusTraversalOrder order:2
          FilledButton onPressed:{done = true}
            Text "Done"
        FocusTraversalOrder order:1
          SizedBox width:240
            TextField
              #decoration hintText:"Start here"
    Parameter Type Default Description
    order double 0 Position in the traversal; fractions are allowed ( 1.5 — between the first and the second)

    FocusableActionDetector

    Turns any block into a "keyboard button": it can be selected with Tab and pressed with Enter or Space, and also pressed with the mouse or a finger. Publishes whether the focus highlight is shown and whether the pointer is over it.

    Type: single-child

    @chosen:0
    FocusableActionDetector @isFocused:false @isHovered:false onPressed:{chosen = chosen + 1}
      DecoratedBox
        #decoration color:{isFocused || isHovered ? 0xFF4F46E5 : 0xFF36345E}
          #borderRadius circular 24
        Padding
          #padding symmetric horizontal:24 vertical:12
          Text "Choose"
            #style color:0xFFFFFFFF
    Parameter Type Default Description
    onPressed callback — A press: Enter, Space or a tap. Without it the block only takes focus
    isFocused @ variable — true while the focus highlight is shown — that is, when working from the keyboard; on a finger tap the highlight is usually not shown
    isHovered @ variable — true while the pointer is over the block
    autofocus bool false Take focus when it appears
    enabled bool true false — the block takes no focus and cannot be pressed

    Actions

    Declares named actions that any descendant can call — by name, without access to variables. The name is the parameter key in quotes, the handler is its value.

    Type: wrapper

    @sent:false
    Actions "form:submit":{sent = true}
      Form @email:""
        Column spacing:8
          TextFormField name:"email"
          FilledButton onPressed:"form:submit"
            Text "Send"

    The parameters are "name":{body} pairs, as many as you like. An action can be called with a string in a handler ( onPressed:"cart:clear" ) or with a function in a body ( onTap:{action("cart:add")} ).

    Why: a reaction has no button of its own. A form reports success with an action name, and without a handler for that name success can only be seen from its @ variables. An action that no Actions handled goes out — to the host app; on the site it becomes a reeel:action event on window .

    @count:0
    Actions "cart:add":{count = count + 1} "cart:clear":{count = 0}
      Column spacing:8
        Text $"In the cart: ${count}"
        Row spacing:8
          FilledButton onPressed:{action("cart:add")}
            Text "Add"
          OutlinedButton onPressed:"cart:clear"
            Text "Clear"

    CallbackShortcuts

    Keyboard shortcuts. The parameter name is the key combination in quotes, the value is what to do.

    Type: wrapper

    @saved:0
    CallbackShortcuts "ctrl+s":{saved = saved + 1} "meta+s":{saved = saved + 1} "escape":{saved = 0}
      Focus autofocus:true
        Text $"Saved times: ${saved}"

    A combination is modifiers and a key joined with + , case-insensitive:

    Part Values
    Modifiers meta / cmd , ctrl / control , alt / option , shift
    Keys letters a – z , enter , escape / esc , tab , space , backspace , up , down , left , right , f1 – f12

    Digits and punctuation are not in the list — such a combination does not fire. Modifiers are checked exactly: "ctrl+s" does not fire when ctrl+shift+s is pressed.

    • In the app shortcuts are heard only while focus is somewhere inside the element. So there must be something focusable inside — a field or Focus autofocus:true , as in the example.
    • On the site shortcuts are heard while focus is inside the element or nowhere (the page has just opened). While the person is typing in a field, combinations without modifiers on letters, Space, Backspace and the arrows are not intercepted — they stay input.
    • For Mac and the other systems write both variants: "meta+s" and "ctrl+s" .

    PopScope

    Controls the system "back" (button or gesture) on the screen it is placed in.

    Type: single-child

    PopScope canPop:false
      Center
        Text "Here “back” does not close the screen"
    Parameter Type Default Description
    canPop bool true false — the system "back" does not close the screen

    The element cannot tell the person why "back" did not work: there is no parameter for a handler.

    Differences between the app and the site

    Element On the site
    Focus Works: true while focus is on anything inside; autofocus — the first field inside
    FocusScope , FocusTraversalGroup , FocusTraversalOrder Only the child is drawn. Tab goes in markup order; policy and order are not read
    FocusableActionDetector Only the child is drawn: Tab does not select it, onPressed is not called, isFocused / isHovered do not change. For the site use InkWell — it takes focus and is pressed with Enter and Space
    Actions Works the same; an unhandled action is a reeel:action event
    CallbackShortcuts Works; the focus and input rules are above
    PopScope Only the child is drawn: it does not hold the browser's "back" button

    See also