Home > Back-end >  Flutter - GestureDetector function does not work with custom widget
Flutter - GestureDetector function does not work with custom widget

Time:12-15

I am trying to manage some gap between elements and update the gap using setState(). Unfortunately, it did not work with a custom widget TextFieldInput as child of GestureDetector.

GestureDetector(
                      onTap: () {
                        setState(() {
                          gap = 16.00;
                        });
                      },
                      child: TextFieldInput(
                        textEditingController: _passwordController,
                        hintText: 'Enter your password',
                        textInputType: TextInputType.text,
                        isPass: true,
                      ),
                    ),

But it did work with a Text widget.

GestureDetector(
                  onTap: () {
                    setState(() {
                      gap = smallGap;
                    });
                  },
                  child: Container(
                    child: Padding(
                      padding: EdgeInsets.all(5),
                      child: Text('Tap here'),
                    ),
                    padding: const EdgeInsets.symmetric(
                      vertical: 8,
                    ),
                  ),
                )

Here is the TextFieldInput widget structure:

import 'package:flutter/material.dart';

class TextFieldInput extends StatefulWidget {
  final TextEditingController textEditingController;
  final bool isPass;
  final String hintText;
  final TextInputType textInputType;
  final VoidCallback onTap;
  const TextFieldInput({
    super.key,
    required this.textEditingController,
    this.isPass = false,
    required this.hintText,
    required this.textInputType,
    required this.onTap,
    this.child,
  });
  final Widget? child;

  @override
  State<TextFieldInput> createState() => _TextFieldInputState();
}

class _TextFieldInputState extends State<TextFieldInput> {

  @override
  Widget build(BuildContext context) {
    final inputBorder = OutlineInputBorder(
        borderSide: Divider.createBorderSide(context)
    );
    return TextField(
      controller: widget.textEditingController,
      decoration: InputDecoration(
        hintText: widget.hintText,
        border: inputBorder,
        focusedBorder: inputBorder,
        enabledBorder: inputBorder,
        filled: true,
        contentPadding: const EdgeInsets.all(8),
      ),
      keyboardType: widget.textInputType,
      obscureText: widget.isPass,
      onTap: () {},
    );
  }
}

But "gap" does not change even on tapping on the TextFieldInput. Can anyone help with this ?

CodePudding user response:

gesture detector doesn't work with textField try adding ignorePointer

GestureDetector(
            child: new IgnorePointer (
                child: new TextField(
              .......
            ))),
        onTap: () {
         //do some thing
        },

CodePudding user response:

The TextField has its onTap property, I think this is the cause of your problem. You should you onTap property of TextField or use another suitable widget

  • Related