createHtmlElement method

  1. @override
WebHTMLElement? createHtmlElement()
override

Creates and returns a WebHTMLElement to represent this object in the DOM.

Returns null if no element should be created.

Implementation

@override
WebHTMLElement? createHtmlElement() {
  if (!kIsWeb) return null;

  final document = webWindow.document;
  late WebHTMLElement element;

  // Create textarea for multiline, otherwise input element.
  if (effectiveInputType == InputType.textarea) {
    element = document.createElement('textarea') as WebHTMLElement;
    if (maxLines != null) {
      element.setAttribute('rows', '$maxLines');
    }
  } else {
    element = document.createElement('input') as WebHTMLElement;
    final htmlInputType = _mapKeyboardTypeToHtmlInputType();
    element.setAttribute('type', htmlInputType);
  }

  element.id = id ?? _generateRandomId();

  if (placeholder != null) {
    element.setAttribute('placeholder', placeholder!);
  }

  if (controller != null && controller!.text.isNotEmpty) {
    element.setAttribute('value', controller!.text);
  }

  return element;
}