Flutter - how to vertically align text input within a container? - flutter

I am trying to add a search input control to the app header in my Flutter web app.
See...
Widget _buildSearchControl() {
return Container(
alignment: Alignment.centerLeft,
color: Colors.white,
padding: EdgeInsets.only(right: 20),
margin: EdgeInsets.all(10),
width: 300,
child: TextField(
style: TextStyle(backgroundColor: Colors.white, color: Colors.black),
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search,color: Colors.black,),
suffixIcon: IconButton(
icon: const Icon(Icons.clear,color: Colors.black,),
onPressed: () {
/* Clear the search field */
},
),
hintText: 'Search...',
border: InputBorder.none),
),
);
}
However, when entering the text, the characters are not vertically centre aligned as I wpuld expect
Is there a way I can have the characters input to the TextField control vertically centered so that they line up with the icons?

Have you tried this textAlignVertical property?
TextField(
textAlignVertical: TextAlignVertical.center,
....
),

I wrap the Container with Padding.
And add contentPadding: const EdgeInsets.all(5), to InputDecoration
...
Padding(
padding: const EdgeInsets.all(8.0).copyWith(bottom: 0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Theme.of(context).primaryColor, width: 2),
borderRadius: const BorderRadius.all(Radius.circular(10)),
color: Colors.white
),
child: TextFormField(
controller: _controller,
onChanged: (string) {},
style: TextStyle(color: Theme.of(context).primaryColor),
cursorColor: Theme.of(context).primaryColor,
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(5),
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
labelText: 'Enter',
labelStyle: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 16,
fontWeight: FontWeight.w500),
prefixIcon: Icon(
Icons.document_scanner_sharp,
color: Theme.of(context).primaryColor,
),
suffixIcon: Padding(
padding: const EdgeInsets.only(top: 5.0),
child: IconButton(
alignment: Alignment.topLeft,
icon: const Icon(
Icons.clear,
size: 25,
color: Colors.red,
),
tooltip: 'Clear',
onPressed: () {
FocusScope.of(context).unfocus();
_controller.clear();
}
),
)
)
),
),
),

Use this , maybe it depends on your contentPadding
Container(
width: 300,
height: 56,
margin: EdgeInsets.fromLTRB(0, 0, 0, 20),
child: TextFormField(
key: widget.globalKey,
autocorrect: false,
onTap: widget.onClick,
minLines: widget.minLines,
maxLines: widget.maxLines,
textInputAction: TextInputAction.done,
onChanged: (val) => widget.onChangeValue(val),
style: getInputStyle,
initialValue: widget.initialValue,
textAlign: TextAlign.left,
autovalidateMode: AutovalidateMode.onUserInteraction,
controller: widget.controller,
keyboardAppearance: Brightness.dark,
decoration: InputDecoration(
contentPadding: REdgeInsets.fromLTRB(
14, 16, 0, 16),
labelText: widget.labelText,
hintText: widget.hintText,
floatingLabelBehavior: FloatingLabelBehavior.auto,
labelStyle: getLabelStyle,
hintStyle: getLabelStyle,
fillColor: widget.backGroundColor ,
filled: true,
prefix: widget.prefix,
),
),
)

Related

How to change the position of the text in the textformfield?

