Flutter textfield expand as user types - flutter

I have a textfield and need to have multi-line as below so that the text wraps, however it takes up the space of all the lines that is defined in maxLines. I want to be able to have it look like 1 line and expand as the text wraps. Any ideas of how to do this is flutter?
new TextField(
decoration: const InputDecoration(
hintText: 'Reply',
labelText: 'Reply:',
),
autofocus: false,
focusNode: _focusnode,
maxLines: 1,
controller: _newreplycontroller,
keyboardType: TextInputType.text,
),

Set maxLines to null and it will auto grow vertically.
It's documented (but not entirely clearly) here (edit: I've created a PR to update this, which I should've done to begin with for this question ;). To figure it out I ended up looking at the code for TextField and its superclass(es), seeing what the behavior would be if set to null.
So your code should be as follows:
new TextField(
decoration: const InputDecoration(
hintText: 'Reply',
labelText: 'Reply:',
),
autofocus: false,
focusNode: _focusnode,
maxLines: null,
controller: _newreplycontroller,
keyboardType: TextInputType.text,
),
If you're trying to make it autogrow horizontally, you probably shouldn't - at least not based on the text content. But I'm asuming you want to make it autogrow vertically.

Using this should do the trick:
keyboardType: TextInputType.multiline,
minLines: 1,
maxLines: 20,
maxLength: 1000,
Adding the minLines and MaxLines is important in order to get the required behaviour, whereas maxLength is a treat!
Maybe too late, but better late than never!

As shown in the first answer, setting maxLines to null will do the trick. However this would allow the textfield to grow infinitely.
To have more control, set minLines to 1 and maxLines as needed.
Hope this helps!
Code Sample:
TextField(minLines:1,maxLines:8)

The accepted answer works great for growing vertically. If you'd like the TextField to start small and expand horizontally as the user types, you can do the following:
IntrinsicWidth(
child: TextField(...
),
In my case I wanted it centered on the screen (horizontally), so I did:
Center(
child: IntrinsicWidth(
child: TextField()
)
)

Setting to null was working the same as setting to 1 (it grows horizontally just one line).
I think you have to set minLines and maxLines.
I'm using minLines: 1 and maxLines: 2 and when the first line reaches the end it expands another line. When the second line reaches the end it scroll the second to the first and creates the third line. So you will have to set maxLenght too.

If you want to set a predefined height, use expands: true:
SizedBox(
height: 200,
child: TextField(
decoration: InputDecoration(hintText: 'Enter a message'),
maxLines: null,
expands: true,
),
)

You can simply use this;
TextField get _text => TextField(
maxLines: null,
decoration: InputDecoration(
constraints: BoxConstraints(
maxHeight: responsiveHeight,
maxWidth: responsiveWidth),
contentPadding: EdgeInsets.symmetric(
horizontal: responsiveHorizontalPadding,
vertical: responsiveVerticalPadding),
disabledBorder: InputBorder.none,
border: InputBorder.none,
enabledBorder: InputBorder.none,
isDense: false,
));

#Rabi Roshan,
As shown in the first answer, setting maxLines to null will do the
trick. However this would allow the textfield to grow infinitely.
To have more control, set minLines to 1 and maxLines as needed. Hope
this helps!
Thank you so much!
I also prefer this version much more because it will only expand if the user inputs more than one line. And if it exceeds the maxLines, it will scroll vertically which is what should normally happen and what all big Chat Apps use.

textfield and need to have multi-line as below so that the text wraps, however it takes up the space of all the lines that is defined in maxLines. I want to be able to have it look like 1 line and expand as the text wraps
Container(
child: new TextField (
keyboardType: TextInputType.multiline,
minLines: 1,
maxLines: 10,
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
filled: true,
hintStyle: new TextStyle(color: Colors.grey[800]),
hintText: "Type in your text",
fillColor: Colors.white70),
),
padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0),
);

Related

How to use a responsive text form field widget in flutter?

I'm using "maxLine" as a length for the text form field but it does not fit in different size of phones, what should I put in order to make the length responsive.
The box border should fit on the size of the phone. https://i.stack.imgur.com/Wisa6.png. Here's my code, I'm using onTap in gesture detector to call that widget
Widget buildDescription() => TextFormField(
maxLines: MediaQuery.of(context).size.height.toInt(),
initialValue: widget.description,
style: TextStyle(color: Colors.black, fontSize: 18),
decoration: InputDecoration(
hintText: 'Tell us what you feel',
hintStyle: TextStyle(color: Colors.black54),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
borderSide: BorderSide(
color: Colors.grey.shade500,
width: 2,
style: BorderStyle.solid))),
validator: (description) => description != null && description.isEmpty
? "I'm here to listen. Don't hesitate to open up when you're ready"
: null,
onChanged: widget.onChangedDescription,
);
}
The following should make the TextFormField fill the screen and also make the number of lines unlimited.
Expanded(
child: TextFormField(
maxLines: null,
keyboardType: TextInputType.multiline,
expands: true,
),
)
You can put your TextFormField inside an Expanded widget, which will stretch it over the remaining screen space.
// expanded should be used inside rows or columns
return Column(
children: [
DateMarker(...),
HowDidIFailField(...),
// the expanded stretches the view over the remaining space
Expanded(
child: TextFormField(),
),
]
);
A thing to note is that when the keyboard appears, less space is available and the widget will shrink further. Make sure to test with that as well.

