dropDownWidget static method

Widget dropDownWidget(
  1. BuildContext context,
  2. String hintText,
  3. dynamic value,
  4. List lstData,
  5. Function onChanged,
  6. Function onValidate, {
  7. double hintFontSize = 15,
  8. Color borderColor = Colors.redAccent,
  9. double borderRadius = 30,
  10. Color borderFocusColor = Colors.redAccent,
  11. double paddingLeft = 20,
  12. double paddingRight = 20,
  13. double paddingTop = 0,
  14. double paddingBottom = 0,
  15. String optionValue = "id",
  16. String optionLabel = "name",
  17. double contentPadding = 6,
  18. Color validationColor = Colors.redAccent,
  19. Color textColor = Colors.black,
  20. Color hintColor = Colors.black,
  21. double borderWidth = 2,
  22. double focusedBorderWidth = 2,
  23. double enabledBorderWidth = 1,
  24. Widget? suffixIcon,
  25. Icon? prefixIcon,
  26. bool showPrefixIcon = false,
  27. Color prefixIconColor = Colors.redAccent,
  28. double prefixIconPaddingLeft = 30,
  29. double prefixIconPaddingRight = 10,
  30. double prefixIconPaddingTop = 0,
  31. double prefixIconPaddingBottom = 0,
})

Implementation

static Widget dropDownWidget(
  BuildContext context,
  String hintText,
  dynamic value,
  List<dynamic> lstData,
  Function onChanged,
  Function onValidate, {
  double hintFontSize = 15,
  Color borderColor = Colors.redAccent,
  double borderRadius = 30,
  Color borderFocusColor = Colors.redAccent,
  double paddingLeft = 20,
  double paddingRight = 20,
  double paddingTop = 0,
  double paddingBottom = 0,
  String optionValue = "id",
  String optionLabel = "name",
  double contentPadding = 6,
  Color validationColor = Colors.redAccent,
  Color textColor = Colors.black,
  Color hintColor = Colors.black,
  double borderWidth = 2,
  double focusedBorderWidth = 2,
  double enabledBorderWidth = 1,
  Widget? suffixIcon,
  Icon? prefixIcon,
  bool showPrefixIcon = false,
  Color prefixIconColor = Colors.redAccent,
  double prefixIconPaddingLeft = 30,
  double prefixIconPaddingRight = 10,
  double prefixIconPaddingTop = 0,
  double prefixIconPaddingBottom = 0,
}) {
  if (value != "") {
    var findValue = lstData
        .where((item) => item[optionValue].toString() == value.toString());

    if (findValue.length > 0) {
      value = findValue.first[optionValue].toString();
    } else {
      value = null;
    }
  }

  return Padding(
    padding: EdgeInsets.only(
      left: paddingLeft,
      right: paddingRight,
      top: paddingTop,
      bottom: paddingBottom,
    ),
    child: FormField<dynamic>(
      builder: (FormFieldState<dynamic> state) {
        return DropdownButtonFormField<String>(
          isExpanded: true,
          value: value != "" ? value : null,
          isDense: true,
          hint: Text(
            hintText,
            style: TextStyle(
              fontSize: hintFontSize,
            ),
          ),
          decoration: InputDecoration(
            contentPadding: EdgeInsets.all(contentPadding),
            errorStyle: TextStyle(
              color: validationColor,
            ),
            hintStyle: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: hintFontSize,
              color: hintColor,
            ),
            hintText: hintText,
            enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(borderRadius),
              borderSide: BorderSide(
                color: borderColor,
                width: enabledBorderWidth,
              ),
            ),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(borderRadius),
              borderSide: BorderSide(
                color: borderColor,
                width: borderWidth,
              ),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(
                color: borderFocusColor,
                width: focusedBorderWidth,
              ),
              borderRadius: BorderRadius.circular(borderRadius),
            ),
            suffixIcon: suffixIcon,
            prefixIcon: showPrefixIcon
                ? Padding(
                    child: IconTheme(
                      data: IconThemeData(color: prefixIconColor),
                      child: prefixIcon!,
                    ),
                    padding: EdgeInsets.only(
                      left: prefixIconPaddingLeft,
                      right: prefixIconPaddingRight,
                      top: prefixIconPaddingTop,
                      bottom: prefixIconPaddingBottom,
                    ),
                  )
                : null,
          ),
          // decoration: InputDecoration(
          //   contentPadding: EdgeInsets.all(contentPadding),
          //   hintStyle: TextStyle(
          //     fontWeight: FontWeight.bold,
          //     fontSize: hintFontSize,
          //   ),
          //   enabledBorder: OutlineInputBorder(
          //     borderRadius: BorderRadius.circular(borderRadius),
          //     borderSide: BorderSide(
          //       color: borderColor,
          //       width: 1,
          //     ),
          //   ),
          //   border: OutlineInputBorder(
          //     borderRadius: BorderRadius.circular(borderRadius),
          //     borderSide: BorderSide(
          //       color: borderColor,
          //       width: 2,
          //     ),
          //   ),
          //   focusedBorder: OutlineInputBorder(
          //     borderSide: BorderSide(
          //       color: borderFocusColor,
          //       width: 2.0,
          //     ),
          //     borderRadius: BorderRadius.circular(borderRadius),
          //   ),
          // ),
          onChanged: (newValue) {
            //  FocusScope.of(context).requestFocus(new FocusNode());
            state.didChange(newValue);
            return onChanged(newValue);
          },
          validator: (value) {
            return onValidate(value);
          },
          items: lstData.map<DropdownMenuItem<String>>(
            (dynamic data) {
              return DropdownMenuItem<String>(
                value: data[optionValue].toString(),
                child: new Text(
                  data[optionLabel],
                  style: new TextStyle(color: Colors.black, fontSize: 13),
                ),
              );
            },
          ).toList(),
        );
      },
    ),
  );
}