1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Not able to persist keyboard on the mobile screen while tapping outside or scrolling...

Discussão em 'Mobile' iniciado por Stack, Outubro 17, 2024 às 04:43.

  1. Stack

    Stack Membro Participativo

    In my flutter PWA (app is being viewed from a mobile browser), i want to NOT dismiss the keyboard. Every time I tap outside or scroll the screen to see other fields in the form, it dismisses. Even if the input field loses focus - i want to keep the keyboard visible on the screen. I tried to add GestureDetector, NotificationListener<ScrollNotification>, TapRegion

    But I'm only able to request focus again and I'm not able to retain it.

    On trying FocusScope.of(context).requestFocus(_model.textFieldFocusNode) ->Keyboard closes and then opens again - which gives a bad user experience.

    How to prevent keyboard from closing unless a certain condition is true.

    Is this possible at all?

    Things I have tried (and didn't work):

    1. In GestureDetector

    behavior: HitTestBehavior.translucent,
    onTap: () {
    // Either requesting focus again
    if (!canLoseFocus) {
    FocusScope.of(context).requestFocus(_model.textFieldFocusNode);
    }

    // Or leaving the onTap function empty to not dismiss the keyboard
    },

    1. In NotificationListener

    NotificationListener<ScrollNotification>(
    onNotification: (ScrollNotification notification) {
    if (notification is ScrollUpdateNotification) {
    // Requesting focus again if it is lost
    if (!canLoseFocus) {
    FocusScope.of(context).requestFocus(_model.textFieldFocusNode);
    }
    }
    return true; // Consume scroll notifications to prevent focus loss
    },

    1. Also, wrapped widgets TapRegion to prevent input field from losing focus

    This is the code for my text input field

    TextFormField(
    controller: _model.textController,
    focusNode: _model.textFieldFocusNode,
    autofocus: false,
    obscureText: false,
    decoration: InputDecoration(
    labelStyle: FlutterFlowTheme.of(context).labelMedium.override(
    fontSize: AppFontSizes.medium/textScaleFac,
    color: AppColors.textLightLabel,
    fontFamily: 'Poppins',
    letterSpacing: 0,
    ),
    hintText: 'Enter Answer Here',
    hintStyle: FlutterFlowTheme.of(context).labelMedium.override(
    color: AppColors.textLightLabel,
    fontSize: AppFontSizes.medium,
    fontFamily: 'Poppins',
    letterSpacing: 0,
    ),
    enabledBorder: OutlineInputBorder(
    borderSide: BorderSide(
    color: AppColors.inputDarkBorder,
    width: 1,
    ),
    borderRadius: BorderRadius.circular(4),
    ),
    focusedBorder: OutlineInputBorder(
    borderSide: BorderSide(
    color: AppColors.inputDarkBorder,
    width: 1,
    ),
    borderRadius: BorderRadius.circular(4),
    ),
    ),
    style: FlutterFlowTheme.of(context).bodyMedium.override(
    fontFamily: 'Poppins',
    fontSize: 14,
    color: Colors.white,
    letterSpacing: 0,
    ),
    textAlign: TextAlign.start,
    maxLines: 6,
    validator: _model.textControllerValidator.asValidator(context),
    textInputAction: TextInputAction.done, // Shows the "Done" button on the keyboard
    onFieldSubmitted: (value) {
    setState(() {
    canLoseFocus = true;
    });
    FocusScope.of(context).unfocus();
    _model.textFieldFocusNode?.unfocus();
    },
    ),

    Continue reading...

Compartilhe esta Página