Adding keyboard functionalities to button in flutter (custom keyboard with buttons) - flutter

I can use TextFormField() widget effectively. But, some of my users may not have the characters in their keyboard that is needed in my application. So, I want to create a button (raised, flat, material does not matter) and when user taps on the button I want to add some characters that are in the FormTextField() already.
Logically code is this:
RaisedButton(
child: Text('ə'),
onPressed: (){
input = input + 'ə';
}
),

You can modify the context of a text field using a TextEditingController.
Declaration:
final _controller = TextEditingController();
Assignment:
TextField( // Or TextFormField
controller: _controller,
...
),
Usage:
RaisedButton(
child: Text('a'),
onPressed: () {
setState(() => _controller.text += 'a');
},
),

Related

Flutter 3 - Stop Propagation When Tapping On Widget

I have a TextField widget inside a Container and want to be able to tap on the TextField without the tap propagating to the parent (the Container). I also want to unfocus the TextField when tapping outside it. To achieve that I have wrapped the Container with an AbsorbPointer widget inside a GestureDetector. The problem is that this makes all taps go to the Container and I lose the ability to interact with the TextField.
How to stop event propagation from the TextField to the Container?
Edit: Futter docs says that TextField widget has a function for tapping outside:
onTapOutside → TapRegionCallback?
Called for each tap that occurs outside of the TextFieldTapRegion group when the text field is focused.
This property does not show among the available properties on the TextField in my app; Flutter 3.3.10 • channel stable
Code looks something like this:
class Filters extends StatelessWidget {
const Filters({super.key});
#override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 12),
child: OutlinedButton.icon(
.
.
onPressed: () => {
showDialog(
context: context,
builder: (_) {
return Dialog(
child: SingleChildScrollView(
child: GestureDetector(
onTap: () => log("parent"),
child: AbsorbPointer(
child: Container(
.
.
child: Column(
children: [
.
.
TextField(
controller: textEditingController,
focusNode: focusNode,
onTap: () {
log("textField..............");
},
.
.
),
],
),
),
),
),
),
);
},
),
},
),
);
}
}

Long pressed menu in flutter

how to make this top bar menu when we do a long press on a card or container.
you can wrap your card widget with InkWell or GestureDetector both have longPress, then you pass a call back to your view to show the menu.
Color _colorFilterTile = Colors.transparent;
InkWell(
onLongPress: (){
//call function
setState(() {
_colorFilterTile= Colors.green.withOpacity(0.5);
});
},
child: Container(
color: _colorFilterTile,
child: ...,
),
)

How to get Flutter AlertDialog actions to watch for keyboard event (enter key)?

Is there a way to get the "enter" key keyboard event in flutter to default to one of the action buttons on my AlertDialog? Note I'm referring to use of flutter for web where there is a physical keyboard. For example I have this dialog:
Future<void> _confirmDelete(int index) async {
var fp = SessionInfo.of(context).myFloorplans.entries.toList()[index].value;
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Confirm Delete?'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8.0))),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text("Please confirm deleting ${fp.name}"),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text('Delete'),
onPressed: () {
_deleteFloorplan(index);
Navigator.of(context).pop();
},
),
],
);
},
);
}
Is there a way to default the "enter" key on the keyboard to act like they hit delete? Maybe not the safest UI experience yet, but mostly just asking to understand the foundation here.
Thanks,
Justin
The only thing you need to do is wrap your AlertDialog in a RawKeyboardListener.
check this example :
RawKeyboardListener(
// its better to initialize and dispose of the focus node only for this alert dialog
focusNode: FocusNode(),
autofocus: true,
onKey: (v) {
if (v.logicalKey == LogicalKeyboardKey.enter) {
_deleteFloorplan(index);
Navigator.pop(context);
}
},
child: your alert dialog ...
I don't see anything that will cause the keyboard to show in this dialog.
Anyway if you have a TextFormField that will show the keyboard you can do something like this
onFieldSubmitted: (value) {
_deleteFloorplan(index);
Navigator.of(context).pop();
}
onFieldSubmitted gets called whenever the user hits the text Input action button (the "enter" key you mentioned)

How to keep focus on TextField but not display keyboard in Flutter?

I am new to Flutter. The thing I want is to keep focus on TextField, but not display keyboard. Is it possible?
To give focus to a text field as soon as it’s visible, use the autofocus property.
content_copy
TextField(
autofocus: true,
);
_dismissKeyboard(BuildContext context) {
FocusScope.of(context).requestFocus(new FocusNode());
}
#override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: () {
this._dismissKeyboard(context);
},
child: new Container(
color: Colors.white,
child: new Column(
children: <Widget>[/*...*/],
),
),
);
}
Both of these components should be used together to implement what you are trying to acheive.

Flutter - click one RaisedButton and all RaisedButtons update

I am working on a calculator type app that has a lot of buttons on the screen. When I turn on dev tools to check repainting, I noticed that whenever one button is clicked, every button on the screen repaints. Instead of pasting tons of code in here, I created a demo app that shows what it is that I am doing. This is what the screen looks like:
Whenever any of the buttons are pressed, every button on the screen redraws.
This is the code that I'm using:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Button Testing'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
buttonPressed,
textAlign: TextAlign.center,
),
SizedBox(
height: 10.0,
),
RaisedButton(
child: Text('Button One'),
onPressed: () {
setState(() {
buttonPressed = 'Button One Was Pressed';
});
},
),
RaisedButton(
child: Text('Button Two'),
onPressed: () {
setState(() {
buttonPressed = 'Button Two Was Pressed';
});
},
),
RaisedButton(
child: Text('Button Three'),
onPressed: () {
setState(() {
buttonPressed = 'Button Three Was Pressed';
});
},
),
RaisedButton(
child: Text('Button Four'),
onPressed: () {
setState(() {
buttonPressed = 'Button Four Was Pressed';
});
},
),
],
),
),
),
);
}
I've tried breaking the button area out into a separate widget and nothing I've done matters. My question is, is there something else that I should be doing? Is this expected and acceptable behavior? Does it even matter that everything is redrawing whenever any buttons are tapped? I thought that in Flutter, only the things that needed to be redrawn were redrawn.
Whenever you call setState then it rebuild whole build method, so it is obvious that it rebuild RaisedButton.
You can avoid by using provider, Bloc and so on which will not rebuild whole build method. It will only change data.
It is totally depends on you that you want to accept it or not. If widget is not that much big than you can keep it according to me.
Redrawing widget every time is really not good practice but if it is not rebuilding very frequently or your application is not that much big then it is fine.
setState will rebuild all the screen ,
So you can avoid that by using Bloc it is a state controller which won't rebuild everything only what you need to rebuild you can use it from scratch or use the plugin bloc 1.0.0