This is the design I want:
This is my current design:
I am new to using flutter. I want to ask for an opinion on how to make text in the textformfield at what position according to the design I want. I try to use contentPadding: EdgeInsets.symmetric(vertical: 40), and height in textformfield style but still can't solve my problem. Please advise from stack overflow.
This is my code:
Container(
width: double.infinity,
margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30,
child: Text(
'Important patient note',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16),
),
),
SizedBox(
width: 950,
child: TextFormField(
textAlign: TextAlign.start,
style: const TextStyle(color: Colors.black),
controller: importantNotes,
onSaved: (String? value) {
importantNotes.text = value!;
},
decoration: const InputDecoration(
contentPadding: EdgeInsets.all(70.0),
border: OutlineInputBorder(),
hintText: 'Important patient note',
hintStyle: TextStyle(
color: Colors.black, fontSize: 16,),
),
),
)
],
))
Just remove contentPadding: EdgeInsets.all(70.0),
SizedBox(
width: 950,
child: TextFormField(
textAlign: TextAlign.start,
maxLines : 5, // add this
style: const TextStyle(color: Colors.black),
controller: importantNotes,
onSaved: (String? value) {
importantNotes.text = value!;
},
decoration: const InputDecoration(
contentPadding: EdgeInsets.all(70.0), // remove or set it to 0
border: OutlineInputBorder(),
hintText: 'Important patient note',
hintStyle: TextStyle(
color: Colors.black, fontSize: 16,),
),
)),
try this one
Widget textfield(
String hint, TextEditingController _controller, bool obsecurtext) {
return Container(
width: MediaQuery.of(context).size.width - 70,
height: 60,
child: TextFormField(
obscureText: obsecurtext,
controller: _controller,
decoration: InputDecoration(
labelStyle: TextStyle(fontSize: 15),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(21),
borderSide:
BorderSide(width: 1, color: Color.fromARGB(255, 226, 135, 230)),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(21),
borderSide:
BorderSide(width: 1, color: Color.fromARGB(255, 231, 127, 127)),
),
hintText: hint,
),
),
);
}
usage textfield("Email", _email, false)

TextField auto resize flutter

How to auto resize TextField when you type in long messages like whatsapp?
Here is my TextField code:
Row(
children: [
Expanded(
flex: 1,
child: Icon(
Icons.tag_faces,
color: Colors.grey,
size: 30,
),
),
Expanded(
flex: 8,
child: TextField(
controller: inputTextController,
style: TextStyle(
color: Colors.black
),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Type a message',
hintStyle: TextStyle(
color: Color(0xff99999B),
)
),
),
),
Expanded(
flex: 1,
child: IconButton(
icon: Icon(
Icons.camera_alt,
color: Colors.grey,
size: 30,
),
padding: EdgeInsets.all(0),
onPressed: () {
//print(inputTextController.text);
},
),
)
],
),
Tried using maxLines: null and it actually works for the text/message to auto entered but the textfield wouldn't resize vertically.
Below is my implementation:
Container(
margin: const EdgeInsets.only(top: 20, bottom: 8),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.1),
borderRadius: BorderRadius.circular(2),
),
constraints: BoxConstraints(
minWidth: MediaQuery.of(context).size.width,
maxWidth: MediaQuery.of(context).size.width,
minHeight: 25.0,
maxHeight: 100.0,
),
child: Scrollbar(
child: TextField(
keyboardType: TextInputType.multiline,
textInputAction: TextInputAction.done,
onSubmitted: (value) {
},
maxLines: null,
// focusNode: focusNode,
controller: _message,
style: const TextStyle(color: Colors.white),
decoration: const InputDecoration(
border: InputBorder.none,
contentPadding:
EdgeInsets.symmetric(horizontal: 13, vertical: 13),
hintText: "Message(optional)",
hintStyle:
TextStyle(color: Colors.white24, fontSize: 14),
),
),
),
),

Drop down inside text field flutter

