How do I center the posts in the widget I created? - flutter

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,
);
}

Related

flutter how remove text underline on text selection (TextFormField)?

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),
),

Error message showing inside the text form field in Flutter

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) !

flutter How to modify the content and the height of the cursor in the TextField

The cursor and text in this picture are what I want
But my demo looks like this.The text and cursor are small and not filled
code
TextEditingController searchController = new TextEditingController();
FocusNode focusNode = new FocusNode();
// other code
TextField(
key: Key("buy_subject_input"),
autofocus: true,
focusNode: focusNode,
cursorColor: Colours.default_color,
decoration: InputDecoration(
hintText: 'search',
hintStyle: TextStyle(
color: Colours.hint_text_color,
),
prefixIcon: Icon(
Icons.search,
color: Colours.hint_text_color,
),
fillColor: Colors.white,
filled: true,
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(5)),
),
),
controller: searchController,
),
//other code
After searching everywhere I finally found it you have to give style: TextStyle(
height: 2.0,
), to increase the cursor height.
TextField(
style: TextStyle(
height: 2.0,
),
decoration: InputDecoration(
isDense: true,
contentPadding: EdgeInsets.only(
bottom: 15, top: 15, left: 10, right: 10)),
),
TextField(
cursorHeight: 20, // you can put your ideal height here
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'How to Change Cursor Height'
You can use the CupertinoTextField to achieve this:
CupertinoTextField(
prefix: Icon(Icons.search),
placeholder: 'search',
),
you can play around with input decoration parameter content-padding to reduce space between The limit area of text field and the TextForm style parameter to make text and cursor height bigger.
here is a code sample
TextEditingController searchController =
new TextEditingController(text: 'search');
FocusNode focusNode = new FocusNode();
#override
Widget build(BuildContext context) {
return TextField(
key: Key("buy_subject_input"),
autofocus: true,
focusNode: focusNode,
cursorColor: Colors.black,
style: TextStyle(fontSize: 22, height: 2.0),
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 2.0),
hintStyle: TextStyle(
color: Colors.black,
),
prefixIcon: Icon(
Icons.search,
color: Colors.black,
),
fillColor: Colors.white,
filled: true,
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(5)),
),
),
controller: searchController,
);
}
}
you can also use the cupertino text field to have same layout as iphone
code sample
CupertinoTextField(
prefix: Icon(Icons.search),
placeholder: 'search',
);

How to make resizable TextField?

I want to make TextField resizable vertically and I have one idea about that
Expanded(
child: TextField(
cursorColor: Colors.black,
decoration: new InputDecoration.collapsed(
// border: new OutlineInputBorder(
// borderSide: new BorderSide(color: hexToColor('#5c6277'),style: BorderStyle())
// ),
hintText: "El Baraka",hintStyle: TextStyle(fontFamily: 'RobotoLight',color: Colors.grey,fontSize: 16),
),
),
This can be achieved with maxLines: null:
TextField(
cursorColor: Colors.black,
decoration: new InputDecoration.collapsed(
hintText: "El Baraka",hintStyle: TextStyle(fontFamily: 'RobotoLight',color: Colors.grey,fontSize: 16),
),
maxLines: null,
)
Now TextField will add a new line when your text reaches the edge.

How to focus text field when initstate is initialized

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.