how can I put my textfield input stay center vertically? - flutter

I want to put a textFiled in a Container. But I found the input content couldn't stay vertically certer(a bit higher) in the Container but the two icons displayed well. Is there a solution to my situation?
Container(
width: ScreenAdapt.widthToDp(330),
height: ScreenAdapt.heightToDp(40),
alignment: Alignment.centerLeft,
child: TextField(
controller: inputController,
focusNode: focusNode,
style: MyTextStyle.level4,
keyboardType: TextInputType.number,
decoration: InputDecoration(
prefixIcon: MyIcon.SearchGreyIcon,
hintText: 'hint text here',
hintStyle: MyTextStyle.grey,
border: InputBorder.none,
suffixIcon: focusNode.hasFocus ? IconButton(
icon: MyIcon.CloseIcon,
onPressed: () => inputController.clear(),
) : Container(),
),
),
decoration: BoxDecoration(
color: MyColor.MidGray,
borderRadius: MyStyle.CIRCULAR_BORDER_RADIUS,
),
),

try this as #Assassin Suggested
InputDecoration(
prefixIcon: MyIcon.SearchGreyIcon,
hintText: 'hint text here',
hintStyle: MyTextStyle.grey,
border: InputBorder.none,
filled: true,
suffixIcon: focusNode.hasFocus
? IconButton(
icon: MyIcon.CloseIcon,
onPressed: () => inputController.clear(),
)
: Container(),
)

Related

how to add bottom TextFormField in flutter like send comment buttton

child: Row(
children: [
// First child is enter comment text input
TextFormField(
controller: commentController,
autocorrect: false,
decoration: new InputDecoration(
labelText: "Some Text",
hintText: "Write a comment",
labelStyle:
TextStyle(fontSize: 20.0, color: Colors.white),
fillColor: Colors.blue,
border: OutlineInputBorder(
// borderRadius:`your text`
// BorderRadius.all(Radius.zero(5.0)),
borderSide:
BorderSide(color: Colors.purpleAccent)),
),
),
// Second child is button
IconButton(
icon: Icon(Icons.send),
iconSize: 20.0,
onPressed: () {
},
)
]
)
Try below code and use suffixIcon widget is best for your design
TextFormField(
autocorrect: false,
decoration: InputDecoration(
suffixIcon: IconButton(
icon: Icon(Icons.send),
iconSize: 20.0,
onPressed: () {},
),
labelText: "Some Text",
hintText: "Write a comment",
fillColor: Colors.blue,
border: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.purpleAccent),
),
),
),
Your code result->
My above code result->

How to add search icon to TextField in Flutter?

Is it possible to display hint in the TextField as in the image below?
Try below code hope its helpful to you. Used prefixIcon for that.
TextField(
decoration: InputDecoration(
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(),
hintText: 'Search Tech Talk',
),
),
Your result screen->
there is no proper way to add icon in hint, but you can try this alternative, use rich text on textfield as hint text, hide when tap on textfield and show with condition when textfield is empty and keyboard is hide:
Stack(
alignment: AlignmentDirectional.center,
children: [
Offstage(
offstage: _isHide,
child: IgnorePointer(
ignoring: true,
child: Text.rich(
TextSpan(
children: [
WidgetSpan(
child: Icon(
Icons.search,
color: Colors.grey,
),
),
TextSpan(
text: "blablablablabla",
style: TextStyle(color: Colors.grey),
),
],
),
),
),
),
TextField(onTap: () {
_isHide = true;
setState(() {});
}),
],
),
TextField(
onChanged: (value){
searchData(st = value.trim().toLowerCase());
// Method For Searching
},
decoration: InputDecoration(
hintText: "Search Data",
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(7.0)),
),
),
),
RESULT
Make use of the Prefix icon property and content padding in the textfield widget
SizedBox(
height: 40.h,
child: TextField(
decoration: InputDecoration(
fillColor: ColorUtils.COLOR_GRAY_AAAAAA[12],
filled: true,
contentPadding: EdgeInsets.symmetric(vertical: 6.h, horizontal: 12.w),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8.r)),
borderSide: BorderSide.none,
),
hintText: 'input_order_code'.tr,
hintStyle: TextStyleUtils.sizeText15Weight400().copyWith(color: ColorUtils.COLOR_GRAY_AAAAAA),
prefixIcon: Icon(Icons.search),
),
style: TextStyleUtils.sizeText15Weight400().copyWith(color: ColorUtils.COLOR_GRAY_363636),
),
),
SizedBox(
height: 50,
child: TextField(
cursorHeight: 25,
decoration: InputDecoration(
prefixIcon: Icon(Icons.search),
fillColor: Colors.white,
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
),
hintText: 'Search Here...'),
),
)

