I have a textfiled in container, and i want the textfiled support multiline, so i can not the the height of the container. but how to set the default height of the textfield when there is no word?
I have try the contentPadding, but not work.
here is my code:
TextField(
controller: _textEditingController,
decoration: InputDecoration(
hintText: "留下你的评论吧~",
contentPadding: EdgeInsets.only(left: getMyWidth(10), right: getMyWidth(10)),
fillColor: default_bg,
filled: true,
border: OutlineInputBorder(borderSide: BorderSide.none, borderRadius: BorderRadius.all(Radius.circular(getMyWidth(5)))),
),
style: makeBlackStyle(),
keyboardType: TextInputType.multiline,
maxLines: null,
cursorColor: themeColor,
onChanged: (str) {
setState(() {
inputStr = str;
});
},
)
so, my question is: How to set the default height of the textfield when there is no word? I want to decrease the default height of the textfiled , and need support mulitiline
You can use 'minLines: 2', it will give you the height of the number of lines you put.
Hope it help.
just set isDense : true in decoration
https://github.com/flutter/flutter/issues/27838
accepted aswer
Use -> contentPadding:
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.all(8.0),//here set your padding
https://github.com/flutter/flutter/issues/27838#issuecomment-507603179
or
new Container(
width:80.0
child:new TextField
)
Use can set maxLines of the TextField to set max height. Setting maxLines will still support multiline.
TextField(
maxLines: 5
)
You can use the following, which I am using in one my projects as well. Wrap your TextField in a container and change the size of the container itself. Then add a contentPadding to make the input smaller.
Container(
height 56, // Optional to decrease/increase
child: TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.all(8.0), // Increase to make smaller
prefixIcon: icon,
labelText: 'Label',
hintText: 'Hint',
border: null,
),
minLines: 1,
maxLines: null // Adds support for multiple lines
));
Replace your code with following to have at-least 3 lines in your TextField:
TextField(
minLines: 3,
controller: _textEditingController,
decoration: InputDecoration(
hintText: "留下你的评论吧~",
contentPadding: EdgeInsets.only(left: getMyWidth(10), right: getMyWidth(10)),
fillColor: default_bg,
filled: true,
border: OutlineInputBorder(borderSide: BorderSide.none, borderRadius: BorderRadius.all(Radius.circular(getMyWidth(5)))),
),
style: makeBlackStyle(),
keyboardType: TextInputType.multiline,
maxLines: null,
cursorColor: themeColor,
onChanged: (str) {
setState(() {
inputStr = str;
});
},
)
set in TextField
minLines: NUMBER, // default: 1
maxLines: NUMBER or null, // null: support for multiple lines
set in decoration
isDense : true // uses less vertical space, default: false
Hope to help you 😘💕
Related
I am developing this textfield and for some reason the text is having some space below it. Please let me know the solution to this.
This is my code for textfield.
Expanded(
child: Container(
height: 40,
child: TextField(
keyboardType: TextInputType.number,
controller: textEditingController,
style: TextStyle(
fontSize: 14, height: 0, color: Colors.white),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
decoration: InputDecoration(
// isDense: true,
// errorText: validateQtyTxt.toString(),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10)),
labelText: 'QTY > '
// hintText: 'Enter a search term',
),
onChanged: (String newValue) {
(text) => setState(() => _text);
print(newValue);
int qty;
derivativeScanController.buttonClick(newValue, qty);
},
),
),
),
This is the image for reference
enter image description here
There is a property called contentPadding which takes EdgeInsetsGeometry type value with that you can reduce the spacing between textfield and actual text
sample code .
contentPadding: EdgeInsets.zero,
if you want to remove all the spacing change the isDense property to true also
isDense: true,
all the space will be removed
You can use the contentPadding in the decoration property of the TextField to edit its padding.
https://api.flutter.dev/flutter/material/InputDecoration-class.html
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.only(...),
),
)
Which you can fill with whatever values you want. You can use things like EdgeInsets.zero or EdgeInsets.symmetric too:
https://api.flutter.dev/flutter/painting/EdgeInsets-class.html
Change the textStyle from 0 to 1 and adjust the text using padding.
When the TextDield receives focus the label receives a margin in relation to the prefixIcon.
Current
Objective:
Container(
padding: EdgeInsets.all(10),
child: TextFormField(
keyboardType: TextInputType.number,
textInputAction: TextInputAction.continueAction,
style: TextStyle(fontSize: 20),
decoration: InputDecoration(
alignLabelWithHint: true,
prefixIcon: Icon(Icons.device_thermostat),
labelText: "Temperature",
border: OutlineInputBorder(),
hintText: "Enter temperature",
),
),
)
Try replacing prefixIcon: Icon(Icons.device_thermostat), with prefix: Icon(Icons.device_thermostat),. Here is an example on DartPad with one field using prefixIcon and a second using prefix.
When I insert a multi-line TextField inside the ScrollBar, then add a lot of lines, then scroll it to the end, the rewind line on the right side does not reach the end. Why?
Widget _buildTextField() {
controller = TextEditingController(
text: text,
);
controller.addListener(_onTextChanged);
return Scrollbar(
child: TextField(
style: Styles.headlineRegular(color: ThemeColors.blackText),
controller: controller,
maxLines: widget.maxLines,
minLines: widget.minLines,
keyboardType: TextInputType.multiline,
decoration: InputDecoration(
fillColor: ThemeColors.veryLightGray,
filled: true,
hintStyle: Styles.headlineRegular(color: ThemeColors.darkGray),
hintText: widget.hintText,
enabledBorder: _buildBorder(ThemeColors.lightGray),
focusedBorder: _buildBorder(ThemeColors.darkGray),
),
),
);}
Add contentPadding: EdgeInsets.zero inside the IntputDecoration, it will remove that scrollbar padding.
Then you can wrap everything in a Padding Widget without messing the scrollbar.
Widget _buildTextField() {
controller = TextEditingController(
text: text,
);
controller.addListener(_onTextChanged);
return Scrollbar(
child: TextField(
style: Styles.headlineRegular(color: ThemeColors.blackText),
controller: controller,
maxLines: widget.maxLines,
minLines: widget.minLines,
keyboardType: TextInputType.multiline,
decoration: InputDecoration(
contentPadding: EdgeInsets.zero, // <----- ADD THIS LINE
fillColor: ThemeColors.veryLightGray,
filled: true,
hintStyle: Styles.headlineRegular(color: ThemeColors.darkGray),
hintText: widget.hintText,
enabledBorder: _buildBorder(ThemeColors.lightGray),
focusedBorder: _buildBorder(ThemeColors.darkGray),
),
),
);
}
Screenshot of the problem
I am trying to add an Email, which is a long text, but when the text field got full it cut all text in half.
here is my code:
Container(
height: 45,
decoration: BoxDecoration(
color: HexColor(MyColors.white),
),
child: TextFormField(
controller: _email,
maxLength: 100,
autofocus: true,
validator: validateEmail,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email',
counterText: "",
border: OutlineInputBorder(),
),
),
),
I get your point.. your problem there is text got high up.. you can set
Just adjust the contentPadding top bottom so it can be centered
decoration: InputDecoration(
contentPadding:
const EdgeInsets.only(
left: 8.0,
bottom: 8.0,
top: 8.0)),```
My problem get solved by adding those lines:
maxLines: 1,
decoration: InputDecoration(
isDense: true,
.
.
.)
I think your problem happens because you are using Container in the wrong way, so your Container is covering your TextFormField. So, in my opinion you can use Container, but with minimal attribute, or without attributes at all. But, I'm more prefer to not using Container.
And also, you can add the maxLines as null if you want it to be really long.
Your code should be like:
TextFormField(
maxLines: null,
controller: _email,
maxLength: 100,
autofocus: true,
validator: validateEmail,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email',
counterText: "",
border: OutlineInputBorder(),
),
style: TextStyle(
color: Colors.black,
),
),
Also, you can improve the design later by your self.
(NOTE: In my codes, I'm not using Container)
I created TextFields with a custom height and a different background color. Here is the code snippet:
Container(
width: width,
height: 35,
decoration: BoxDecoration(
color: Constants.greyColor,
borderRadius: BorderRadius.all(Radius.circular(2)),
),
padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
textAlign: descriptor == null ? TextAlign.center : TextAlign.left,
decoration: InputDecoration(
hintText: placeholder,
hintStyle: Constants.textStyleTextInputHint,
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 0),
),
onChanged: (value) {
state.didChange(value);
},
obscureText: isPassword,
style: Constants.textStyleTextInput,
cursorColor: Constants.primaryColor,
),
),
It worked fine until I recently updated Flutter to version 1.12.13+hotfix.5. Now the hint text as well as the input text are not centered anymore. It seems as if the Container does not change the height of the TextField anymore. Like so:
If I add a prefixIcon the text and the icon will get perfectly centered. Like so:
Does anybody know how I can center the text without an Icon?
Thank you!
I think it's because you add a contentPadding property, even tho it's only horizontal. Have you tried removing it?
TextField(
textAlign: TextAlign.center
decoration: InputDecoration(
hintText: ...,
hintStyle: ...,
border: ...,
// contentPadding:
)
)