How to align Hint Text into centre in Text Field? - dart

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

Related

Flutter Custom Widget Property Transfer

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

How to set TextField width?

I nested my TextField inside a Container with width 10.0, but the textfield is still taking up the whole width of the device.
Widget _reviewBody() {
return SliverToBoxAdapter(
child: Container(
width: 10.0,
padding: EdgeInsets.only(bottom: 30.0),
child: TextField(
onChanged: (text) {
setState(() {
reviewBody = text;
});
},
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
hintText: 'Add your thoughts here',
hintStyle: TextStyle(color: Burnt.hintTextColor),
enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Burnt.lightGrey)),
focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: Burnt.primaryLight, width: 1.0)),
),
textAlign: TextAlign.center,
),
),
);
}
How do I set the TextField width?
Edit: Turns out SliverToBoxAdapter does magic, so I've used media query to set a padding on my container. Maybe I'm not supposed to use SliverToBoxAdatper, if someone can explain the right thing to do that would be great thanks.
Edit2: Actually I came up with an even better idea, I just put my Container inside a Row and made the Row center my 100.0 wide Container
I tried something like this
Container(
margin: EdgeInsets.only(top: 30, left: 30, right: 30),
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
TextField(
decoration: InputDecoration(
hintText: "Inserisci la tua email",
hintStyle: TextStyle(fontSize: 20),
labelText: "Email",
labelStyle: TextStyle(
color: Utils.blackColor, fontSize: 20),
border: UnderlineInputBorder(
borderSide: BorderSide(color: Utils.greyColor)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
style: BorderStyle.solid))),
controller: nameController,
style: TextStyle(fontSize: 23)),
Padding(padding: EdgeInsets.only(top: 20)),
],
),
),
pu textfield and a container in a column as a child, then wrap children with flexible widget give them flex property

Flutter: Outline input border

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

How to edit spacing between Flutter's TextFormField input and errorText

