Resize TextField based on content in Flutter - flutter

How a TextField width can be resized automatically based on content in Flutter?
An example with 3 TextField which autoresize depending on the content :

Only place the TextField inside an Expanded that will be in a row.
return Scaffold(
body: Container(
padding: EdgeInsets.only(top: 50),
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Expanded(
child: Padding(
child: new TextField(
onTap: () {//action of TextField
},
keyboardType: TextInputType.text,
decoration: InputDecoration(
border: InputBorder.none, hintText: 'Password'),
style: Theme.of(context).textTheme.body1,
),
padding: EdgeInsets.only(left: 40),
)),
IconButton(
icon: Icon(Icons.apps),
onPressed: () {//action of iconbutton
},
)
],
)
])),
);

Set the maxLines property of TextField to null. maxLines: null
TextField(
controller: new TextEditingController(
text: comment
),
maxLines: null,
style: TextStyle(
color: Colors.black,
fontSize: 12.0,
),
textAlign: TextAlign.left,
decoration: InputDecoration(
filled: true,
fillColor: HexColor('#ecedec'),
border: OutlineInputBorder(
borderSide:BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
),
)

I found the solution using the IntrinsicWidth class
IntrinsicWidth(
child: TextField(...
),

Related

How to make the text above the textformfield?

This is the design I want to make
This is my current design
I'm new to flutter. My question is how to make the text above the textformfield. I have searched on the internet and tried to do it but still no success. I don't know where else to go to solve my problem. I hope overflow is willing to help me.
This is my code:
Container(
width: double.infinity,
margin: EdgeInsets.fromLTRB(10, 0, 10, 10),
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
SizedBox(
height: 100,
width: 100,
child: Text('First Name'),
),
SizedBox(
width: 200,
child: TextFormField(
style: TextStyle(color: Colors.black),
controller: firstName,
onSaved: (String? value) {
firstName.text = value!;
},
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(
color: Colors.black, fontSize: 16),
),
),
),
],
)
],
),
)
Your problem occurs because of wrong widget placement. Notice that you've Row inside a Column, but you don't really use Column's children in order to vertically place 'First Name' text widget.Also the row is basically redundant currently. You can make a Row of Columns I just shared below in order to achieve your desired UI. Try to play with it :)
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
const SizedBox(
height: 20,
width: 100,
child: Text('First Name'),
),
SizedBox(
width: 150,
child: TextFormField(
style: const TextStyle(color: Colors.black),
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(color: Colors.black, fontSize: 16),
),
),
)
],
),
200px is too big and structure I will prefer
Row
-Expanded
- Column (CrossAxisAlignment.startMainAxisSize.min,)
- Text
- TextFormField
-Expanded
- Column (CrossAxisAlignment.startMainAxisSize.min,)
- Text
- TextFormField
-Expanded
- Column (CrossAxisAlignment.startMainAxisSize.min,)
- Text
- TextFormField
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.transparent,
width: 1.0,
),
),
child: Row(
children: [
filed(),
filed(),
filed(),
],
),
)
],
),
);
}
Expanded filed() {
return Expanded(
child: Padding(
padding: EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.only(bottom: 20),
child: Text('First Name'),
),
TextFormField(
style: TextStyle(color: Colors.black),
onSaved: (String? value) {},
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(color: Colors.black, fontSize: 16),
),
),
],
),
),
);
}
}
To create the TextField you want, you just need to use a Column like this:
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('First Name', style: TextStyle(fontWeight: FontWeight.bold),),
SizedBox(
width: 200,
child: TextFormField(
style: TextStyle(color: Colors.black),
onSaved: (String? value) {
// firstName.text = value!;
},
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'First Name',
hintStyle: TextStyle(color: Colors.black, fontSize: 16),
),
),
)
],
)
The result is:
In the input decoration of TextFormField, use label instead of hintText.
decoration: InputDecoration(
border: OutlineInputBorder(),
label: Text('First Name',
style: TextStyle(color: Colors.black, fontSize: 16),),
),
List item
You use the the property 'Labletext' of TextFormFiled or TextField....
to give the above text of the textformFiled.
just use column widget first children is Text and second children as TextFormField.

