How to remove label of textfield? - flutter

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.

Related

Adaptive Mask for TextField

I've made a TextField that is suppose to update the user's info.
The user has to type either 11 or 14 numbers, so the mask for the textfield has to change if someone types more than 11 numbers. How do I do that?
Masks:
var mascaraCpf = MaskTextInputFormatter(
mask: '###.###.###-##',
filter: {"#": RegExp(r'[0-9]')},
type: MaskAutoCompletionType.lazy);
var mascaraCnpj = MaskTextInputFormatter(
mask: '##.###.###/####-##',
filter: {"#": RegExp(r'[0-9]')},
type: MaskAutoCompletionType.lazy);
TextField:
TextField(
keyboardType: TextInputType.number,
inputFormatters: [
mascaraCpf,
FilteringTextInputFormatter.digitsOnly
],
controller: cpfController,
decoration: InputDecoration(
filled: true,
fillColor: Color(0xffFCF9F4),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(5))),
hintText: appModel.usuario!.cpf,
),
),
Do you have an StatefulWidget?
If yes, try something like the following:
onChanged: (value) {
if (value.length >= 11) {
setState(() {
//change something
});
} else {
setState(() {
//change back
});
}
}
First, make sure your screen is a stateful widget.
Then replace the textfield with the following code:
TextField(
onChanged: (value) => {
if (value.length >= 11 && value.length <= 14) //changing value if input length is more than 11 and less than 14
{
setState(() {
currentMask = mascaraCnpj;
})
}
else
{
if(currentMask != mascaraCpf) { //rebuilding screen if currentMask != mascaraCpf , otherwise it'll lead to unnecessary rebuilds
setState(() {
currentMask = mascaraCpf;
})
}
}
},
keyboardType: TextInputType.number,
inputFormatters: [currentMask, FilteringTextInputFormatter.digitsOnly],
controller: TextEditingController(),
decoration: InputDecoration(
filled: true,
fillColor: Color(0xffFCF9F4),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5))),
hintText: appModel.usuario!.cpf,
),
),

Flutter focusnode problem not working - only by clicking it works

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.

flutter format currency in texformfield

I have a flutter app which accepts an amount of money as input using a textformfield. I would like to format the input in textformfield so that whatever is being input can be formatted as currency copmlete wiht thousand separator commas. I have tried using the intl package number formatter but all I can do is print it to the command line.
Here is how it looks currently
This is how I would like it to look like
This is the textfield code
TextEditingController currencyControler = TextEditingController();
String? amount;
TextFormField(
controller: currencyControler,
keyboardType: TextInputType.number,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter an amount';
}
return null;
},
onSaved: (String? value) {
amount = value;
},
decoration: InputDecoration(
icon: const Icon(Icons.money_outlined),
labelText: "Amount",
hintText: 'Enter an amount',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
)
How can I format the input so that the comma separators appear as any number is being entered
Try below code hope its helpful to you.Used intl package here for number formation
Your functions:
TextEditingController currencyControler = TextEditingController();
String formNum(String s) {
return NumberFormat.decimalPattern().format(
int.parse(s),
);
}
Your Widget:
TextFormField(
controller: currencyControler,
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(
Icons.money,
)),
keyboardType: TextInputType.number,
onChanged: (string) {
string = '${formNum(
string.replaceAll(',', ''),
)}';
currencyControler.value = TextEditingValue(
text: string,
selection: TextSelection.collapsed(
offset: string.length,
),
);
},
),
Your result screen->

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.