xLayout_CellWidget_Editable method

Container xLayout_CellWidget_Editable(
  1. XCol xCol,
  2. dynamic item,
  3. BuildContext context,
  4. dynamic itemToSet, {
  5. void parentSetState(
    1. void ()
    )?,
  6. double? fixWidth,
  7. bool onBeforeCMD_Active = true,
  8. bool autoFocus = false,
  9. TextEditingController? controller,
})

Override per cambiare l'editor della Cella Editabile dell'Items

Implementation

Container xLayout_CellWidget_Editable(XCol xCol, dynamic item, BuildContext context, dynamic itemToSet, {void Function(void Function())? parentSetState, double? fixWidth, bool onBeforeCMD_Active = true, bool autoFocus = false, TextEditingController? controller}) {
  late Widget _widgetForCell;
  // FocusNode focusNode = FocusNode();

  //WIDGET NEL CASO FOSSE UNA STRING
  if (xCol.dataType == String && xCol.colKey.startsWith("li").not() && xCol.dataType != bool) {
    _widgetForCell = FocusScope(
        onKeyEvent: widget.isEditable
            ? (node, event) {
                //TODO Tab problem
                if (event is KeyDownEvent && event.logicalKey.keyLabel == LogicalKeyboardKey.tab.keyLabel) {
                  print("Tab premuto");
                  FocusScope.of(context).nextFocus();
                  return KeyEventResult.handled; // Indica che l'evento è stato gestito
                } else {
                  return KeyEventResult.skipRemainingHandlers; // Indica che l'evento non è stato gestito
                }
              }
            : null,
        onFocusChange: (value) async => value.not()
            ? (widget.schedaMode ?? false)
                ? item is K
                    ? widget.rootItemEdited!(item)
                    : widget.itemEdited!(item as XFDataItem)
                : null
            : null,
        child: xLayout_TextFormField_Builder(
          height: null,
          context: context,
          controller: controller,
          xCol: xCol,
          autoFocus: autoFocus,
          // focusNode: focusNode,
          multiLines: (fixWidth ?? xCol.width) >= 200 ? true : false,
          editable: xCol.readOnly,
          labelColor_Disabled: xCellEditable_labelColor_Disabled,
          borderColor: xCellEditable_borderColor,
          borderColor_Disabled: xCellEditable_borderColor_Disabled,
          onSubmitted: (value) => xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, value),
          value: item[xCol.colKey] ?? "",
          onChanged: (value) => isWeb
              ? xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, value)
              : onBeforeCMD_Active.not()
                  ? item[xCol.colKey] = value
                  : xOnXCell_Edit_OnChanged(item, xCol, value),
          onBefore_Edit: () {
            if ((changed_Data || widget.edited!.value) && (widget.schedaMode ?? false)) {
              widget.rootItemEdited!(item as K);
            }
          },
        ));
  }
  //WIDGET NEL CASO FOSSE UN DATETIME
  else if (xCol.dataType == DateTime) {
    _widgetForCell = xLayout_DateFormField_Builder(
      xCol: xCol,
      borderColor: xCellEditable_borderColor,
      borderColor_Disabled: xCellEditable_borderColor_Disabled,
      labelColor_Disabled: xCellEditable_labelColor_Disabled,
      format: xCellEditable_xFormat_Date,
      onBefore_Edit: () {
        if ((changed_Data || widget.edited!.value) && (widget.schedaMode ?? false)) {
          widget.rootItemEdited!(item as K);
        }
      },
      // onBefore_Edit: () => xOnXCell_Before_Edit(xCol, item),
      value: item[xCol.colKey] == DateTime(1900, 01, 01) ? null : item[xCol.colKey],
      onDateSelected: (value) {
        if (widget.schedaMode ?? false) {
          widget.rootItemEdited!(item as K);
        }
        onBeforeCMD_Active ? item[xCol.colKey] = value : xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, value);
      },
    );
  }
  //WIDGET NEL CASO FOSSE UN DOUBLE
  else if (xCol.dataType == double) {
    // Definisci la RegExp che permette numeri interi e decimali con punto o virgola
    final RegExp doubleRegExp = RegExp(r'^(\d+[\.,]?\d*|\d*[\.,]?\d+)?$');

// Crea il FilteringTextInputFormatter usando la RegExp definita
    final FilteringTextInputFormatter doubleInputFormatter = FilteringTextInputFormatter.allow(doubleRegExp);

    _widgetForCell = FocusScope(
        onKeyEvent: widget.isEditable
            ? (node, event) {
                if (event is KeyDownEvent && event.logicalKey.keyLabel == LogicalKeyboardKey.tab.keyLabel) {
                  print("Tab premuto");
                  (widget.schedaMode ?? false) ? widget.rootItemEdited!(item as K) : null;
                  FocusScope.of(context).nextFocus();
                  return KeyEventResult.handled; // Indica che l'evento è stato gestito
                } else {
                  return KeyEventResult.skipRemainingHandlers; // Indica che l'evento non è stato gestito
                }
              }
            : null,
        child: xLayout_TextFormField_Builder(
            context: context,
            selectText: true,
            editable: xCol.readOnly,
            autoFocus: autoFocus,
            xCol: xCol,
            height: null,
            controller: controller,
            maxLines: 1,
            borderColor: xCellEditable_borderColor,
            borderColor_Disabled: xCellEditable_borderColor_Disabled,
            labelColor_Disabled: xCellEditable_labelColor_Disabled,
            onBefore_Edit: () {
              if ((changed_Data || widget.edited!.value) && (widget.schedaMode ?? false)) {
                widget.rootItemEdited!(item as K);
              }
            },
            keyboardType: TextInputType.numberWithOptions(decimal: true),
            value: (item[xCol.colKey] == null || item[xCol.colKey].toString() == "0.0")
                ? ""
                : xCol.readOnly
                    ? XUtils.xFormatDouble(context, item[xCol.colKey]).trim()
                    : item[xCol.colKey].toString(),
            // inputFormatters: [FilteringTextInputFormatter.allow(RegExp("[.,\\\\]"))],
            inputFormatters: [doubleInputFormatter],
            onSubmitted: (value) => xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, double.parse(value)),
            onChanged: (value) {
              if (value != "") {
                isWeb
                    ? xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, double.parse(value))
                    : onBeforeCMD_Active.not()
                        ? item[xCol.colKey] = double.parse(value)
                        : xOnXCell_Edit_OnChanged(item, xCol, double.parse(value));
              } else {
                isWeb
                    ? xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, 0.0)
                    : onBeforeCMD_Active.not()
                        ? item[xCol.colKey] = 0.0
                        : xOnXCell_Edit_OnChanged(item, xCol, 0.0);
              }
            }));
  }
  //WIDGET NEL CASO FOSSE UN INTERO
  else if (xCol.dataType == int) {
    _widgetForCell = FocusScope(
        onKeyEvent: widget.isEditable
            ? (node, event) {
                if (event is KeyDownEvent && event.logicalKey.keyLabel == LogicalKeyboardKey.tab.keyLabel) {
                  print("Tab premuto");
                  (widget.schedaMode ?? false) ? widget.rootItemEdited!(item as K) : null;
                  FocusScope.of(context).nextFocus();
                  return KeyEventResult.handled; // Indica che l'evento è stato gestito
                } else {
                  return KeyEventResult.skipRemainingHandlers; // Indica che l'evento non è stato gestito
                }
              }
            : null,
        child: xLayout_TextFormField_Builder(
          context: context,
          editable: xCol.readOnly,
          selectText: true,
          maxLines: 1,
          controller: controller,
          autoFocus: autoFocus,
          xCol: xCol,
          height: null,
          borderColor: xCellEditable_borderColor,
          borderColor_Disabled: xCellEditable_borderColor_Disabled,
          labelColor_Disabled: xCellEditable_labelColor_Disabled,
          keyboardType: TextInputType.number,
          onBefore_Edit: () {
            if ((changed_Data || widget.edited!.value) && (widget.schedaMode ?? false)) {
              widget.rootItemEdited!(item as K);
            }
          },
          onSubmitted: (value) => xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, value),
          value: item[xCol.colKey].toString() == "0.0" ? "" : item[xCol.colKey].toString(),
          inputFormatters: [FilteringTextInputFormatter.deny(RegExp("[- /,.\\\\]"))],
          // inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}'))],
          onChanged: (value) {
            if (value != "") {
              isWeb
                  ? xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, int.parse(value))
                  : onBeforeCMD_Active.not()
                      ? item[xCol.colKey] = int.parse(value)
                      : xOnXCell_Edit_OnChanged(item, xCol, int.parse(value));
            } else {
              isWeb
                  ? xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, 0)
                  : onBeforeCMD_Active.not()
                      ? item[xCol.colKey] = 0
                      : xOnXCell_Edit_OnChanged(item, xCol, 0);
            }
          },
        ));
  } else if (xCol.dataType == bool) {
    _widgetForCell = Container(
      child: XCheckBoxWidget(
          verticalCheckBoxWidget: false,
          label: xCol.colCaption,
          label_Color: xCol.xmodelXprop.col_Color != Colors.black ? xCol.xmodelXprop.col_Color : XColors.foregroundLight,
          activeColor: xCol.xmodelXprop.col_Color != Colors.black ? xCol.xmodelXprop.col_Color : XColors.foregroundLight,
          checkColor: XColors.foregroundDark,
          mainAxisAlignment_Label_HorizontalMode: MainAxisAlignment.start,
          value: (item[xCol.colKey] ?? false),
          onTap: () async {
            parentSetState != null
                ? parentSetState(() {
                    item[xCol.colKey] = (item[xCol.colKey] as bool).not();
                    xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, item[xCol.colKey]);
                  })
                : setState(() {
                    item[xCol.colKey] = (item[xCol.colKey] as bool).not();
                    xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, item[xCol.colKey]);
                  });
          },
          onChanged: (value) async {
            parentSetState != null
                ? parentSetState(() {
                    xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, value);
                    item[xCol.colKey] = value;
                  })
                : setState(() {
                    xOnXCell_Edit_SubmitValue_INTERNAL(item, xCol, value);
                    item[xCol.colKey] = value;
                  });
          }),
    );
  } else {
    //DEVE essere W e H = 0 se no il Wrap fa casino nel gestirlo e va a "un po' a capo" dopo l'ultimo Widget
    return Container(width: 0, height: 0);
  }
  if (li != null && li!.isNotEmpty) {
    var res = li!.last.originY != xCol.originY;
    if (res) {
      li = (widget.view ?? widget.viewDetts!).cols.where((element) => element.originY == xCol.originY).toList();
    }
  } else {}
  if (li != null) {
    li!.sort((a, b) => Comparable.compare((a.visPos), (b.visPos)));
    li!.forEach((element) {
      if (xCol.resizeStyle == 0) {
        widthCols = widthCols + ((MediaQuery.of(context).size.width - 30) - widthCols);
      } else {
        widthCols = widthCols + element.width;
      }
      if (li!.lastWhereOrNull((element) => element.colKey == xCol.colKey) != null) {
        widthCols = 0.0;
      }
    });
  }

  return Container(
    width: (fixWidth ?? xCol.width) + (xCol.dataType == DateTime && widget.isEditable ? 40 : 0),
    margin: EdgeInsets.only(bottom: widget.isEditable ? 5 : 0, left: widget.isEditable ? 2 : 0, right: widget.isEditable ? 2 : 0),
    padding: xCol.dataType == DateTime ? EdgeInsets.only(top: (widget.isEditable ? 8 : 1) * (XUtils.kforScale)) : null,
    child: _widgetForCell,
  );
}