Facing assertion error when trying to add row inside a column

I am trying to make a login page which looks like this
But whenever I try to add row which has another column iside it for textinput field and text It shows assertion error. I also tried removing sized box before text row still it is showing the same error
My flutter code:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: color,
body: SafeArea(
child: Center(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 20.0,
),
Text('Life Drop',
style: TextStyle(fontSize: 40, color: Colors.white)),
SizedBox(
height: 10.0,
),
Text("your blood can save lives",
style: TextStyle(fontSize: 12, color: Colors.white)),
SizedBox(
height:20.0
),
Row(
children: [
Column(children: [
Text('Login'),
TextFormField(
controller: emailController,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Email',
),
),
SizedBox(width: 20),
],)
],
)
],
),
),
)
)
);
}
edit your row like below code:
Expanded(
child: Row(
children: [
Expanded(
child: Column(
children: [
const Text('Login'),
TextFormField(
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Email',
),
),
const SizedBox(width: 20),
],
)),
],
))

flutter layout rows to have columns inside

i have a layout which i am working on but i am very new to flutter what i would like to do is
have a layout as such:
but what i have managed to do so far is this
the code for what i have managed to do is here:
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.all(20.0),
color: Colors.cyan,
child: Row(
children: <Widget>[
Checkbox(
value: false,
onChanged: (bool? value) {
setState(() {
print(value!);
});
}, //onChanged
),
Text("Enable 1"),
new Flexible(
child: new TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'username',
),
), //textfield
), //flexible
new Flexible(
child: new TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'password',
),
), //textfield
), //flexible
],
),
) //Container
), //Expanded
Expanded(
flex: 1,
child: Container(
child: ElevatedButton.icon(
icon: Icon(
Icons.save_alt,
color: Colors.green,
size: 24.0,
),
label: Text('Save'),
onPressed: () {
print('pressed');
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.greenAccent),
),
),
),
)
]
)//end of body,
i have no idea on how to achieve my desired output any help would be appreciated
You should use a Column instead of the Row.
Then a Row for the text and the checkbox to be aligned.
Here's an example:
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.all(20.0),
color: Colors.cyan,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Checkbox(
value: false,
onChanged: (bool value) {
setState(() {
print(value);
});
}, //onChanged
),
Text("Enable 1"),
],
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: new TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'username',
),
),
), //flexible
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: new TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'password',
),
),
), //flexible
],
),
) //Container
), //Expanded
Expanded(
flex: 1,
child: Container(
child: ElevatedButton.icon(
icon: Icon(
Icons.save_alt,
color: Colors.green,
size: 24.0,
),
label: Text('Save'),
onPressed: () {
print('pressed');
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.greenAccent),
),
),
),
)
])

Flutter:My ScrollWidget is scrolling down but my SUBMIT and CANCEL Raised Buttons are getting invisible or not shown

Error showing is
Invalid Matrix
I want my submit button and Cancel button as this is Form submission in flutter
My ScrollWidget is scrolling down but my SUBMIT and CANCEL Raised Buttons are getting invisible or not shown.
the code is as follows:
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 20.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.person_pin),
labelText: 'CompanyName',
hintText: 'Type the company Name',
),
),
),
),
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.phone_android),
labelText: 'ModelName',
hintText: 'Type the Model Name',
),
),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
keyboardType: TextInputType.number,
inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly
],
decoration: InputDecoration(
labelText: 'SerielNumber',
hintText: 'Type the Seriel Number',
prefixIcon: Icon(Icons.view_headline),
),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
color: Color(0xff476cfb),
onPressed: () {
Navigator.of(context).pop();
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: TextField(
maxLines: null,
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.place),
labelText: 'Cancel',
),
),
),
RaisedButton(
color: Color(0xff476cfb),
onPressed: () {
uploadPic(context);
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: TextField(
maxLines: null,
autofocus: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.place),
labelText: 'Submit',
),
),
),
],
)
],
),
),
);
}
}
The problem is with your RaisedButton's Child. TextField must have bounded width.
you can use Text widget or Container widget as child of RaisedButton and use Row as child of Container to set title and icon like code below:
RaisedButton(
color: Color(0xff476cfb),
onPressed: () {
Navigator.of(context).pop();
},
elevation: 4.0,
splashColor: Colors.blueGrey,
child: Container(
child: Row(
children: [
Text('submit'),
Icon(Icons.place),
],
)),
),

