Get current height or line count of a TextField in Flutter - 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);

Related

How to reduce Space between textfield and the input text in flutter

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.

Flutter Textfield size is too small while typing in small screens like watch displays

The size of the display area is too small compared to the free spaces around it. This happens in small screens displays like watches. How to increase the visibility of the sentence while typing?
Code sample
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
contentPadding:
EdgeInsets.symmetric(vertical: 8, horizontal: 8),
filled: true,
),
style: FontSizeModifier.body(context),
controller: textConroller,
keyboardType: TextInputType.multiline,
textInputAction: TextInputAction.go,
maxLines: null,
minLines: null,
expands: true,
autofocus: true,
textAlignVertical: TextAlignVertical.top,
enableSuggestions: true,
)

Flutter TextField ScrollBar bottom margin

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

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