Flutter focusnode problem not working - only by clicking it works - flutter

iam using a device that scans barcode , I want after each read the focus to return the TextFormFiled - the code below do the work and I see the cursor is focused on the TextFormFiled but when I read next time its show nothing , I need to manually just click by my finger on the textfiled to activate the focus ,can somebody help me ( the device returned LF after each read)
TextFormField(
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
filled: true,
hintStyle: new TextStyle(
color: Colors.grey[800]),
hintText: "Read BarCode",
fillColor: Colors.white70),
focusNode: myFocusNode,
controller: search,
autofocus: true,
maxLines: null,
validator: (value) {
// print(value.toString().runes);
if (value.toString().contains("\n")) {
fetchProducts(value!);
search.text = "";
} else {}
},
),

Use your myFocusNode to activate the focus on textField.
void function(){
/// after scanning is complete call this
focusNode.requestFocus()
}

I pass for this and did solve it like this:
_addItem() {
final isValid = _formKey.currentState?.validate() ?? false;
if (!isValid) {
return;
}
final ean = int.parse(_eanController.text);
listaEan.add(ean);
_eanController.text = '';
setState(() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_eanFocus.requestFocus();
});
});
}
but in physical scan device, did works fine. without use the addPostFramaCallback.

Related

How can I hide keyboard jsut once in Flutter

I'm creating a "barcoder" text field on flutter. After the page opened which is the barcoder belong,
I need to autofocus on TextField. But on the first opening, I want to hide the keyboard. And then if the user focuses on TextField The keyboard can appear.
How can I achieve this? I already try
class FirstDisabledFocusNode extends FocusNode {
#override
bool consumeKeyboardToken() {
return false;
}
}
and it didn't work.
Here is my code
Card(
elevation: 1,
child: TextField(
autofocus: true,
focusNode: textFieldFocusNode,
onEditingComplete: () {
print('Barcode: ${textController.text}');
buttonFocusNode.requestFocus();
events!(context);
if (textController.text.isNotEmpty) {
_audioCache.play('success.mp3');
}
},
controller: textController,
decoration: const InputDecoration(
fillColor: Colors.white,
prefixIcon: Icon(Icons.qr_code),
hintText: 'Barcode',
focusColor: Palette.asBlue,
border: InputBorder.none,
focusedBorder: InputBorder.none),
),
);
When it goes to the next screen, you can use this in the initState of that widget.
void initState() {
super.initState();
Future.delayed(
Duration(),
() => SystemChannels.textInput.invokeMethod('TextInput.hide'),
);
}
if it didn't work click the link that i provided in the comments.

How To Create Number Picker as a alert dialog in flutter?

I am developing e com application, when user click on quantity label want to show dialog box for select how many quantity he want to buy.
I tried to wrap number picker inside Alert dialog. It show alert dialog, but the problem is not updating value when scroll
I can I use Number Picker for that?
Use a drop button combo widget. The DropdownButtonFormField holds a collection of YourClassView type objects held in the list listStatusMenuItems. The _currentStatusComboView variable holds the selected value. It is assigned on the onChange event. I use a restful call to load the listStatusMenuItems.
List<DropdownMenuItem> listStatusMenuItems = <DropdownMenuItem>[];
StatusComboView _currentStatusComboView;
_loadStatusCombo() {
Provider.of<Api>(context, listen: false)
.getYourClassViews()
.then((listView) {
setState(() {
listStatusMenuItems =
listView?.map<DropdownMenuItem<YourClassView>>((item) {
return DropdownMenuItem<StatusComboView>(
value: item, child: Text(item.displayValue));
}).toList();
});
});
#override
void initState() {
super.initState();
_loadStatusCombo();
}
DropdownButtonFormField<YourClassView>(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0),
),
),
filled: true,
hintStyle:
TextStyle(color: Colors.grey[800]),
hintText: "Select a Value",
fillColor: Colors.orange),
items: listStatusMenuItems,
isDense: true,
isExpanded: true,
value: this._currentStatusComboView,
validator: (value) =>
value == null ? 'field required' : null,
onChanged: (StatusComboView value) {
setState(() {
this._currentStatusComboView = value;
});
}),

How to change the set value of a text field

So I have TextFormField and it has a set value. However when I try to change the value of it by typing in the Feild, it just reverts to the specified value. This is my code:
TextFormField(
onEditingComplete: () {
FocusScope.of(context).unfocus();
setState(() {
// THIS IS WHERE I NEED TO CHANGE ITS VALUE
});
},
controller: _titleController..text = newTask.title, //newTask IS AN OBJECT
cursorHeight: 23.0,
decoration: InputDecoration(
disabledBorder: UnderlineInputBorder(),
border: OutlineInputBorder(),
hintText: "Enter Text",
labelText: "Title",
labelStyle: TextStyle(
fontSize: 20.0
),
),
),
I need to somehow set _titleController.text to the value that is in the field when enter or the tick button is clicked - but I cant just do _titleController.text = _titleController.text; for obvious reasons. Please help. Comment if you need more details.
change it to this:
#override
void initState() {
super.initState();
_titleController.text = newTask.title;
}
.
.
.
.
.
//And in your TextFormField
controller: _titleController,
You assign the value of newTask.title to _titleController.text
controller: _titleController..text = newTask.title, //newTask IS AN OBJECT
Make it:
controller: _titleController

TextFormField cuts last character input

I'm having a weird issue with a TextFormField, it cuts off the last input. If I type something like hello, it's gonna cut off the o and save hell. if I give it a space like hello , it's gonna save hello. Has anyone been through the same issue and know how to fix it?
here is my code:
String messageText;
_ChatScreenState() {
_formKey = GlobalKey<FormState>();
_scrollController = ScrollController();
messageText = "";
}
Widget messageTextField() {
return SizedBox(
width: deviceWidth * 0.55,
child: TextFormField(
style: TextStyle(color: Colors.white),
validator: (text) {
if (text.isEmpty) {
return 'Digite sua mensagem';
}
return null;
},
onChanged: (text) {
_formKey.currentState.save();
},
onSaved: (text) {
setState(() {
messageText = text;
});
},
cursorColor: Colors.white,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Digite uma mensagem',
hintStyle: TextStyle(color: Colors.white.withAlpha(100))),
autocorrect: false,
),
);
}
I do not think save() is intended to be used with onChanged.
If you place a button somewhere and perform _formKey.currentState.save(); within onPressed of the button, you should see the entire text value being saved.

How to remove label of textfield?

Would like to ask if it is possible to remove the label "Label" on top left of the text field in Flutter.
Yes, set the floatingLabelBehavior to never in InputDecoration. It worked for me.
decoration: InputDecoration(
floatingLabelBehavior: FloatingLabelBehavior.never,
),
Yes, is it possible. Just remove the hintText and labelText in InputDecoration.
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0),
)),
// hintText: Localization.of(context).accessLevel,
// labelText: Localization.of(context).accessLevel,
),
The following code removes the LabelText when you write anything in the TextField and shows LabelText when you do not write anything in the TextField.
TextEditingController Controller = TextEditingController();
bool ForBool = true;
TextField(
onChanged: (value) {
if (value.length <= 0) {
setState(() {
ForBool = false;
});
} else {
setState(() {
ForBool = true;
});
}
Controller.text = value;
print(value);
Controller.selection = TextSelection.fromPosition(
TextPosition(offset: Controller.text.length));
},
controller: Controller,
decoration: InputDecoration(
hintText: ForBool ? null : "Search...",
),
),
Simply Remove the labelText property in the InputDecoration.