How to expand TextField in Container vertically to cover all available space in Flutter

I want to expand a TextField to cover all the space vertically, its Container is expanding but TextField doesn't, here is design:
Blue is the Container area. but TextField is not expanding
This is the code I am using:
Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Title"),
Container(
margin: EdgeInsets.only(top: 8),
child: TextField(
controller: c_title,
decoration: Styles.getInputFieldStyle(""),
),
),
Container(
margin: EdgeInsets.only(top: 16),
child: Text("Feedback"),
),
Expanded(
child: Container(
color: Colors.blue,
margin: EdgeInsets.only(top: 8),
child: TextField(
decoration: Styles.getInputFieldStyle(""),
controller: c_feedback,
keyboardType: TextInputType.multiline,
),
),
),
Container(
margin: EdgeInsets.only(top: 16),
width: double.infinity,
child: RaisedButton(
onPressed: (){_onSubmitPressed();},
child: Text("Submit"),
textColor: Colors.white,
color: MyColors.theme_red,
),
)
],
),
);
With Flutter 1.5.4 you can do simply the following:
Expanded(
child: TextField(
expands: true,
maxLines: null,
),
)
It will occupy all vertical free space.
You need this.
TextFormField(
decoration: InputDecoration(hintText: "Enter your very long text here"),
maxLines: double.maxFinite.floor(),
),
Edit: This is the final solution for you.
Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Title"),
Container(
margin: EdgeInsets.only(top: 8),
child: TextField(
controller: c_title,
decoration: Styles.getInputFieldStyle(""),
),
),
Container(
margin: EdgeInsets.only(top: 16),
child: Text("Feedback"),
),
Expanded(
child: LayoutBuilder(
builder: (_, __) {
return Container(
color: Colors.blue,
margin: EdgeInsets.only(top: 8),
child: TextField(
decoration: Styles.getInputFieldStyle(""),
controller: c_feedback,
maxLines: double.maxFinite.floor(),
keyboardType: TextInputType.multiline,
),
);
},
),
),
Container(
margin: EdgeInsets.only(top: 16),
width: double.infinity,
child: RaisedButton(
onPressed: () {
_onSubmitPressed();
},
child: Text("Submit"),
textColor: Colors.white,
color: MyColors.theme_red,
),
)
],
),
),
Below code can help you -
Use "maxLines" and "keyboardType: TextInputType.multiline" to expand text fields
new Container(
child: TextFormField(
textAlign: TextAlign.start,
style: new TextStyle(
fontSize: 14.0, color: Colors.black),
keyboardType: TextInputType.multiline,
focusNode: nodeTwo,
textInputAction: TextInputAction.next,
maxLines: 4,
maxLength: 200,
decoration: InputDecoration(
labelText: 'Add Description',
alignLabelWithHint: true ,
hintText: 'Enter Description',
hintStyle: TextStyle(fontWeight:
FontWeight.w300,fontSize: 14.0),
errorStyle: TextStyle(color: Colors.redAccent),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0))),
)
Just set maxLines to null:
TextField(
maxLines: null,
),
If your textField widget is Column's children widget and set expands: true .
Can set textAlignVertical: TextAlignVertical.top to vertical align text in the top.