Home > Software design >  Why is my GestureDetector onTap event not firing?
Why is my GestureDetector onTap event not firing?

Time:12-31

I've created a custom form field and in it is a GestureDetector but the onTap is not firing. I want to use the GestureDetector to collect the click event when someone clicks on the form field in a disabled state so I can launch a dialog box. Code is below. I'm wondering what I must be doing wrong or misunderstanding.

Widget _customFormField(
    {required String title,
    required String initialValue,
    required int maxLines,
    required int maxLength,
    required bool enabled,
    required TextEditingController controller,
    bool autoFocus = false,
    required FocusNode currentFocusNode,
    required FocusNode futureFocusNode,
    Function? function,
    bool formEnd = false}) {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(title,
          style: const TextStyle(
              fontSize: 14.0,
              fontWeight: FontWeight.bold,
              fontFamily: 'Ariel',
              color: Colors.pink)),

      GestureDetector(
        onTap: () {
          print('onTap');  // <-------------------- This never gets hit
          function;
        },
        child: TextFormField(
          autofocus: autoFocus,
          enabled: enabled,
          textInputAction:
              formEnd ? TextInputAction.done : TextInputAction.next,
          onEditingComplete: () =>
              FocusScope.of(context).requestFocus(futureFocusNode),
          focusNode: currentFocusNode,
          controller: controller,
          keyboardType: TextInputType.text,
          validator: (value) {
            if (value!.isEmpty) {
              if (_formValid) {
                _formValid = false;
                currentFocusNode.requestFocus();
              }
              //title.toLowerCase()
              return 'Please enter the ${title.toLowerCase()}';
            } else {
              return null;
            }
          },
        ),
      ),
    ],
  );
}

I've tried changing it to an InkWell but it behaves the same.

I've removed some styling code from the example but nothing that could have an affect on the problem.

CodePudding user response:

well the function property that you got from the properties are the definition of the method, when you do :

  onTap: () {
          print('onTap');
          function;
        },

you not calling the method, you putting it there, you need to call it so it runs, by adding () or by calling call() on it.

  onTap: () {
          print('onTap');
          function(); // like this
          // function.call();  or like this
        },

CodePudding user response:

Try the following:

onTap: () {
          print('onTap');  // <-------------------- This never gets hit
          if (function != null) function.call();
        }
  • Related