Flutter TextField ScrollBar bottom margin - flutter

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

Related

Get current height or line count of a TextField in Flutter

I would like to measure the height or the current line count of a TextField in Flutter. The height could be different depending on the device screen.
TextField(
keyboardType: TextInputType.multiline,
minLines: 5,
autofocus: true,
controller: storyController,
onChanged: (text) {
onStoryTextChanged(text);
},
cursorColor: Colors.black,
decoration: InputDecoration(
contentPadding: EdgeInsets.zero,
fillColor: Colors.transparent,
filled: true,
border: InputBorder.none,
),
style: textStyleNormalOnPaper,
),
You can check it from onchanged event of textfield
onChanged: (val){
print('\n'.allMatches(val).length);
},
Edit
TextSelection _selection = TextSelection(baseOffset: 0, extentOffset: text.length);
List<TextBox> lines = textPainter.getBoxesForSelection(_selection);
print(lines.length);

How to remove the margin added to the label in TextField that has prefixIcon when receiving focus?

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.

TextField's input text cutting in half after getting full?

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)

How can i change the focus to other textfield after i complete max characters in Flutter?

How do I change the focus to other node after completion max characters without pressing next/done in Flutter ?. How do I pass the control to other TextField using Focus node. I want to pass the control to next textfield after 4 characters of every textfield.
Widget serailKey1() {
return TextField(
controller: controller_key1,
focusNode: _serialKeyFN1,
textAlign: TextAlign.center,
keyboardType: TextInputType.text,
textCapitalization: TextCapitalization.characters,
style: textStyle,
maxLength: 4,
decoration: new InputDecoration(
counterText: "",
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(0.0),
),
),
),
onEditingComplete: ()=> FocusScope.of(context).requestFocus(_serialKeyFN2),
);
}
Widget serailKey2() {
return TextField(
controller: controller_key2,
focusNode: _serialKeyFN2,
textAlign: TextAlign.center,
keyboardType: TextInputType.text,
style: textStyle,
textCapitalization: TextCapitalization.characters,
maxLength: 4,
decoration: new InputDecoration(
counterText: "",
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(0.0),
),
),
),
onEditingComplete: ()=> FocusScope.of(context).requestFocus(_serialKeyFN3),
);
}
This works fine for me:
FocusNode fNode = FocusNode();
...
TextField(onChanged: (value){
if(value.length == 4)
FocusScope.of(context).requestFocus(fNode);
}),
TextField(focusNode: fNode),

set min height of textfiled in flutter

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 😘💕