Search code examples
flutterflutter-web

Physical KeyBoard Not working For TextFormField in desktop Flutter


Recently I started making an admin panel on a desktop and using a text form field to take input from the user but unfortunately, the keyboard cannot write data inside the text field, I searched on Google and did not find any solution, please can someone help me?

Here is some of the text fields that I am using in my project:

Container(
                            width: 200,
                            margin: EdgeInsets.all(15),
                            child: TextFormField(
                              onChanged: (value) {
                                referalName.text = value;
                              },
                              validator: (value) {
                                if (value!.isEmpty) {
                                  return 'Please Enter Referal Code';
                                }
                                return null;
                              },
                              controller: referalName,
                              style: TextStyle(
                                fontSize: 24,
                                color: Colors.blue,
                                fontWeight: FontWeight.w600,
                              ),
                              decoration: InputDecoration(
                                focusColor: Colors.white,
                                //add prefix icon
                                prefixIcon: Icon(
                                  Icons.person_outline_rounded,
                                  color: Colors.grey,
                                ),

                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(10.0),
                                ),

                                focusedBorder: OutlineInputBorder(
                                  borderSide: const BorderSide(
                                      color: Colors.blue, width: 1.0),
                                  borderRadius: BorderRadius.circular(10.0),
                                ),
                                fillColor: Colors.grey,

                                hintText: "Referal Name",

                                //make hint text
                                hintStyle: TextStyle(
                                  color: Colors.grey,
                                  fontSize: 16,
                                  fontFamily: "verdana_regular",
                                  fontWeight: FontWeight.w400,
                                ),

                                //create lable
                                labelText: 'Enter Referal Name',
                                //lable style
                                labelStyle: TextStyle(
                                  color: Colors.grey,
                                  fontSize: 16,
                                  fontFamily: "verdana_regular",
                                  fontWeight: FontWeight.w400,
                                ),
                              ),
                            ),
                          ),

Solution

  • I have tested your code and it worked fine for me except one thing. You're resetting the value of referral controller every time text changes in your input field. Which is causing the unexpected error. Try removing or commenting out the onChanged function's code.

    onChanged: (value) {
      // referalName.text = value;
    },
    

    check out this link for more info:
    https://api.flutter.dev/flutter/material/TextField/onChanged.html