startEditing method

void startEditing(
  1. MeshElement element,
  2. String attributeName
)

Implementation

void startEditing(docs.MeshElement element, String attributeName) {
  Object? text = element.getAttribute(attributeName);
  _controller = TextEditingController(text: text == null ? null : "$text");

  if (text != null) {
    _controller!.selection = TextSelection(baseOffset: 0, extentOffset: "$text".length);
  }
  editingValue = _controller!.text;
  _editing = element;
  _editingAttribute = attributeName;
  _editingFocusNode?.dispose();
  _editingFocusNode = FocusNode(
    onKeyEvent: (_, evt) {
      if (evt.logicalKey == LogicalKeyboardKey.escape) {
        _editing = null;
        _editingAttribute = null;

        notifyListeners();

        return KeyEventResult.handled;
      } else if (evt.logicalKey == LogicalKeyboardKey.enter) {
        _editingFocusNode!.unfocus();

        _editing = null;
        _editingAttribute = null;

        notifyListeners();
      }
      return KeyEventResult.ignored;
    },
  );
  _editingFocusNode!.requestFocus();
  _editingFocusNode!.addListener(() {
    if (!_editingFocusNode!.hasFocus) {
      if (editingValue == "") {
        editingValue = null;
      }

      final prop = element.elementType.properties.firstWhere((x) => x.name == attributeName);

      if (editingValue == null) {
        element.setAttribute(attributeName, editingValue);
      } else {
        if (prop is ValueProperty) {
          if (prop.type == SimpleValue.string) {
            element.setAttribute(attributeName, editingValue);
          } else if (prop.type == SimpleValue.number) {
            element.setAttribute(attributeName, double.tryParse(editingValue.toString()));
          } else if (prop.type == SimpleValue.boolean) {
            if (editingValue == "true") {
              element.setAttribute(attributeName, true);
            } else if (editingValue == "false") {
              element.setAttribute(attributeName, false);
            }
          }
        }
      }

      _editing = null;
      _editingAttribute = null;

      notifyListeners();
    }
  });

  notifyListeners();
}