focus method Null safety

  1. @override
void focus(
  1. [String name = '']
)
override

Sets focus to a child control.

The argument name is a dot-delimited string that define the path to the control.

Example:

Focus a child control by name.

final form = fb.group({'name': ''});

// UI text field get focus and the device keyboard pop up
form.focus('name');

Focus a nested child control by path.

final form = fb.group({
  'person': fb.group({
    'name': '',
  })
});

// UI text field get focus and the device keyboard pop up
form.focus('person.name');

Implementation

@override
void focus([String name = '']) {
  if (name.isNotEmpty) {
    final control = findControlInCollection(name.split('.'));
    if (control != null) {
      control.focus();
    }
  } else if (_controls.isNotEmpty) {
    _controls.values.first.focus();
  }
}