Flutter : Textield clear automaticly after input text

Issue

I have this text field in flutter app using provider as shown :

getPhoneNumberScreen(size, context) {
  var remianheight = size.height - 70;
  TextEditingController _phoneNumber = TextEditingController();
  TextEditingController _code = TextEditingController();

  return TextField(
                  keyboardType: TextInputType.number,
                  controller:
                      !authProvider.isPhoneNumberInserted ? _phoneNumber : _code,
                  decoration: InputDecoration(
                      isDense: true,
                      hintText: !authProvider.isPhoneNumberInserted
                          ? 'Phone Number'
                          : 'Verification Code',
                      enabledBorder: UnderlineInputBorder(
                        borderSide: const BorderSide(
                          color: Color(0x00000000),
                          width: 1,
                        ),
                        borderRadius: BorderRadius.circular(30),
                      ),
                      focusedBorder: UnderlineInputBorder(
                        borderSide: const BorderSide(
                          color: Color(0x00000000),
                          width: 1,
                        ),
                        borderRadius: BorderRadius.circular(30),
                      ),
                      filled: true,
                      fillColor: ColorManager.helibi,
                      contentPadding:
                          const EdgeInsetsDirectional.fromSTEB(20, 15, 0, 15),
                      prefixIcon: const Icon(
                        Icons.phone_sharp,
                        color: Color(0xFF8F8F8F),
                      )),
                );
}    

When I try to input some text, it is clear after I finish.
Also how make the textfild move up to be shown.

Solution

the mistake is it controller of textfield mus be outside the method :

TextEditingController _phoneNumber = TextEditingController();
TextEditingController _code = TextEditingController();
getPhoneNumberScreen(size, context) {
  var remianheight = size.height - 70;
  

  return TextField(
                  keyboardType: TextInputType.number,
                  controller:
                      !authProvider.isPhoneNumberInserted ? _phoneNumber : _code,
                  decoration: InputDecoration(

Answered By – Sarmed MQ Berwari

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.