简体   繁体   中英

Flutter Text Form Field

i want to hide the second text form field if the first starts with a if i write any word in the first text form field that starts with a the second disappear

    TextFormField(
                            controller: firstColorController,
                            decoration:const InputDecoration(
                                border: OutlineInputBorder(),
                                hintText: "first color"
                            ),
                            validator: (query){
                              if(query!.isEmpty){
                                return "color can't be empty";
                              }else if(query.trim().length<5||query.trim().length>9){
                                return "colors can't be less than 5 chars or greater than 9 chars";
                              }
                              else if(checkFirstColor(query.trim())){
                                return cubit.errorMessage;
                              }

                            },
                          ),
                          const SizedBox(height: 15,),
                           if(firstColorController.text.startsWith("a"))
                           TextFormField(
                              controller: secondColorController,
                              decoration:const InputDecoration(
                                  border: OutlineInputBorder(),
                                  hintText: "second color"
                              ),
                              validator: (value){
                                if(value!.isEmpty){
                                  return "value can't be empty";
                                }
                                return null;
                              },
                           ),

Here's an example.

  bool secondVisible = true;
  TextEditingController firstCtl = TextEditingController();
  TextEditingController secondCtl = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
          TextFormField(controller: firstCtl, onChanged: (){
            setState(() {
              secondVisible =  firstCtl.text.startsWith('a');  
            });
          },),
          Visibility(
              visible: secondVisible,
              child: TextFormField(controller: secondCtl)  
          ),
          ],
        ),
      ),
      
    );
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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