This is what I have currently:
My design / what I want it to be:
This is my code and this sized box is a part of a column which is a part of a form:
SizedBox(
height: 80.0,
child: Stack(alignment: Alignment.centerRight, children: [
TextFormField(
controller: null,
style: const TextStyle(color: Colors.white),
maxLength: 2,
decoration: InputDecoration(
fillColor: const Color(0xff353251),
filled: true,
hintText: 'ex. 5 hours',
contentPadding: const EdgeInsets.all(20.0),
hintStyle: const TextStyle(color: Colors.white24),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0)))),
DropdownButton(
value: goalValue,
dropdownColor: const Color(0xff403A4F),
style: const TextStyle(color: Colors.white),
onChanged: (String? value) {
setState(() {
goalValue = value!;
});
},
items: <String>['Per Week', 'Per Day', 'Per Month']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList()),
]),
),
Thanks in advance!
Instead of Stack, I am using Container for outer decoration and Row for placing two widgets. There are many changes occur during styling the property and used random color, play with styling.
Now The result is
Container(
alignment: Alignment.center,
padding: const EdgeInsets.only(left: 16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15.0),
color: Color(0xff353251),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Flexible(
flex: 2,
child: TextField(
controller: null,
style: TextStyle(color: Colors.white),
maxLength: 2,
buildCounter: null,
decoration: InputDecoration(
fillColor: Color(0xff353251),
filled: true,
counterText: "",
hintText: 'ex. 5 hours',
contentPadding: EdgeInsets.only(right: 4),
hintStyle: TextStyle(color: Colors.white24),
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
),
),
),
Flexible(
flex: 1,
child: Container(
padding: EdgeInsets.only(right: 16, left: 16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15.0),
color: Colors.purpleAccent,
),
child: Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.purpleAccent,
),
child: DropdownButton(
borderRadius: BorderRadius.circular(15.0),
underline: const SizedBox(),
icon: const SizedBox(),
value: goalValue,
style: const TextStyle(color: Colors.white),
hint: const Text(
"select",
style: TextStyle(color: Colors.white),
),
onChanged: (String? value) {
setState(() {
goalValue = value!;
});
},
items: <String>['Per Week', 'Per Day', 'Per Month']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList()),
),
),
),
],
),
),

Flutter TextField with maxLength and

I need a Text Area with an enforced maxLength of 250 chars and its label aligned with the hint text. My code is below.
However, the widget is rendering the text on top of the hint. Does anyone know a solution?
It's like if the counter widget forces the actual text field to move upwards.
TextField with maxLength = 250 and alignLabelWithHint = true
TextField(
expands: false,
minLines: null,
maxLines: null,
maxLengthEnforced: maxLengthEnforced,
maxLength: maxLength,
controller: controller,
onChanged: (String value){
print(value);
},
cursorColor: Colors.deepOrange,
keyboardType: keyboardType,
decoration: InputDecoration(
alignLabelWithHint: true,
labelText: text,
labelStyle: TextStyle(color: Colors.grey),
hintText: text,
prefixIcon: Container(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 15.0),
child: Material(
elevation: 0,
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(30)),
child: Icon(
icon,
color: Colors.red,
),
),
),
],
),
),
border: InputBorder.none,
contentPadding: EdgeInsets.only(right: 10, top: 5, bottom: 5)),
)
The purpose of a hint is to disappear when the user start typing I tried your code and it worked with no problem.
TextField(
expands: false,
minLines: null,
maxLines: null,
maxLengthEnforced: true,
maxLength: 250,
// controller: controller,
onChanged: (String value) {
print(value);
},
cursorColor: Colors.deepOrange,
// keyboardType: keyboardType,
decoration: InputDecoration(
alignLabelWithHint: true,
labelText: "hi",
labelStyle: TextStyle(color: Colors.grey),
hintText: "hi",
prefixIcon: Container(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 15.0),
child: Material(
elevation: 0,
color: Colors.white,
borderRadius:
BorderRadius.all(Radius.circular(30)),
child: Icon(
Icons.highlight,
color: Colors.red,
),
),
),
],
),
),
border: InputBorder.none,
contentPadding:
EdgeInsets.only(right: 10, top: 5, bottom: 5)),
),

How to hide the things behind the textfield border?

I am trying to make a textfield with rounded border and some drop shadow,
when i use elevation it shows some parts outside the border, please have alook in the image i had attached.
Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 30.0,
child: Material(
elevation: 2.0,
shadowColor: Colors.grey,
child: TextField(
autofocus: false,
style: TextStyle(
color: Colors.black,
),
decoration: kTextFieldDecorationCircular,
onChanged: (value){
searchWord = value;
},
onEditingComplete: searchTheWord,
),
),
),
),
);
const kTextFieldDecorationCircular = InputDecoration(
contentPadding: EdgeInsets.all(2.0),
filled: true,
fillColor: Colors.white,
prefixIcon: Icon(Icons.search, color: Colors.grey,),
hintText: 'Search',
hintStyle: TextStyle(color: Colors.grey),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(50.0)),
),
);
This is my code.
Thank you in advance.
You could add this to your Material widget:
borderRadius: BorderRadius.all(Radius.circular(50.0)),