Kokila
Kokila

Reputation: 317

Flutter,I want my textfield with focus but hiding keyboard?

I need to scan data through a physical device using flutter textfield, but I don't have a solution yet I have gone through many and nothing works for me, please someone helps with this.

TextFormField(
                focusNode: loginTextfieldFocus,
                style: const TextStyle(fontSize: 15),
                controller: loginTextController,
                decoration: decoration(context, loginTextController),
                onChanged: (val) {
                  if (val.contains('\n') || val.contains('\r')) {
                    Log.d("LoginTextfield Entered");     
                  }
                },
              ),

Upvotes: 2

Views: 935

Answers (3)

Kokila
Kokila

Reputation: 317

This is not the proper way to do , but somehow i managed to do it.

 TextFormField(
   autofocus: true,
   enabled: isEditable,
   showCursor: isEditable,
   readOnly: true,
   keyboardType: TextInputType.none,
   focusNode: textfieldFocus,
   controller: textfieldCtr 
),
                

  //Called this under widget build
  @override
  Widget build(BuildContext context) {
    SchedulerBinding.instance.addPostFrameCallback((Duration _) {
      FocusScope.of(context).requestFocus( textfieldFocus);
    });

To receive scan data from device i used RawKeyboardListener. For scanning you can refer it here (flutter Enter event not found on zebra tc77?)

Upvotes: 2

Anandh Krishnan
Anandh Krishnan

Reputation: 5984

Try this

Widget build(BuildContext context) {
  Future.delayed(const Duration(), () => SystemChannels.textInput.invokeMethod('TextInput.hide'));

  return Scaffold(
    body: TextField(
      autofocus: true,
    ),
  );
}

Upvotes: 0

Muhammad Rameez
Muhammad Rameez

Reputation: 11

Yes, I have the same issue. I have solve by changing onChanged to validator because onChange is keep reloading your state thatswhy your keyboard is automatically dismiss every time.

validator: (value) {
  if (value != null && value.trim().length < 10) {
         return 'This field requires a minimum of 10 characters';
      }

        return null;
      },

Upvotes: 0

Related Questions