How to disable TextFormField default animation

I want to disable the default behavior when a user focus on the text field, where the label text gets smaller and docks to the upper left :
I want to disable this behavior and only make the label text disappear, how can I do it?
Use FloatingLabelBehavior.never inside InputDecoration will help you hiding your label and instead of labelText you can use hintText so it becomes disappear when you type something.
TextFormField(
decoration: InputDecoration(
hintText: "Search",
floatingLabelBehavior: FloatingLabelBehavior.never,
border: InputBorder.none,
suffixIcon: Icon(
Icons.search,
),
),
),
Try out this
TextFormField(
cursorColor: Colors.black,
keyboardType: TextInputType.text,
decoration: new InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
contentPadding:
EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
hintText: "Hints"),
)
And customize textfield decoration as you desired.
You can actually add a leading widget inside TextFormField where you can put the country picker.
Row(
children: <Widget>[
Expanded(
child: FormBuilderTextField(
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(context),
FormBuilderValidators.minLength(context, 9),
FormBuilderValidators.maxLength(context, 9),
FormBuilderValidators.numeric(context),
]),
name: "mobile_number",
keyboardType: TextInputType.phone,
decoration: InputDecoration(
prefixIcon: Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: CountryPickerDropdown(
initialValue: 'AU',
itemBuilder: _buildDropdownItem,
itemFilter: (c) => [
'AU',
'PH',
].contains(c.isoCode),
priorityList: [
CountryPickerUtils.getCountryByIsoCode('AU'),
CountryPickerUtils.getCountryByIsoCode('PH'),
],
sortComparator: (Country a, Country b) =>
a.isoCode.compareTo(b.isoCode),
onValuePicked: (Country country) {
print("${country}");
},
),
),
labelText: 'Mobile #',
floatingLabelBehavior: FloatingLabelBehavior.never,
),
)),
],
),

Flutter wrong position of text in textfield after changing height

According to my design I have a TextField with 30pt of height.
Here's how I am trying to do that:
Container(
height: 30,
child: new TextField(
textAlign: TextAlign.start,
decoration: new InputDecoration(
prefixIcon: Icon(Icons.search),
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(30.0),
),
),
filled: true,
hintStyle: new TextStyle(color: Colors.grey[800]),
hintText: "Search",
fillColor: Colors.lightGreen),
)),
And here's the result:
How can I align hint and text vertically?
Set contentPadding to EdgeInsets.symmetric(horizontal: 0) in InputDecoration. This will remove the default horizontal padding for the Text:
Container(
height: 35,
child: TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(horizontal: 0),
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0),
),
),
filled: true,
hintStyle: TextStyle(color: Colors.grey[800]),
hintText: "Search",
fillColor: Colors.lightGreen),
),
),

Flutter: how to make a TextField with HintText but no Underline?

