Flutter: How to disable LongPress on TextFormField?

89 views Asked by At

There's a problem. I'm new to flutter. I needed to place a TextFormField in a ReorderableListView, but at the same time I can’t shuffle the fields when I hold it for a long time, since clicking on the TextFormField is triggered.

 ReorderableListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return Padding(
                    padding: const EdgeInsets.all(5),
                    key: Key('$index'),
                    child: Container(
                      height: 40,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),
                          border: Border.all(color: Colors.blueAccent)),
                      child: Padding(
                          padding: const EdgeInsets.all(10),
                          child: TextFormField()),
                    ),
                  );
                },
                itemCount: 3,
                onReorder: (int oldIndex, int newIndex) {
                  context.read<MainCubit>().onReorder();
                },
                shrinkWrap: true,
              ),

I can move the TextFormField if I wrap it in an AbsorbPointer, but in this case, the TextFormField does not work.

  child: AbsorbPointer(
                        absorbing: true,
                        child: Padding(
                            padding: const EdgeInsets.all(10),
                            child: TextFormField()),
                      ),

What can you do about it? It seems to me that you can disable TextFormField’s response to long presses, but I don’t know how to do this.

1

There are 1 answers

2
Mahendran K On

Use gesture recognizers along with a custom ReadOnlyEditableText widget to allow users to input text in a TextFormField, but disable long-press actions.

 TextFormField(
    controller: myController,
    decoration: InputDecoration(
      labelText: 'Your Label',
    ),
    // Use a custom EditableText widget with a custom gesture recognizer
    builder: (context, textEditingController, child) {
        return EditableText(
            controller: textEditingController,
            focusNode: textEditingController.focusNode,
            style: DefaultTextStyle.of(context).style,
            cursorColor: Colors.blue, // Customize as needed
            backgroundCursorColor: Colors.grey,
            // Set a custom gesture recognizer to handle long-press actions
           selectionControls: TextSelectionControls(
                handlePaste: (TextSelectionControls controls, 
                TextEditingValue value) {
                  return value; // Disable paste action
                },
                handleCopy: (TextSelectionControls controls) {
                  return ''; // Disable copy action
                },
                handleCut: (TextSelectionControls controls) {
                  return ''; // Disable cut action
                },
            ),
         );
      },
    )