WillPopScope does not work from another widget

I have multiple widgets in my flutter app. If I click at another widget, onWillPopScope method which is defined at the top of Widget build(BuildContext context) does not working. Could you give me advice how to fix it? Thanks
See below code:
ExpansionTile(
initiallyExpanded: false,
title:
Text('Solution', style: basicAlertBlack),
children: <Widget>[
Container(
color: Colors.amber.shade50,
height:
MediaQuery
.of(context)
.size
.height /
7,
child: _solutionTextField(),
)
],
),
Code of second widget:
Widget _solutionTextField() {
return TextField(
focusNode: myFocusNode,
keyboardType: TextInputType.multiline,
maxLines: null,
style: solution,
controller: solutionController,
decoration: new InputDecoration(
contentPadding: EdgeInsets.all(8),
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
hintText: 'Enter a solution'));
}
Can you provide more context, please?
As documentations says:
WillPopScope class
Registers a callback to veto attempts by the user to dismiss the enclosing ModalRoute.
It means that it will be called on pop actions. It could be called from android navigation by clicking the return arrow or iOS by swiping from edge left side to right. And every other pop method that you call via Navigator. So it doesn't have a connection with clicking at Widget.

How do I layer the text box hint in a Flutter text book like in the mock below?

I'm working on my onboarding screens for a Flutter mobile app I'm developing, and I want to make my textboxes look like the mock here below:
The desired goal
I have already created most of the text box, but I can't figure out how to make the hint move to the top of the textbox whenever there is text entered in the box. So far I have the below representation does anyone have a clue on how I could do that?
My current implementation
You can do it by
Container(
margin: EdgeInsets.all(20),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Full Name',
),
),
),
Samples:
If I understand your question correctly. What you want is called labelText with floatingLabelBehavior: FloatingLabelBehavior.auto
Example
TextField(
decoration: InputDecoration(
labelText: "Username *",
floatingLabelBehavior: FloatingLabelBehavior.auto,
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8.0))
),
)
)

How to create a custom height TextFormField in flutter?

I have a form where there are two TextFormField and one bottom button to validate it but the problem is that the screen is looking awkward. I want the **TextFormField ** the bottom one two cove the area between them completely even if there is no content and become scrollable once it reaches the bottom.
the left one is what I have, but I want the right one, I tried to wrap it with Expandable or Container but it didn't work because I was using border property.
You can achieve this by wrapping the TexFormField widget with an Expanded widget and setting the expands and maxLines property of the TextFormField to true and null respectively.
The TextFormField with take all the available space irrespective of the screen size, check the code provided below for an example:
// wrap it with an Expanded widget
Expanded(
child: TextFormField(
// set the keyboard type to multiline f
keyboardType: TextInputType.multiline,
// set maxlines to null
maxLines: null,
// set expands to true
expands: true,
decoration: InputDecoration(
isDense: true,
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.black)
)
),
),
),
You just have to increase the maxLines inside the TextFormField to increase it's size like this:
Container(
height: MediaQuery.of(context).size.height*0.5,//set the size according to your choice
decoration: BoxDecoration(
//Customize your container
color: Colors.red,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
child: TextFormField(
maxLines: 30,//add any number of lines
//Customize your text field
)
),

TextField with animated hint / label

I want to implement a form containing TextFields. Each field has a label / hint. I want the hint to animate and become a label when the user starts typing. This is a standard Material design pattern, so I expected it to be implemented by the standard Widgets.
Something like this:
It turns out to be very simple.
InputDecoration has a labelText parameter, which does what I wanted.
E.g.
TextField(decoration: InputDecoration(labelText: 'Full name')),
In Flutter, both hint and label are behaving in two different way that hintText will be shown as fixed but the labelText will be(double acting) shown as hint which is animating to the top when the cursor is getting focused.
TextField(decoration: InputDecoration
(
labelText: "Animatable hint",
hintText: "Inanimate hint"
)
)
Difference between labelText and HintText.
labelText : Shows label top of the input field, if it's empty or unfocused. When it get focus, labelText moves to above the input field.
hintText: just shows hint to the user.
TextField(decoration: InputDecoration(labelText: 'labelText'),),
TextField(decoration: InputDecoration(hintText: 'hintText'),),
TextField(decoration:InputDecoration(hintText: 'both', labelText: 'both'),),
more information - TextFormField placeholder
Also it's a good way to make your own Method or widget.(So you can reuse code later)
Example:
//your generator method or you can make your own widget class if you want that.
Widget _entryField(String title, {bool isPassword = false}) {
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
title,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
),
SizedBox(
height: 10,
),
TextField(
obscureText: isPassword,
decoration: InputDecoration(
//labelText: title , // you can change this with the top text like you want
hintText: "Please enter your $title" ,
border: InputBorder.none,
fillColor: Color(0xfff3f3f4),
filled: true))
],
),
);
}
==============
Edit:
As mentioned by #Evin1_ below.
After reading this article Splitting widgets to methods is a performance antipattern/Iiro Krankka
I found it's better to use StatelessWidget to split your code and functions only for doing Operations.
the reason:
This way, you won’t be rebuilding your static widget trees multiple times for nothing but wasted CPU cycles.
If you really prefer building your widget trees with methods, you might want to take a look at a package called functional_widget by Remi Rousselet.
Also others comments for more information about this topic here difference between functions and classes to create reusable widgets