Flutter: Conflict between TextField's onTap and prefixIcon's onPressed

Issue

When the prefixicon is clicked, onTap will be called back automatically. How to ban it?

          child: TextField(
            onTap: ReplyController.to.onClickTextField,
            decoration: InputDecoration(
              prefixIcon: IconButton(
                onPressed: ReplyController.to.openEmojiField,
                color: Colors.black,
                icon: Icon(CupertinoIcons.smiley),
              ),
            ),
          ),

Solution

You can add a FocusNode to the TextField to have access whether the field is actually focused. And now if you want to ignore the onTap when textfield has focus you could modify the onTap such as onTap: !focusNode.hasFocus? ReplyController.to.onClickTextField : null

Answered By – esentis

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.