How to prevent keyborad from popping up in the whole app flutter - flutter

I am building an app in flutter for android pos device. but i want to hide the keyboard
when clicking on the textfield. how can i achieve that ?
I've tried the following code
FocusManager.instance.primaryFocus?.unfocus();
when i calling this code nothing can be entered and the third or fourth tap keyboard popes again

try this:
TextField(
showCursor: true,
readOnly: true),
found here : https://stackoverflow.com/a/60106951/12838877

try this,
InkWell(
onTap: (){
FocusManager.instance.primaryFocus?.unfocus();
},
child: AbsorbPointer(
absorbing: true,
child: TextField(),
),
)

Related

Flutter: Showing Text Selection toolbar onTap apart from long press

I have created TextField like below
TextField(
inputFormatters: [
AppConstants.defaultPhoneInputFormatter
],
keyboardType: TextInputType.phone,
controller: phoneController,
onTap: () {
///... any suggestion
},
)
What should I do inside the onTap method of above TextField so that TextSelection controls appear onTap, just like they appear on long press.
Can we do this leveraging built-in Text controls without using custom popup?
Thanks in advance.
I think you can not do that exactly, but you can "override" long press function:
GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(_textfieldFocusNode);
showMenu...
},
onLongPress: () {
showMenu...
},
child: IgnorePointer(
child: TextField(
focusNode: _textfieldFocusNode,
controller: _controller,
),
)
)
And use Clipboard class to paste

How to make InkWell `onLongPress()` override tap on a TextField inside of it?

I have a TextField inside of an InkWell with an onLongPress() callback. The problem is, despite the fact that even when long pressing on the TextField, I see the ripple effect on InkWell, but the onLongPress() does not run after the long press time passes. It only gets me into editing Text. When pressing on the bottom side of the Card, everything runs fine.
In short: On tap I want to get into TextField editing. On long press I want to trigger the onLongPress(), not the TextField, even if I am pressing on it.
How do I do this? Thank you.
InkWell(
onLongPress: () {
// do stuff
}
child: ListTile(
title: TextField(),
),
),
You can use the AbsorbPointer widget to ignore the TextField gesture recognizer:
InkWell(
onLongPress: () {
print('onLongPress');
},
child: AbsorbPointer(
child: ListTile(
title: TextField(),
),
),
)
To still enabling the editing of TextField when single tapping on it, you can use FocusNode like this:
InkWell(
onLongPress: () {
print('onLongPress');
},
onTap: () => node.requestFocus(),
child: AbsorbPointer(
child: ListTile(
title: TextField(
focusNode: node,
controller: textController,
),
),
),
)
#Bach 's answer helped me to find a solution. Thank you!
InkWell(
onLongPress: () {
// do stuff
},
child: ListTile(
title: GestureDetector(
onTap: () => FocusScope.of(context).requestFocus(_focusNode),
child: AbsorbPointer(
child: TextField(
focusNode: _focusNode,
),
),
),
),
The only problem is now that I started messing with focusNode, multiple input fiels are focusing at the same time. But that is a whole other story ;)
UPD: Just realised, that I can't move text cursor this way. So not useful.
It seems that IntrinsicWidth widget can find the right balance between long press and text editing.
The rationale behind is that IntrinsicWidth will let the TextField shrink to its minimum width, therefore avoiding a gesture collision with the InkWell
So your solution can be like this:
InkWell(
onLongPress: () {
// do stuff
}
child: ListTile(
child: IntrinsicWidth(
title: TextField(
//remember to make some hints here
//because with intrinsicwidth if your textfield is empty it might disappear
),
),
),
),

flutter :Hide keyboard for a TextField

I have a search textfield and when I enter the field I see the keyboard. I would like to go to another screen when the user click/select the field instead of showig the keyboard.
How do I hide the keyboard and go go to another screen when a user click on the field.
Basically, I want to create the action that Gmail is doing when you click the search button.
This is snippet of the textfield..
Container(
color: Palette.WHITE,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
TextField(
controller: _textSearchController,
onSubmitted: (val) {}
decoration: InputDecoration(...)
)
],
)
)
Thanks for your help
Have you tried to hide keyboard at onSubmit method like below?
onSubmitted: (val) {
FocusScope.of(context).requestFocus(FocusNode());
}
Let me know if it works.
If you want to not open a keyboard and use your TextField as basically a button to go to another screen:
TextField(
readOnly: true,
onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => SomeOtherScreen())),
),
There is a onTap property on the TextField widget and you can use this to navigate to a different route. If you want to disable typing, you can set the readOnly property to true.
If you want to re-create the search button on Gmail, you can take a look at the Search Delegate showMenu method that flutter provides

Flutter keyboard hide not working on tab clicks

I have one application of tabbars. one tab have textfield. It shows keyboard. I want to hide keyboard after press ob any other tab. I used below code but its still not working on other tabs.
return new Scaffold(
key: _scaffoldKey,
resizeToAvoidBottomInset: false,
backgroundColor: blackColor,
body: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: Stack(children: <Widget>[
_showForm(),
],)
),
);
you can use onTap under TabBar() like this:
TabBar(
onTap: (_) => FocusManager.instance.primaryFocus?.unfocus(),
tabs: [
tab1(),
tab2(),
)
this will make every tab unfocus the keyboard and move to other tab as well
Use FocusScope.of(context).unfocus() when tab change.
when you click to change tab write below line :
FocusScope.of(context).requestFocus(new FocusNode());

Using a textfield as a button

I'm trying to use a textfield as a button so when someone taps it, it takes him to a different page.
I've tried disabling the textfield but then I cannot use the onTap property. But if the textfield is not disabled, when I click it, it gets editing focus and this is something I don't want.
Can anyone help what's the easiest way this could be done?
Thanks
TextField(
readOnly: true,
onTap: () {
// Do something
},
);
You can use a Flatbutton instead. Just put this in your code:
FlatButton(
onPressed: () {
/*...*/
},
child: Text(
"Clickable text!",
),
)
Or else use a GestureDetector:
GestureDetector(
OnTap: () {...},
child: TextField(
// Text...
),
);
you can wrap it with an InkWell, see below:
InkWell(
onTap: () {
},
child: Container(
padding: EdgeInsets.all(4.0),
child: Text('Click me'),
),
);
you can try the following.
GestureDetector(
onTap: () {
// redirect // nav to different screen
},
child: TextField(
enabled: false
),
)
References
TextField
GestureDetector