flutter 自定义DropDown按钮上的验证程序

sr4lhrrt  于 2022-12-19  发布在  Flutter
关注(0)|答案(1)|浏览(118)

我创建自己自定义DropDownField:

class DropMenu extends StatefulWidget {
  String lableText;
  int selectvalueindex;
  DropMenuMode menuMode;
  var model;
  bool isSelected = false;
  final String Function(String) validator;
  DropMenu({this.lableText, this.selectvalueindex, this.menuMode, this.isSelected, this.model, this.validator});

  @override
  _DropMenuState createState() => _DropMenuState(
      lableText: lableText,
      selectvalueindex: selectvalueindex,
      menuMode: menuMode,
      isSelected: isSelected,
      model: model,
      validator: validator,
  );
}

class _DropMenuState extends State<DropMenu> {
  String lableText;
  String titleText;
  int selectvalueindex;
  DropMenuMode menuMode;
  bool isSelected = false;
  final String Function(String) validator;
  var model;
  TextStyle lableStyle = dropDownLableGrey;
  _DropMenuState({this.lableText, this.selectvalueindex, this.menuMode, this.isSelected, this.model, this.validator});

  @override
  initState(){
    if(menuMode == DropMenuMode.countries){
      titleText = 'Land';
    }else{
    titleText = lableText;
    }
    if(isSelected == true){
      isSelected = true;
      lableStyle = dropDownLableBlack;
    }
    super.initState();

  }

  List<String> parseMap(List<dynamic> dataMap) {
    List<String> finalParseList = [];
    if (menuMode == DropMenuMode.countries) {
      for (var item in dataMap) {
        for (var mapItem in item.entries) {
          if (mapItem.key == "de") {
            finalParseList.add(mapItem.value);
          }
        }
      }
    } else if (menuMode == DropMenuMode.genders) {
      for (var item in dataMap) {
        for (var mapItem in item.entries) {
          if (mapItem.key == "name") {
            finalParseList.add(mapItem.value);
          }
        }
      }
    } else if (menuMode == DropMenuMode.branche) {
      for (var item in dataMap) {
        for (var mapItem in item.entries) {
          if (mapItem.key == "name") {
            finalParseList.add(mapItem.value);
          }
        }
      }
    } else if (menuMode == DropMenuMode.funktion) {
      for (var item in dataMap) {
        for (var mapItem in item.entries) {
          if (mapItem.key == "name") {
            finalParseList.add(mapItem.value);
          }
        }
      }
    }
    return finalParseList;
  }

  SetGlobalRequaerData(String result, List<dynamic> dataMap) {
    bool isfinde = false;
    for (Map item in dataMap) {
      if (isfinde == true){
        break;
      }
      if (item.containsValue(result)) {
        if (menuMode == DropMenuMode.countries) {
          for (var mapData in item.entries) {
            if (mapData.key == 'country_code'){
              model.selectedDropMenu[selectvalueindex] = mapData.value;
              isfinde = true;

              break;
            }
          }
        }else if (menuMode == DropMenuMode.genders) {
          for (var mapData in item.entries) {
            if (mapData.key == 'name'){
              model.selectedDropMenu[selectvalueindex] = mapData.value;
              isfinde = true;
              break;
            }
          }
        }else if (menuMode == DropMenuMode.branche) {
          for (var mapData in item.entries) {
            if (mapData.key == 'id'){
              model.selectedDropMenu[selectvalueindex] = mapData.value;
              isfinde = true;
              break;
            }
          }
        }else if (menuMode == DropMenuMode.funktion) {
          for (var mapData in item.entries) {
            if (mapData.key == 'id'){
              model.selectedDropMenu[selectvalueindex] = mapData.value;
              isfinde = true;
              break;
            }
          }
        }
      }
    }

  }

  void _popUpModalBottomSheet(BuildContext context) async {
    List<String> finalDataList = [];

    List<dynamic> dataMap = await apiRegistrationDataList(menuMode);
    finalDataList = parseMap(dataMap);
    final result = await showModalBottomSheet(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(9.0),
        ),
        isScrollControlled: true,
        context: context,
        builder: (context) => DialogMenu(
              title: titleText,
              data: finalDataList,
            ));
    setState(() {
      if(result == null){
        lableText = lableText;
      }else{
        lableText = result ?? lableText;
        lableStyle = dropDownLableBlack;
      }

      SetGlobalRequaerData(result, dataMap);
    });
  }

  @override
  Widget build(BuildContext context) {
    return FormField(
      autovalidateMode: AutovalidateMode.always,
      validator: (value) {
        print("Bitte fülle das Feld aus");
        return null;
      },
      builder: (FormFieldState state) {
        return InputDecorator(
          decoration: InputDecoration(
            contentPadding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
            border: OutlineInputBorder(
                borderRadius: const BorderRadius.all(Radius.circular(4)),
                borderSide: BorderSide(color: GeneralStyles.greyColor.withOpacity(0.5), width: .5)),
            enabledBorder:const OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(4)),
                borderSide: BorderSide(color: Color(0xFF707070), width: .5)),
            focusedBorder: OutlineInputBorder(
                borderRadius: const BorderRadius.all(Radius.circular(4)),
                borderSide: BorderSide(color: GeneralStyles.blackColor, width: 1)),
            errorBorder:const OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(4)),
              borderSide: BorderSide(color: Colors.red, width: .5),
            ),

          ),
          child: DropdownButtonHideUnderline(
              child: GestureDetector(
            onTap: () => _popUpModalBottomSheet(context),
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 15),
              child: Row(
                //  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  SizedBox(
                    width: 285,
                    child: Text(
                      lableText,
                      maxLines: 1,
                      overflow: TextOverflow.clip,
                      softWrap: false,
                      style: lableStyle,
                    ),
                  ),
                  const Spacer(),
                  Icon(Icons.keyboard_arrow_down,color: greyColor,),
                ],
              ),
            ),
          )),
        );
      },
    );
  }
}

我想验证它,但我真的不明白如何使它。当我使用这个小部件在我的形式,我写验证函数,当我点击按钮,它应该是工作,但这个字段不验证,并没有redlable字段下。
请帮助我验证此小部件

soat7uwm

soat7uwm1#

我建议你试试this Widget
我不确定您的自定义实现如何,但是如果您希望简化验证,我建议您尝试this package

相关问题