Inside my TextFormField, I have a maxLength of 35, and the maxLengthEnforced (which by default is true) isn't being enforced, when the user writes something between the text, it just pushes the characters to the right and deletes the ones in the end to meet the 35 maxLength.
This is what I'm using:
TextFormField(
focusNode: _currentFocus,
textInputAction: _textInputAction ?? TextInputAction.done,
onFieldSubmitted: _textInputAction == TextInputAction.done
? (_) => FocusScope.of(context).unfocus()
: (_) => FocusScope.of(context).nextFocus(),
initialValue: _initialValue,
decoration: InputDecoration(
counterText: "",
hintText: _hintText,
hintStyle: TextStyle(color: Colors.grey),
focusedBorder: new UnderlineInputBorder(
borderSide: new BorderSide(color: Colors.blue),
),
),
maxLines: 1,
maxLength: 35,
maxLengthEnforced: true,
onChanged: _onChanged,
style: TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.w500),
);
Related
I have Text field which will take a long message from the user and I set expands to True
the problem is the hint text is not at the beginning of the the line, How do I change its position ?
SizedBox(
height: 150,
child: CustomTextField(
enabledRadius: 7,
focusedRadius: 7,
expands: true,
hintText: 'message',
controller: messageController,
textInputType: TextInputType.multiline,
),
),
Thanks everyone, I figured it out by using
textAlignVertical: TextAlignVertical(y: -1),
You can use `textAlign:TextAlign.start` to align the hint
Here see the full code:
SizedBox(
height: 150,
child: CustomTextField(
enabledRadius: 7,
focusedRadius: 7,
expands: true,
maxLines: null,
textAlign: TextAlign.start,
hintText: 'message',
controller: messageController,
textInputType: TextInputType.multiline,
),
),
You can customise TextFormField by using these properties.
textAlign: TextAlign.left,
decoration: const InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
hintText: '0',
counterText: '',
hintStyle: TextStyle(
color: greyPlaceholderColor,
fontSize: fontSize40,
fontWeight: FontWeight.w700)),
TextField textWidget(String text, IconData icon, bool isPasswordType,
TextEditingController controller, TextAlign align) {
return TextField(
controller: controller,
obscureText: isPasswordType,
enableSuggestions: !isPasswordType,
autocorrect: !isPasswordType,
cursorColor: Colors.white,
style: TextStyle(color: Colors.white.withOpacity(0.9)),
decoration: InputDecoration(
prefixIcon: Icon(
icon,
color: Colors.white70,
),
labelText: text,
labelStyle: TextStyle(color: Colors.white.withOpacity(0.9)),
filled: true,
floatingLabelBehavior: FloatingLabelBehavior.never,
fillColor: const Color.fromARGB(255, 209, 107, 107).withOpacity(0.7),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: const BorderSide(width: 0, style: BorderStyle.none)),
),
keyboardType: isPasswordType
? TextInputType.visiblePassword
: TextInputType.emailAddress,
);}
I created a widget called textWidget and it's;
child: textWidget(
"Mail Adress",
Icons.people_alt_outlined,
false,
_numberController,
TextAlign.center),
I tried to add it like this. I call in textWidget;
(String text, IconData icon, bool isPasswordType,TextEditingController controller, TextAlign align)
I assigned all the values as seen in the second code. However, this did not bring the articles to the middle of the label.
You can use label to place Text widget instead of labelText
decoration: InputDecoration(
label: Center(
child: Text(
text,
style: TextStyle(
color: Colors.white.withOpacity(0.9),
),
),
),
TextField textWidget(String text, IconData icon, bool isPasswordType,
TextEditingController controller, TextAlign align) {
return TextField(
controller: controller,
obscureText: isPasswordType,
enableSuggestions: !isPasswordType,
autocorrect: !isPasswordType,
cursorColor: Colors.white,
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white.withOpacity(0.9)),
decoration: InputDecoration(
label: Center(
child: Text(
text,
style: TextStyle(
color: Colors.white.withOpacity(0.9),
),
),
),
prefixIcon: Icon(
icon,
color: Colors.white70,
),
filled: true,
floatingLabelBehavior: FloatingLabelBehavior.never,
fillColor: const Color.fromARGB(255, 209, 107, 107).withOpacity(0.7),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: const BorderSide(width: 0, style: BorderStyle.none)),
),
keyboardType: isPasswordType
? TextInputType.visiblePassword
: TextInputType.emailAddress,
);
}
I want to get rid of the letters going up when I hit enter in a text field. What should I do?
It's part of the code.
Container(
child: TextField(
textAlign: TextAlign.center,
autofocus: true,
// controller: myController,
onChanged: (value) {
// print('$value');
setState(() {
mainText = value;
});
},
keyboardType: TextInputType.multiline,
minLines: 1,
maxLines: 10,
decoration: InputDecoration(
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
hintText: "짧은 글귀를 집필해주세요",
hintStyle:
TextStyle(fontSize: 14, color: Colors.black),
),
),
)
As per your hint text you just want short text and you can achieve by integrating with only single line textfield
Container(
child: TextField(
textAlign: TextAlign.center,
autofocus: true,
// controller: myController,
onChanged: (value) {
setState(() {
mainText = value;
});
},
keyboardType: TextInputType.text, // this is change
textInputAction: TextInputAction.done, // this is change
decoration: InputDecoration(
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
hintText: "짧은 글귀를 집필해주세요",
hintStyle: TextStyle(fontSize: 14, color: Colors.black),
),
),
)
for multiline text:
keyboardType: TextInputType.multiline,
maxLines: 5,
textInputAction: TextInputAction.newline,
Whenever I write the word is underlined. As soon as I write a new word, it will no longer be underlined, but what I am currently writing. Can I somehow issue that nothing is underlined when writing? I didn't find anything in the forum either.
child: TextField(
autocorrect: false,
style: TextStyle(
fontSize: 22.0,
),
cursorColor: Colors.black,
decoration: InputDecoration(
hintText: 'Search',
border: InputBorder.none,
icon: Icon(
Icons.search,
color: Color(0xFFe1F5FE),
),
),
),
did you try it with focusedBorder?
TextField(
decoration: new InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
contentPadding: EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
hintText: 'what you want'
),
),
Put this code inside TextField
keyboardType: TextInputType.text,
Example:
TextField(
keyboardType: TextInputType.text,
autocorrect: false,
style: TextStyle(
fontSize: 22.0,
),
cursorColor: Colors.black,
decoration: InputDecoration(
hintText: 'Search',
border: InputBorder.none,
icon: Icon(
Icons.search,
color: Color(0xFFe1F5FE),
),
),
),
I want to focus my text field when initstate is initialized. eg. when I open any page and then if there is any text field then text field needs to be automatically focused.
TextFormField(
focusNode: focusNode,
style: TextStyle(
color: Colors.white,
fontSize: orientation == Orientation.portrait
? MediaQuery.of(context).size.width * 0.025
: MediaQuery.of(context).size.width * 0.015,
),
validator: (val) => Validators.validateRequired(
val, " Product Baarcode"),
controller: _addproduct,
// focusNode: _addproductFocusNode,
decoration: InputDecoration(
errorStyle: TextStyle(color: Colors.yellow),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
fillColor: Colors.white,
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
hintStyle: TextStyle(color: Colors.white),
labelStyle: TextStyle(color: Colors.white),
filled: false,
prefixIcon: Icon(
FontAwesomeIcons.barcode,
color: Colors.white,
),
labelText: "Enter Product Barcode",
hintText: "Enter Product Barcode",
),
onFieldSubmitted: (val) {
_addProduct();
},
),
If you want a TextField to be focused when you open a page, then you can use the property autofocus:
TextField(
autofocus: true,
);
If you want it to be focused later point in time, then you can use the class FocusNode. You can check an example here:
https://flutter.dev/docs/cookbook/forms/focus#focus-a-text-field-when-a-button-is-tapped
You can use the TextFormField's autofocus: parameter and set it to true. This will however make the keyboard appear immediately as well.