简体   繁体   English

需要验证的颤动下拉菜单

[英]flutter drop down menu that needs to be validated

I am working on "flutter" specifically validating the dropdown menu within the api link, I came up with what you can see below, it's multiple of ideas in one, plus the fact that I don't have time, so please can anyone tell me how can I solve it.我正在研究“颤振”,专门验证 api 链接中的下拉菜单,我想出了你在下面看到的,它是多个想法,再加上我没有时间,所以请任何人告诉我该如何解决。

  >     > _validateForm(String? opName1) {
    >     > 
    >     >     if (opName1 == null) {
    >     >       return
    >     >         "please recheck";
    >     >     }
    >     >     else (_validate) {
    >     >       //form is valid
    >     >     }; void initState() {
    >     >     super.initState();
    >     >     _validateForm(opName1);
    >     > 
    >     > The opName is what we get the api info with , opNam1 is what it is
    >     > assigned to and opNameData is the full list in which the api fills it.
    >     > 
    >     > child: DropdownButtonHideUnderline(
    >     > 
    >     >                               child: DropdownButton(
    >     >                                 hint: Text("choose a operation",
    >     >                                   style: TextStyle(
    >     >                                       fontSize: 20.0,
    >     >                                       color: Colors.black54,
    >     >                                       fontWeight: FontWeight.bold ,
    >     >                                       inherit: _validate = false || true
    >     > 
    >     >                                   ) ,
    >     >                                 ),
    >     >                                 alignment: AlignmentDirectional.topCenter,
    >     >                                 menuMaxHeight: 300,
    >     >                                 dropdownColor: Colors.grey.shade300,
    >     >                                 borderRadius: BorderRadius.all(Radius.circular(15.0)),
    >     >                                 items: opNameData.map((item) {
    >     >                                 return DropdownMenuItem(
    >     >                                       child: Center(
    >     >                                       child: Text(
    >     >                                       item['OpName'].toString().trim(),    //Names that the api dropdown
    >     > contains
    >     >                                   style: const TextStyle(
    >     >                                   fontSize: 20.0,
    >     >                                   fontWeight: FontWeight.bold
    >     >                                   ), semanticsLabel: _validate ? 'please fill' : null,
    >     > 
    >     >                             textAlign : TextAlign.end,
    >     >                                       ),
    >     >                                       ),
    >     >                                       value: item['OpName'].toString().trim(),
    >     >                                   );
    >     >                                 }).toList(),
    >     >                                    onChanged: (String? newVal) {
    >     >                                      setState(() {
    >     >                                   opName = newVal.toString();
    >     >                                   opName1 = opName?.trim();
    >     >                                   print(opName1.toString());
    >     >                                   //opName1 = newVal;
    >     > 
    >     >                                   });
    >     >                                   },
    >     >                               value: opName,                 //passing the default id that has to be viewed
    >     >                          ),
    >     >                                  ),
    >     >                         ),

I want a text to show to tell the user to chose from the dropdown because i always get the error that null can't be assigned to subtype string. it doesn't print any sentence I want it to when it shows errors.
the button code:
                           onPressed: () {
                                 setState(() {
      if(opValue == null || opName == '')
                                    {                return 'please fill';

it has other details but that's what I have been stuck on for days, especially with the fact that I wanna keep the job and I have no mentor.它还有其他细节,但这就是我几天来一直坚持的事情,尤其是考虑到我想保住这份工作而且我没有导师。 I am learning by what I can.我正在尽我所能学习。 I am almost sure I came so close to doing sth right that shows my abilities specially since I get alot of "u are too slow because I have been studying for almost 4 months, I think I am doing great actually.我几乎可以肯定我已经非常接近做某事了,这特别显示了我的能力,因为我得到了很多“你太慢了,因为我已经学习了将近 4 个月,我认为我实际上做得很好。

PS I am doing it with no libraries to the subject PS我正在做这件事,没有图书馆的主题

PS the other error type string can't be inside a void PS 其他错误类型字符串不能在 void 内

For dropdown you can use dropdown_button2 package:对于下拉菜单,您可以使用 dropdown_button2 包:

https://pub.dev/packages/dropdown_button2 https://pub.dev/packages/dropdown_button2

      DropdownButtonFormField2(
    value: userData['$onSaveLabel'],
    decoration: InputDecoration(
      prefixIcon: Icon(
        iconData,
        color: CustomColors.primaryColor,
        size: 25,
      ),
      isDense: true,
      contentPadding: EdgeInsets.symmetric(vertical: 2),
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(5),
      ),
    ),
    isExpanded: true,
    hint: Text(
      hintText,
    ),
    icon: const Icon(
      Icons.arrow_drop_down,
      color: CustomColors.primaryColor,
    ),
    iconSize: 30,
    buttonHeight: 60,
    buttonPadding: const EdgeInsets.only(left: 20, right: 10),
    dropdownDecoration: BoxDecoration(
      borderRadius: BorderRadius.circular(5),
    ),
    items: items,
    validator: (value) {
      if (value == null && onSaveLabel == 'city') {
        return '   لطفاً یک شهرستان را انتخاب کنید.';
      }
      return null;
    },
    onChanged: (value) {
      //Do something when changing the item if you want.
    },
    onSaved: (value) {
      userData['$onSaveLabel'] = value.toString();
    },
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM