DropDownField constructor

DropDownField({
  1. Key? key,
  2. TextEditingController? controller,
  3. dynamic value,
  4. bool required = false,
  5. Widget? icon,
  6. String? hintText,
  7. TextStyle hintStyle = const TextStyle(fontWeight: FontWeight.normal, color: Colors.grey, fontSize: 18.0),
  8. String? labelText,
  9. TextStyle labelStyle = const TextStyle(fontWeight: FontWeight.normal, color: Colors.grey, fontSize: 18.0),
  10. List<TextInputFormatter>? inputFormatters,
  11. List? items,
  12. TextStyle textStyle = const TextStyle(fontWeight: FontWeight.bold, color: Colors.black, fontSize: 14.0),
  13. FormFieldSetter? setter,
  14. ValueChanged? onValueChanged,
  15. int itemsVisibleInDropdown = 3,
  16. bool enabled = true,
  17. bool strict = true,
})

Implementation

DropDownField(
    {Key? key,
    this.controller,
    this.value,
    this.required: false,
    this.icon,
    this.hintText,
    this.hintStyle: const TextStyle(
        fontWeight: FontWeight.normal, color: Colors.grey, fontSize: 18.0),
    this.labelText,
    this.labelStyle: const TextStyle(
        fontWeight: FontWeight.normal, color: Colors.grey, fontSize: 18.0),
    this.inputFormatters,
    this.items,
    this.textStyle: const TextStyle(
        fontWeight: FontWeight.bold, color: Colors.black, fontSize: 14.0),
    this.setter,
    this.onValueChanged,
    this.itemsVisibleInDropdown: 3,
    this.enabled: true,
    this.strict: true})
    : super(
        key: key,
        initialValue: controller != null ? controller.text : (value ?? ''),
        onSaved: setter,
        builder: (FormFieldState<String> field) {
          final DropDownFieldState state = field as DropDownFieldState;
          final ScrollController _scrollController = ScrollController();
          final InputDecoration effectiveDecoration = InputDecoration(
              border: InputBorder.none,
              filled: true,
              icon: icon,
              suffixIcon: IconButton(
                  icon: Icon(Icons.arrow_drop_down,
                      size: 30.0, color: Colors.black),
                  onPressed: () {
                    SystemChannels.textInput.invokeMethod('TextInput.hide');
                    state.setState(() {
                      state._showdropdown = !state._showdropdown;
                    });
                  }),
              hintStyle: hintStyle,
              labelStyle: labelStyle,
              hintText: hintText,
              labelText: labelText);

          return Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: TextFormField(
                      // autovalidate: true,
                      controller: state._effectiveController,
                      decoration: effectiveDecoration.copyWith(
                          errorText: field.errorText),
                      style: textStyle,
                      textAlign: TextAlign.start,
                      autofocus: false,
                      obscureText: false,
                      //     maxLengthEnforced: true,
                      maxLines: 1,
                      validator: (String? newValue) {
                        if (required) {
                          if (newValue == null || newValue.isEmpty)
                            return 'This field cannot be empty!';
                        }

                        //Items null check added since there could be an initial brief period of time
                        //when the dropdown items will not have been loaded
                        if (items != null) {
                          if (strict &&
                              newValue!.isNotEmpty &&
                              !items.contains(newValue))
                            return 'Invalid value in this field!';
                        }

                        return null;
                      },
                      onSaved: setter,
                      enabled: enabled,
                      inputFormatters: inputFormatters,
                    ),
                  ),
                  IconButton(
                    icon: Icon(Icons.close),
                    onPressed: () {
                      if (!enabled) return;
                      state.clearValue();
                    },
                  )
                ],
              ),
              !state._showdropdown
                  ? Container()
                  : Container(
                      alignment: Alignment.topCenter,
                      height: itemsVisibleInDropdown *
                          48.0, //limit to default 3 items in dropdownlist view and then remaining scrolls
                      width: MediaQuery.of(field.context).size.width,
                      child: ListView(
                        cacheExtent: 0.0,
                        scrollDirection: Axis.vertical,
                        controller: _scrollController,
                        padding: EdgeInsets.only(left: 40.0),
                        children: items!.isNotEmpty
                            ? ListTile.divideTiles(
                                    context: field.context,
                                    tiles: state._getChildren(state._items!))
                                .toList()
                            : [],
                      ),
                    ),
            ],
          );
        },
      );