buildDropDown method

dynamic buildDropDown()

Implementation

buildDropDown() {
  var mqc = MediaQuery.of(context);
  changeSize(context);
  return FocusScope(
      onKeyEvent: (node, event) {
        if (event.physicalKey == PhysicalKeyboardKey.escape) {
          if (node.hasFocus) {
            node.unfocus();
          } else {}
          return KeyEventResult.handled;
        } else {
          return KeyEventResult.ignored;
        }
      },
      onFocusChange: (value) {
        if (value) {
          if (widget.items!.any((element) => element == textController.text)) {
          } else {
            if (selectedItem_UI != null) {
              if (widget.items!.any((element) => element == textController.text).not()) {
                if (T == XEnumDef) {
                  textController.text = (selectedItem_UI as XEnumDef).label;
                } else if (T == String || T == double || T == int) {
                  textController.text = selectedItem_UI.toString();
                } else {
                  textController.text = widget.itemAsString!(selectedItem_UI as T).toString();
                }
              }
            } else {
              clearDDValue();
            }
          }
        } else {}
      },
      child: DropdownMenu<T>(
        width: widget.width,
        enableFilter: false,
        initialSelection: widget.initialValue,
        textStyle: widget.item_TextStyle != null && selectedItem_UI != null
            ? widget.item_TextStyle!(selectedItem_UI ?? widget.initialValue!)!
            : XStyles.xStyTextForLabel(
                fontSize: widget.fontSize_All,
                textColor: (ThemeData.estimateBrightnessForColor((T == XEnumDef)
                            ? selectedItem_UI != null
                                ? (selectedItem_UI as XEnumDef).textColor
                                : widget.item_BackgroundColor ?? widget.backgroundColor
                            : widget.backgroundColor)) ==
                        Brightness.dark //
                    ? XColors.foregroundLight
                    : XColors.foregroundDark,
                activeBold: false),
        controller: textController,
        requestFocusOnTap: widget.searchable ?? (T == XEnumDef) ? false : true,
        enableSearch: widget.searchable ?? (T == XEnumDef) ? false : true,
        menuHeight: mqc.size.height * KforScale,
        menuStyle: MenuStyle(backgroundColor: MaterialStatePropertyAll(Colors.grey[900])),
        trailingIcon: Icon(Icons.arrow_downward_outlined, size: widget.fontSize_All, color: getColoFor_TextAndDecoration(value: selectedItem_UI)),
        leadingIcon: selectedItem_UI == null
            ? null //
            : IconButton(padding: EdgeInsets.all(4), onPressed: () => clearDDValue(), icon: Icon(Icons.clear, size: widget.fontSize_All, color: getColoFor_TextAndDecoration(value: selectedItem_UI))),
        label: widget.label != null && widget.label != "" ? Text(widget.label ?? "", style: get_LabelStyle) : null,
        inputDecorationTheme: InputDecorationTheme(
          filled: true,
          iconColor: getColoFor_TextAndDecoration(value: selectedItem_UI),
          suffixStyle: get_LabelStyle,
          hintStyle: get_LabelStyle,
          suffixIconColor: getColoFor_TextAndDecoration(value: selectedItem_UI),
          contentPadding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 2),
          labelStyle: get_LabelStyle,
          floatingLabelStyle: get_LabelStyle,
          fillColor: selectedItem_UI == null
              ? widget.backgroundColor
              : T == XEnumDef
                  ? (selectedItem_UI as XEnumDef).textColor
                  : widget.item_BackgroundColor,
        ),
        onSelected: (value) {
          setState(() => selectedItem_UI = value);
          // widget.backgroundColor = widget.item_BackgroundColor ?? Colors.grey[900]!;
          widget.onChange(value);
        },
        hintText: "",
        dropdownMenuEntries: widget.items != null && widget.items!.isNotEmpty
            ? widget.items!
                .where((element) => element != null)
                .map((e) => (e is XEnumDef)
                    //Se l'XDD รจ un XEnum
                    ? DropdownMenuEntry<T>(
                        value: e,
                        label: e.label,
                        style: ButtonStyle(
                          backgroundColor: MaterialStatePropertyAll(Colors.grey[900]),
                          overlayColor: MaterialStatePropertyAll(Colors.black.withAlpha(30)),
                          foregroundColor: MaterialStatePropertyAll(widget.item_TextStyle != null && widget.item_TextStyle!(e)!.color != null ? widget.item_TextStyle!(e)!.color : e.textColor),
                          textStyle: MaterialStatePropertyAll(get_Item_TextStyle(e)),
                        ))
                    //Se l'XDD generico
                    : DropdownMenuEntry<T>(
                        value: e!,
                        label: widget.itemAsString != null ? widget.itemAsString!(e) ?? "" : e.toString(),
                        style: ButtonStyle(
                          overlayColor: MaterialStatePropertyAll(Colors.black.withAlpha(60)),
                          foregroundColor: MaterialStatePropertyAll(widget.item_TextStyle != null && widget.item_TextStyle!(e)!.color != null
                              ? widget.item_TextStyle!(e)!.color
                              : (ThemeData.estimateBrightnessForColor(get_Item_BackgroundColor(e))) == Brightness.dark //
                                  ? XColors.foregroundLight
                                  : XColors.foregroundDark),
                          backgroundColor: MaterialStatePropertyAll(get_Item_BackgroundColor(e)),
                          textStyle: MaterialStatePropertyAll(get_Item_TextStyle(e)),
                        )))
                .toList()
            : [],
      ));
}