Home > Mobile >  the information I write in my TextFormField disappears when I finish writing in Dart/flutter
the information I write in my TextFormField disappears when I finish writing in Dart/flutter

Time:10-05

i am trying to make a form registering the WhatsApp number using a showCountryPicker, which I have to select the code and write the number.

case 1:: If I first write the phone number (I have to double click on the textformfield when it should be only 1 to write) and then I select the country code, what I wrote in the true textform disappears.

case 2:: I select the code and then I write in the textformfield, what I write in the textform field disappears when I click on the check.

I don't know how to solve this problem.

Help would be greatly appreciated

code and image::

 class whatsappregistro extends State<WhatsAppRegistro>{
    
        var countrycode = "";
    
      @override
      Widget build(BuildContext context) {
       
        final numerowhatsapp = TextEditingController();
    
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: new AppBar(
        title: new Text("WhatsApp"),
        centerTitle: true,
        automaticallyImplyLeading: false,
      ),
    
       body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          TextFormField(
            controller: numerowhatsapp,
            keyboardType: TextInputType.number,
           textInputAction: TextInputAction.done,
            maxLines: 1,
            decoration:  InputDecoration(
            labelText: "Ingresar  número de Whatsapp",
               // hintText: 'Ej:0',
             border: InputBorder.none,
              prefixIcon: Container(
                padding: const EdgeInsets.symmetric(horizontal: 15,vertical: 6),
        margin: const EdgeInsets.symmetric(horizontal: 8),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
        GestureDetector(
          onTap: () async {
         await showCountryPicker(context: context,
                onSelect: (value){
                  setState(() {
                countrycode = value.phoneCode.toString();
                  });
                }
            );
          },
        
          child:   Row(
            children: [
            Container(
            
              padding: EdgeInsets.symmetric(
            
                horizontal: 16,vertical: 6
            
              ),
            
              decoration: BoxDecoration(
            
                color: Colors.black,
            
                borderRadius: BorderRadius.circular(5)
            
              ),
            
              child: countrycode == "" ? Text(" ",
        
                style: TextStyle(
        
                    color: Colors.white
        
                ),
        
              ):Text(" " countrycode,
        
                style: TextStyle(
                    color: Colors.white
                ),
              ),
            ),
             // SizedBox(height: 150,),
        
            ]
          ),
        )
                  ],
                ),
              )
            ),
          ),
    }

enter image description here

CodePudding user response:

You should do this:

where you declared countryCode

final numerowhatsapp = TextEditingController();

@override
      Widget build(BuildContext context) {
  • Related