Is there a way to decrease the spacing between the actual input and the error text in a TextFormField widget? As it stands right now having error texts displayed on the form almost doubles the size of the form and I would like to keep the form area the same size with or without error text. From the pictures below you can see how much change it makes and that there is quite a lot of space between the input and the error message that could use reducing.
Form before errors
Form after errors
Here is an example of one of the formfields
Padding(
padding: EdgeInsets.only(
top: 5.0, bottom: 5.0, left: 25.0, right: 25.0),
child: TextFormField(
focusNode: myFocusNodeName,
controller: signupNameController,
keyboardType: TextInputType.text,
textCapitalization: TextCapitalization.words,
style: TextStyle(
fontFamily: "WorkSansSemiBold",
fontSize: 16.0,
color: Colors.black),
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
FontAwesomeIcons.user,
color: Colors.black,
),
errorText: signupLastNameErrorText,
hintText: "Last Name",
hintStyle: TextStyle(
fontFamily: "WorkSansSemiBold", fontSize: 16.0),
),
validator: (value) =>
value.isEmpty ? 'Last Name can\'t be empty' : null,
onSaved: (value) => _lastname = value,
),
),
Add decoration in TextFormField Widget.
InputDecoration(
contentPadding: EdgeInsets.only(left: 11, right: 3, top: 14, bottom: 14),
errorStyle: TextStyle(fontSize: 9, height: 0.3),
)
There seems to be no way to do it unless you open the source code of InputDecoration (on your sdk folder flutter/packages/flutter/lib/src/material/input_decorator.dart)
look for _buildError
wrap the Text with Container like
Container(
padding: EdgeInsets.only(bottom: 4),
child: Text(
widget.errorText,
style: widget.errorStyle,
textAlign: widget.textAlign,
overflow: TextOverflow.ellipsis,
maxLines: widget.errorMaxLines,
)
)
The vertical padding of errorText is set in input_decorator.dart:
final double helperErrorHeight =
!helperErrorExists ? 0 : helperError.size.height + subtextGap;
The subtextGap is a static constant and equal to 8.0. Unfortunately, there's no easy way to override that value.
My solutions,
You can using:
maxLength
For each TextFeild,
=> You will have space you want when show error,
Affter that you can transparent color counter,
It Worked,
I tried to change text height, but I noticed that the animation is jumping.
I found another solution. Problem located in material/input_decorator.dart file. In _RenderDecoration class. It contains:
static const double subtextGap = 8.0;
I copied all files, remove InputDecorationTheme and changed value to 0.0.
It also contains InputDecoration. I extended InputDecoration and remove override methods:
class CustomInputDecoration extends InputDecoration
And then i used it in the component:
TextFormField(
decoration: CustomInputDecoration(...)
My flutter version: 2.2.1. I noticed that flutter team often rewrites components, so be careful! There may be changes in next releases.

How to make flutter TextField height match parent of container?

I want to make my TextField height the same as my container height. Please check my code below and let me know how can I make TextField match_parent of my container. I've checked this question The equivalent of wrap_content and match_parent in flutter? but I didn't find any solution. I need to make TextField to take full height and width of my container.
new Container(
height: 200.0,
decoration: new BoxDecoration(
border: new Border.all(color: Colors.black)
),
child: new SizedBox.expand(
child: new TextField(
maxLines: 2,
style: new TextStyle(
fontSize: 16.0,
// height: 2.0,
color: Colors.black
),
decoration: const InputDecoration(
hintText: "There is no data",
contentPadding: const EdgeInsets.symmetric(vertical: 40.0),
)
),
),
)
Please check the screenshot below. As said, I need my TextField to take full height of Container
Here is my solution:
Container(
height: 200,
color: Color(0xffeeeeee),
padding: EdgeInsets.all(10.0),
child: new ConstrainedBox(
constraints: BoxConstraints(
maxHeight: 200.0,
),
child: new Scrollbar(
child: new SingleChildScrollView(
scrollDirection: Axis.vertical,
reverse: true,
child: SizedBox(
height: 190.0,
child: new TextField(
maxLines: 100,
decoration: new InputDecoration(
border: InputBorder.none,
hintText: 'Add your text here',
),
),
),
),
),
),
),
It works pretty good for me. And here is a screen shot.
Answering this in 2021. There is an expands property available now. This code works:
Column(
children: [
Expanded(
child: TextField(
maxLines: null,
minLines: null,
expands: true,
),
flex: 1),
],
)
Let's remove a few lines in code and understand how flutter works.
Why we are giving height 200 to Container. Can't the Container adjust the height based on its child (in this case SizedBox.expand)
If we remove height 200, then Container occupied the entire screen because of SizedBox.expand
Do we really need the SizedBox for our use case. Let's remove that also see what happens.
Now our Container wraps the TextField. But there is some space above and below.
Who decided that space? TextField's decoration's contentPadding. Let's remove that also. It looks like below where textField wrapped by Container. Hope this is what you want. If not, please comment, we can tweak a bit and get what you want. Cheers
Final version of code which displays the above image
new Container(
// height: 200.0,
decoration: new BoxDecoration(
border: new Border.all(color: Colors.black)
),
child: new TextField(
maxLines: 2,
style: new TextStyle(
fontSize: 16.0,
// height: 2.0,
color: Colors.black
),
decoration: const InputDecoration(
hintText: "There is no data",
// contentPadding: const EdgeInsets.symmetric(vertical: 40.0),
)
),
)
Currently the only way to achieve the TextField to fill the available vertical space is:
TextField(maxLines: 1000000) //maxlines: any large int
While it is tempting to use TextField(maxLines: null), it will just set the TextField to expand with its content, until it reaches its container limit.
I think there needs to be a bool stretchVertically parameter. TextField(stretchVertically: true) would mean that the TextField will try to fill as much vertical space as it can. stretchVertically and maxLines would have to be mutually exclusive.

Resources