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.
Related
It is necessary to move the cursor to the end of the text, but in flutter this is strangely implemented through text selection, I do it like this
textController.selection =
TextSelection.collapsed(offset: textController.text.length);
it works, but when typing, it appears underlined
It is possible to somehow remove the underlining of the text, I read the documentation, but did not find it.
My TextFormField
TextFormField(
cursorColor: Colors.white,
style: const TextStyle(
color: Colors.white,
fontSize: 20,
),
controller: textController,
autofocus: false,
decoration: const InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white)),
filled: true,
isDense: true,
hintText:
'search',
hintStyle: TextStyle(
//Style of hintText
color: Colors.white60,
fontSize: 20,
),
),
),
This might help you:
TextField(
style: const TextStyle(
decoration: TextDecoration.none),
),
I have implemented a text form field with validation, and it returns the error message but it gets displayed inside the text form field. Is there a way to show it outside the text form field?
Container(
alignment: Alignment.center,
margin: const EdgeInsets.symmetric(
horizontal: 20),
decoration: BoxDecoration(
color: Colors.grey.withOpacity(0.1),
borderRadius: const BorderRadius.all(
Radius.circular(10))),
child: TextFormField(
autovalidateMode:
AutovalidateMode.onUserInteraction,
controller: _controller,
validator: validatePhone,
textAlignVertical: TextAlignVertical.center,
style: const TextStyle(
color: primaryText,
fontFamily: 'InterMedium',
fontSize: 15),
decoration: const InputDecoration(
counterText: '',
errorStyle: TextStyle(
fontFamily: 'InterMedium',
),
prefixIcon: Padding(
padding: EdgeInsets.only(
left: 20, right: 15),
child: Icon(
Icons.phone_android_rounded,
color: secondaryText,
),
),
hintText: 'Enter mobile number',
hintStyle: TextStyle(
fontFamily: 'InterMedium',
color: secondaryText,
),
border: InputBorder.none,
),
cursorColor: secondaryColor,
maxLines: 1,
maxLength: 10,
keyboardType: TextInputType.phone,
textInputAction: TextInputAction.done,
),
),
How it is now -
How I want it to look like -
Remove parent widget Container and add decoration property of TextFormField like below example !
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
cursorColor: Colors.black,
validator: validator,
controller: controller,
keyboardType: TextInputType.phone,
style: const TextStyle(fontSize: 16, color: Colors.black, fontFamily: 'Helvetica', fontWeight: FontWeight.normal),
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xff13367A)),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xff13367A)),
),
border: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xff13367A)),
),
hintText: hint)),
),
P.S :- If you want to add grey color in your TextFormField then you may use decoration: InputDecoration(fillColor: Colors.grey, filled: true) !
Like I want to use the Textfield only for text only, not numbers. So how to disable that?
title: TextField(
textCapitalization: TextCapitalization.sentences,
style: TextStyle(color: Colors.white),
controller: searchController,
decoration: InputDecoration(
hintText: 'Search for staff name',
hintStyle:
TextStyle(color: Colors.white, fontWeight: FontWeight.w400),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
filled: true,
enabled: true,
prefixIcon: Icon(
Icons.person_pin,
color: Colors.white,
size: 30,
),
suffixIcon: IconButton(
icon: Icon(Icons.clear, color: Colors.white),
onPressed: emptyTheTextformfield,
)),
),
Try this
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter(RegExp(r"[a-zA-Z]+|\s")),
],
Looking to have a TextField which has multiple rows but also an underline for each row. When typing text it should continue to the next row without needing the return key.
Current:
TextField(
maxLines: 2,
decoration: InputDecoration(
enabledBorder: new UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
width: 1.0,
style: BorderStyle.solid),
),
),
)
Current Output:
Desired Output:
this will work for you I guess
TextField(
keyboardType: TextInputType.multiline,
minLines: 100,
maxLines: 500,
style: TextStyle(
decoration: TextDecoration.underline,
),
decoration: InputDecoration(
enabledBorder: InputBorder.none,
hintText: 'Notes.....',
hintStyle: TextStyle(color: Colors.black87),
),
),
If you'd still like to change the color or type or density of the underline use the decorationStyle,decorationColor, and decorationThickness properties.
Here, I've one text field which I want to keep always in focus and I don't want to open keyboard when the text field is clicked. in short text field always needs to be in focus.
Here is the my text field code.
TextFormField(
style: TextStyle(
color: Colors.white,
fontSize: orientation == Orientation.portrait
? MediaQuery.of(context).size.width * 0.030
: MediaQuery.of(context).size.width * 0.020,
),
validator: (val) => Validators.validateRequired(
val, " Product Baarcode"
),
controller: _addproduct,
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();
},
),
You can set the autofocus property to true so that the cursor will start showing as soon as the app starts.
For more info please refer to this link
TextField(
autofocus: true,
)