How to add search icon to TextField in Flutter? - 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...'),
),
)

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 could show an error message from a textformfield's validator (which is inside Container)?

I have the following issue.
I have a TextFormField where the user has to put some data (for example his email).
I am using a validator , but when the error raises from the TextFormField's validator, the error message and my TextFormField's values are not show properly.
Here is the code:
Container(
height: 60,
margin: EdgeInsets.only(right: 10, left: 10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(10),
),
border: Border.all(
color: const Color(0xFF59C48C),
width: 2.0,
style: BorderStyle.solid),
),
child: Container(
child: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Center(
child: Container(
child: Image.asset("images/email.png",
width: 30, height: 30))),
const SizedBox(
width: 10,
),
Expanded(
child: Center(
child: TextFormField(
validator: (mail) =>
bloc.mailValidator(mail ?? "")
? null
: "Invalid Email",
obscureText: false,
controller: _emailController,
keyboardType:
TextInputType.emailAddress,
style: const TextStyle(
color: Colors.black),
decoration: const InputDecoration(
hintText: "Email",
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 20,
),
labelStyle: TextStyle(
color: Colors.black,
fontSize: 20,
),
),
),
),
),
]),
),
),
),
),
I would like to show the error beside the expanded TextFormField or on the border of it like the image below.
Thank you in advance!
Use OutlineInputBorder with label or labelText
decoration: const InputDecoration(
hintText: "Email",
labelText: "Email",
focusedBorder: OutlineInputBorder(),
enabledBorder: OutlineInputBorder(),
errorBorder: OutlineInputBorder(),
disabledBorder: OutlineInputBorder(),
TextFormField(
autovalidateMode: AutovalidateMode.always,
validator: (mail) {
return "Enter dasdasd"; //handle the error text
},
obscureText: false,
keyboardType: TextInputType.emailAddress,
style: const TextStyle(color: Colors.black),
decoration: const InputDecoration(
hintText: "Email",
labelText: "Email",
border: OutlineInputBorder(),
focusedBorder: OutlineInputBorder(),
enabledBorder: OutlineInputBorder(),
errorBorder: OutlineInputBorder(),
disabledBorder: OutlineInputBorder(),
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 20,
),
),
),

Keyboard appears when click on help icon inside textfield

How do I stop the keyboard from appearing if the user clicks on this help icon... Here is a screenshot
Any type of help will greatly appreciated
Here is my code
TextFormField(
obscureText: false,
controller: urlController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter instagram url';
}
return null;
},
decoration: InputDecoration(
focusedBorder:OutlineInputBorder(
borderSide: const BorderSide(color: Colors.green, width: 1.0),
borderRadius: BorderRadius.circular(8.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
borderSide: BorderSide(width: 1,color: Colors.green),
),
filled: true,
fillColor: Colors.white,
border: InputBorder.none,
labelText: 'Paste Your URL',
suffixIcon: IconButton(
onPressed: (){},
icon: Icon(Icons.help),
iconSize: 30.0,
color: Colors.grey[400],
),
),
)
Use suffix property instead of suffixIcon and if it still doesn't work on its own, wrap it with a IgnorePointer widget.
However, I assume that that ? button will have a onTap function, if so, it should then it shouldn't be a problem.
suffix: IgnorePointer(
child:IgnorePointer(
child: IconButton(
onPressed: () {},
icon: Icon(Icons.help),
iconSize: 30.0,
color: Colors.grey[400],
),
);
),
suffixIcon is now part of the TextField so we cannot do it using focus node as per changes made by flutter team.
However you can create your layout with Stack widget.
Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Stack(alignment: Alignment.centerRight, children: [
TextFormField(
focusNode: focusNode,
obscureText: false,
controller: urlController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter instagram url';
}
return null;
},
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide:
const BorderSide(color: Colors.green, width: 1.0),
borderRadius: BorderRadius.circular(8.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8)),
borderSide: BorderSide(width: 1, color: Colors.green),
),
filled: true,
fillColor: Colors.white,
border: InputBorder.none,
labelText: 'Paste Your URL',
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(
Icons.help,
size: 30.0,
color: Colors.grey,
),
),
])),
Or you can use use CupertinoTextField
try this:
suffixIcon: IconButton(
onPressed: (){
FocusScope.of(context).unfocus();
//or FocusScope.of(context).requestFocus(new FocusNode());
},
icon: Icon(Icons.help),
iconSize: 30.0,
color: Colors.grey[400],
),
[EDITED]
or simple use Stack to avoid touching Textfield when touching icon:
Stack(
children: [
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.only(right: 20.0),
),
),
Positioned(
right: 0,
child: Container(
width: 20,
height: 20,
child: IconButton(onPressed:(){}, icon: Icon(Icons.add),),
),
),
],
),
stack(
children: [
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.only(right: 20.0),
),
),
Positioned(
right: 0,
child: Container(
width: 20,
height: 20,
child: IconButton(onPressed:(){}, icon: Icon(Icons.add),),
),
),
],
),

how can I put my textfield input stay center vertically?

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

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