I am trying to change the underline color of a textfield when it's inactive/not focused. I am not sure where to make this change, InputDecorationTheme is only changing the underline color for when it's selected. How do I achieve this?
inputDecorationTheme: new InputDecorationTheme(
labelStyle: new TextStyle(
color: Colors.blue[200],
),
hintStyle: new TextStyle(color: Colors.grey),
),
I am trying to change this color the textfield to a lighter grey when it's not selected/out of focus.
For those who might need to achieve something similar, change the hintColor in your Theme widget.
new Theme(
data: new ThemeData(
//this changes the colour
hintColor: Colors.grey,
inputDecorationTheme: new InputDecorationTheme(
labelStyle: new TextStyle(color: Colors.blue))));
We can use InputDecoration
Try this way
new TextFormField(
controller: passTextController,
decoration: new InputDecoration(
labelText: "Enter password",
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
// when the TextFormField in unfocused
) ,
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.blue),
// when the TextFormField in focused
) ,
border: UnderlineInputBorder(
)
),
keyboardType: TextInputType.text,
obscureText: true,
),
OUTPUT
Related
I want to create a custom widget for TextField widget and I want to transfer all properties to my custom widget. I have used in React Native ...props syntax.
My custom Widget:
return Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.grey,
width: 1,
),
borderRadius: BorderRadius.all(Radius.circular(5))),
margin: margin,
padding: EdgeInsets.symmetric(horizontal: 10),
child: TextField(
decoration: InputDecoration(
labelStyle: TextStyle(color: Colors.black),
labelText: label,
border: InputBorder.none,
counterText: ""),
textDirection: TextDirection.ltr,
//I want to put here custom widget properties
),
);
I want to use custom widget like:
Input(
label: "Şifre",
margin: EdgeInsets.only(bottom: marginBottom),
obscureText: true, //**
textInputAction: TextInputAction.send, //**
controller: passwordController, //**
)
I want to transfer commented ** properties to custom widget commented line
You are using a TextField but look for a TextFormField instead. It gathers all the properties you are asking for.
TextFormField(
decoration: InputDecoration(
labelStyle: TextStyle(color: Colors.black),
labelText: label,
border: InputBorder.none,
counterText: ""),
obscureText: true,
textInputAction: TextInputAction.send,
controller: passwordController,
);
I want to change the color of the prefixIcon to same as the focusedInputBorder color. I have tried with ThemeData in very root of the app but no luck!
The color of the prefixIcon is controlled via the primaryColor of your theme.
You can adjust it only for the TextField by wrapping it inside of a Theme:
Theme(data: Theme.of(context).copyWith(primaryColor: Color(..)), child: TextField(..))
I am new to flutter. But when I tried as below it changes the preFixIcon color: (do not mind the indentation. It is not well indented)
TextField(
obscureText: false,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
fontWeight: FontWeight.bold
),
decoration: new InputDecoration(
prefixIcon: Icon(Icons.vpn_key,
color: Colors.white,),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all( Radius.circular(20)),
borderSide: BorderSide( width: 1, color: Colors.white),),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide( width: 1, color: Colors.white),),
hintText: 'Password',
hintStyle: TextStyle(color: Colors.white,fontSize: 15)
),
)
Below is the expected output I got:
List<FocusNode> _focusNodes = [
FocusNode(),
FocusNode(),
];
#override
void initState() {
_focusNodes.forEach((node){
node.addListener(() {
setState(() {});
});
});
super.initState();
}
TextFormField(
focusNode: _focusNodes[0],
decoration: InputDecoration(
prefixIcon: Icon(
Icons.alternate_email,
color: _focusNodes[0].hasFocus ? Colors.green : Colors.grey,
),
hintText: "Email"),
),
Flutter 3.3.2
can do like this.
Icon(
...
color: FocusScope.of(context).isFirstFocus ? colorA : colorB
...
)
I was trying to build a border for my text field like:
return TextField(
...
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.red,
width: 5.0),
)
)
)
But it always return a black border with 1.0 as width.
The only way that I found to change the color was to create a ThemeData where I specify the hint color, but I could not find a way to change my width.
What your looking for is - enabledBorder property of InputDecoration.
If you want to Change Border on focus use - focusedBorder
TextField(
decoration: new InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.greenAccent, width: 5.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red, width: 5.0),
),
hintText: 'Mobile Number',
),
),
For others coming here who just want a TextField with a border all around it:
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
),
),
You can change the outlined variant of TextField or TextFormField globally by overriding the ThemeData at the top level:
return MaterialApp(
theme: ThemeData(
inputDecorationTheme: const InputDecorationTheme(border: OutlineInputBorder()),
),
)
Live Demo
i am trying to add alignment to the hint-text inside Text-field.but it is not working. How to bring it to centre??
How to write text in curved shape???
Container(
child: new TextFormField(
controller: _pass,
textAlign: TextAlign.center,
decoration: new InputDecoration.collapsed(
hintText: " PASSWORD", ), )),
use textAlign: TextAlign.center, inside TextFormField or textfield
here is the code
The Output Like This
My problem was the auto content padding:
TextField(
textAlign: TextAlign.center,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(0),
hintText: "hola mundo",
),
),
Happy coding.
The textHint will have the same Alignment as the input text. So you can try the code below to achieve what you're looking for:
TextFormField(
keyboardType: TextInputType.number,
maxLength: 5,
textAlign: TextAlign.center,
autofocus: true,
initialValue: '',
decoration: InputDecoration(
hintText: 'some hint',
counterText: "",
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5.0)),
),
);
Note: specific case, but might help others who tried everything else from this thread.
I had a design from which I copied all the paddings and alignments, but I somehow couldn't center the hint text, it was always a few pixels closer to the top edge.
The only thing that worked was finding out that there was a specific text height set in the design. After I applied that, everything aligned perfectly.
InputDecoration(
hintStyle: TextStyle(
fontSize: _smallFontSize, // or whatever
height: 1.4, // <----- this was the key
),
// other properties...
);
you can add textAlign: TextAlign.center to your code
here is my code and its works:
new Padding(
padding: new EdgeInsets.all(30.0),
child:
new TextField(
textAlign: TextAlign.center,
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(color: Colors.teal)),
hintText: 'REF: 2',
),
),
),
all of the answers work for the fixed-sized text fields but in my case, I m using a dynamically sized container that parents my text field
AnimatedContainer(
width: (!widget.scrollController.hasClients ||
widget.scrollController.positions.length > 1)
? MediaQuery.of(context).size.width.toDouble()
: max(
MediaQuery.of(context).size.width -
widget.scrollController.offset.roundToDouble(),
(MediaQuery.of(context).size.width - 80.w).toDouble()),
duration: const Duration(milliseconds: 150),
constraints: BoxConstraints(maxHeight: 40.h),
margin: EdgeInsets.zero,
child: TextField(
controller: controller,
expands: true,
minLines: null,
maxLines: null,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
filled: true,
fillColor: MyAppColors.primaryColor1,
border: UnderlineInputBorder(
borderRadius:
BorderRadius.circular(15.w)),
prefixIcon: Icon(
Icons.search,
size: 20.w,
color: Theme.of(context).accentColor,
),
hintText: 'Songs, albums or artists',
contentPadding: EdgeInsets.zero,
hintStyle:
Theme.of(context).textTheme.subtitle2!.copyWith(
color: Colors.white.withOpacity(0.8),
fontSize: 18.sp,
)),
),
);
the solution that worked for me is that I set expand parameter to true and set these parameters:
minLines: null,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.center
the min and max line must be set to null in order to expand.
that is the only solution I found cuz in another way my hint text gain extra padding even I set the content padding to zero my hint text is not centered in other screen sizes.
You can use following attribute of TextFormField:
textAlign: TextAlign.center
This used to work earlier but now it doesn't. There's a recently opened issue in flutter repo which you can follow
Edit
Now works as expected.
textAlign: TextAlign.center will move your focus too, i fix it with some empty spaces in hint text
hintText: ' this is centred text)',
I'm new to both flutter & dart. Currently, using this in one of my personal projects.
In all of my form, the underline of textField is showing in blue color. I want to change that to some other color. The piece of code which I'm using is like...
new TextField(
controller: this._emailController,
decoration: new InputDecoration(
hintText: "Enter your email",
labelText: "Email",
labelStyle: new TextStyle(
color: const Color(0xFF424242)
)
),
),
Can't able to understand how to achieve this.
Note: I know there is a similar question at here Change TextField's Underline in Flutter. But, at there also it has not completely solved. Also, one more link which looks similar to mine at here Changing EditText bottom line color with appcompat v7 but, that actually belong to Android development by using JAVA not DART(flutter) which I'm using for my android app development. So, please don't get confused about those links.
Just used -:
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.cyan),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.cyan),
),
),
it works for me :)
** See update below or see the answer by #GJJ2019 **
The logical answer would be to use an InputBorder, particularly an UnderlineInputDecorator, and pass it in to the inputdecorator as the border. However, all this does is tell the InputDecorator whether is should use an underline or whatever else you specify.
The actual color is based on the theme - from the source:
Color _getActiveColor(ThemeData themeData) {
if (isFocused) {
switch (themeData.brightness) {
case Brightness.dark:
return themeData.accentColor;
case Brightness.light:
return themeData.primaryColor;
}
}
return themeData.hintColor;
}
So to change the colour do something like this (or specify the theme for your entire application):
new Theme(
data: new ThemeData(
primaryColor: Colors.red,
accentColor: Colors.orange,
hintColor: Colors.green
),
child: new TextField(
decoration: new InputDecoration(
hintText: "Enter your email",
labelText: "Email",
labelStyle: new TextStyle(color: const Color(0xFF424242)),
border: new UnderlineInputBorder(
borderSide: new BorderSide(
color: Colors.red
)
)
),
),
),
UPDATE:
This is now possible to do in the way you'd expect it to work.
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: theColor),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: theColor),
),
border: UnderlineInputBorder(
borderSide: BorderSide(color: theColor),
),
)
To change the color for the entire app, use ThemeData's inputDecorationTheme property.
To use the color only when the input is in focus (i.e. clicked & ready to type):
MaterialApp(
...
theme: ThemeData(
inputDecorationTheme: InputDecorationTheme(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red)
),
)
)
)
To always use the color (even when not in focus), set enabledBorder and border as well:
MaterialApp(
...
theme: ThemeData(
inputDecorationTheme: InputDecorationTheme(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red)
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red),
),
border: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red),
),
)
)
)
decoration: new InputDecoration(
labelText: "Email",
suffixIcon: Icon(Icons.email),
labelStyle: TextStyle(color: Colors.red),
enabledBorder: new UnderlineInputBorder(
borderSide: new BorderSide(color: Colors.red)
)
)
By using InputDecoration, you can set underline color as per your requirement.
TextField(
decoration: InputDecoration(
hintText: "Enter your email",
// [enabledBorder], displayed when [TextField, InputDecoration.enabled] is true
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.lightBlueAccent),
),
//[focusedBorder], displayed when [TextField, InputDecorator.isFocused] is true
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.redAccent),
),
)
)
need change three border.
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: _type.color),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: _type.color),
),
border:
OutlineInputBorder(borderSide: BorderSide(color: _type.color)),
with focusedBorder property in TextField can change underline color like :
focusedBorder: new UnderlineInputBorder( borderSide: new BorderSide(color: Colors.black), ),
#override
ThemeData appBarTheme(BuildContext context) {
return Theme.of(context).copyWith(
primaryColor: Colors.transparent,
appBarTheme: AppBarTheme(elevation: 0),
inputDecorationTheme: InputDecorationTheme(
border: UnderlineInputBorder(
borderSide: BorderSide(style: BorderStyle.none, width: 0))));
}