This is what I'm trying to make:
In the Flutter docs for Text Fields (https://flutter.io/text-input/) it says you can remove the underline by passing null to the decoration. However, that also gets rid of the hint text.
I do not want any underline whether the text field is focused or not.
UPDATE: updated accepted answer to reflect changes in Flutter SDK as of April 2020.
Do it like this:
TextField(
decoration: new InputDecoration.collapsed(
hintText: 'Username'
),
),
or if you need other stuff like icon, set the border with InputBorder.none
InputDecoration(
border: InputBorder.none,
hintText: 'Username',
),
),
New Flutter SDK since after integration of web and desktop support you need to specify individually like this:
TextFormField(
cursorColor: Colors.black,
keyboardType: inputType,
decoration: InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
contentPadding:
EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
hintText: "Hint here"),
)
Here is a supplemental answer that shows some fuller code:
Container(
decoration: BoxDecoration(
color: Colors.tealAccent,
borderRadius: BorderRadius.circular(32),
),
child: TextField(
decoration: InputDecoration(
hintStyle: TextStyle(fontSize: 17),
hintText: 'Search your trips',
suffixIcon: Icon(Icons.search),
border: InputBorder.none,
contentPadding: EdgeInsets.all(20),
),
),
),
Notes:
The dark background (code not shown) is Colors.teal.
InputDecoration also has a filled and fillColor property, but I couldn't get them to have a corner radius, so I used a container instead.
I found no other answer gives a border radius, you can simply do it like this, no nested Container
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(20),
),
),
);
change the focused border to none
TextField(
decoration: new InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
contentPadding: EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
hintText: 'Subject'
),
),
To make a Borderless TextFormField i found below solution:
It is without using container.
TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
borderSide: BorderSide.none,
),
labelText: "Student email/id",
floatingLabelStyle: const TextStyle(
height: 4,
color: Color.fromARGB(255, 160, 26, 179)),
filled: true,
fillColor: Colors.grey[200],
prefixIcon: const Icon(Icons.person),
),
),
Sample Normally:
While having input error:
While user input:
TextField(style: TextStyle(color: Colors.black45,fontSize: 18,decorationThickness: 0.0)). It's showing without underline with decorationThickness:0.0.
Container(
height: 50,
// margin: EdgeInsets.only(top: 20),
decoration: BoxDecoration(
color: Colors.tealAccent,
borderRadius: BorderRadius.circular(32)),
child: TextFormField(
cursorColor: Colors.black,
// keyboardType: TextInputType.,
decoration: InputDecoration(
hintStyle: TextStyle(fontSize: 17),
hintText: 'Search your trips',
suffixIcon: Icon(Icons.search),
border: InputBorder.none,
contentPadding: EdgeInsets.all(18),
),
),
),
Default
Container(
padding: const EdgeInsets.all(20),
child: const TextField(
decoration: InputDecoration(
border: UnderlineInputBorder(), hintText: "Search Your tips"),
),
),
Outline Border
Container(
padding: const EdgeInsets.all(20),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(40),
),
hintText: "Search Your tips",
),
),
),
No Border
Container(
padding: const EdgeInsets.all(20),
child: const TextField(
decoration: InputDecoration(
border: InputBorder.none, hintText: "Search Your tips"),
),
),
Try the following code:
TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(borderSide: BorderSide.none, borderRadius: BorderRadius.circular(30.0)),
hintText: "Search your trips",
hintStyle: const TextStyle(color: Colors.white, fontWeight: FontWeight.w300),
filled: true,
fillColor: Colors.cyan[200],
suffixIcon: IconButton(
onPressed: () {},
icon: const Icon(Icons.search, color: Colors.white),
),
),
),
decoration: InputDecoration(
border:OutLineInputBorder(
borderSide:BorderSide.none
bordeRadius: BordeRadius.circular(20.0)
)
)
To remove underline border: InputBorder.none,
For hint use hintText: 'Hint Text'
TextFormField(
InputDecoration(
border: InputBorder.none,
hintText: 'Hint Text',
),
),
To remove TextField underline in Flutter, you can simply use InputBorder.none.
TextField(
decoration: InputDecoration(
hintText: 'Hint Text',
border: InputBorder.none,
),
)