How do I stop my text field from growing horizontally? - flutter

I have a text field in a modal sheet that is wrapped with a column. When I type text in the text field it grows horizontally instead of vertically, I've tried setting maxLines to null but it still behaves the same way. I want it to expand vertically.
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (BuildContext context) {
return Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context)
.viewInsets
.bottom), //keeps above keyboard
child: SizedBox(
height: 150,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
createSpace(1),
const TextField(
keyboardType: TextInputType.multiline,
autofocus: true,
maxLines: null,
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 10),
border: InputBorder.none,
hintText: "Title",
),
),
const TextField(
style: TextStyle(height: 1),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 20),
hintText: "Description",
),
),
TextButton(onPressed: () {}, child: const Text("Save"))
],
),
),
);
});

Just add below code in TextField.
minLines: 1,
maxLines: null,

You need to set Size box width also. so that the line takes a constant width then goes to next line. you can also fix minLines and maxLines in TextField that can help to take max line
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (BuildContext context) {
return Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context)
.viewInsets
.bottom), //keeps above keyboard
child: SizedBox(
height: 150,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
createSpace(1),
const TextField(
keyboardType: TextInputType.multiline,
autofocus: true,
minLines: 1,
maxLines: 3,
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 10),
border: InputBorder.none,
hintText: "Title",
),
),
const TextField(
style: TextStyle(height: 1),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 20),
hintText: "Description",
),
),
TextButton(onPressed: () {}, child: const Text("Save"))
],
),
),
);
});

Related

How to stack cursor textfield over keyboard on Flutter?

How to solve this error?, i want to stack cursor tetxfield over keyboard.
Actually result
I want the result like this
I tried used stack widget but it's not work.
My source Code
Stack(
children: [
const Positioned.fill(
child: Image(
image: AssetImage('assets/images/background.png'),
repeat: ImageRepeat.repeat,
),
),
ListView.separated(
itemBuilder: (_, index) => const SizedBox(),
separatorBuilder: (_, __) => const SizedBox(
height: 8,
),
itemCount: 5,
reverse: true,
),
Positioned(
bottom: MediaQuery.of(context).viewInsets.bottom,
left: 0,
right: 0,
child: Container(
decoration: BoxDecoration(
color: context.theme.colorScheme.surface,
),
child: Row(
children: [
IconButton(
onPressed: () {},
icon: const Icon(
Icons.attachment_rounded,
color: kIconKeyboardChatColor,
),
),
const Expanded(
child: TextField(
decoration: InputDecoration(
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
hintText: 'Write a message...',
),
keyboardType: TextInputType.multiline,
textInputAction: TextInputAction.newline,
),
),
],
),
),
),
],
)
is other solution? I really appreciate your answer.
Try to add maxLines 6 and minLines 1.
TextField(
decoration: InputDecoration(
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
hintText: 'Write a message...',
textCapitalization: TextCapitalization.sentences,
maxLines: 6,
minLines: 1,
)

How to show user data in Text or TextField flutter

I want to show the data that the user has written into the TextField in the Container as a Text. While receiving data, I gave the Container a constant height and set maxLines: 10000 for the TextField, so as the user writes in without overflowing outside the Container, the text moves up inside the Container. Is there a structure where I can show output user data in this way? When i try to show output in same way, it doesn't show overflowed text, how i make it scrollable?
Here is user data receiving code:
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 4,
),
child: Container(
height: MediaQuery.of(context).size.height * 7 / 10,
decoration: BoxDecoration(
border: Border.all(
color: UserColorHelper.Blue,
width: 1,
),
borderRadius: BorderRadius.circular(4),
),
child: Padding(
padding: const EdgeInsets.all(2.0),
child: TextField(
decoration: InputDecoration.collapsed(
hintText: "Note here..."),
style: TextStyle(color: UserColorHelper.Light),
keyboardType: TextInputType.text,
maxLines: 10000,
textInputAction: TextInputAction.done,
autofocus: false,
onEditingComplete: () {},
),
),
),
),
Text_Input
use TextEditingController
add
controller: reasonController,
fix your code to :
final reasonController = TextEditingController();
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 4,
),
child: Container(
height: MediaQuery.of(context).size.height * 7 / 10,
decoration: BoxDecoration(
border: Border.all(
color: UserColorHelper.Blue,
width: 1,
),
borderRadius: BorderRadius.circular(4),
),
child: Padding(
padding: const EdgeInsets.all(2.0),
child: TextField(
controller: reasonController,
decoration: InputDecoration.collapsed(
hintText: "Note here..."),
style: TextStyle(color: UserColorHelper.Light),
keyboardType: TextInputType.text,
maxLines: 10000,
textInputAction: TextInputAction.done,
autofocus: false,
onEditingComplete: () {
print(reasonController.text);
},
),
),
),
),
get text from Controller
print(reasonController.text)
You can use overflow property of textfield(), do something like this,
overflow: TextOverflow.ellipsis,
OR
overflow: TextOverflow.fade,

Flutter: Column Row [Beginner]

I am new into Flutter and at the moement I try to code my first App.
I just want to implement a screen with Text Fields - that worked.
Now I want to arrange the first two text field next to each other. I tried to use Row () and I got no errors, but suddenly the emulator shows me just a blank page.
Could someone tell me what I have to change? I don't know what I did wrong, because I do not get any errors.
Thanks for help!
import 'package:flutter/material.dart';
class TestEdit extends StatefulWidget {
#override
_TestEditState createState() => _TestEditState();
}
class _TestEditState extends State<TestEdit> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
//`true` if you want Flutter to automatically add Back Button when needed,
//or `false` if you want to force your own back button every where
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.pop(context, false),
),
),
body: Column(
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
//mainAxisSize: MainAxisSize.min,
//padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
children: <Widget>[
//Row(children:[
//crossAxisAlignment: CrossAxisAlignment.start,
//mainAxisSize: MainAxisSize.min,
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'InputFirst',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'InputSecond',
),
),
),
],
),
//],
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
],
),
);
}
}
TextField widget will try to horizontally expand as much as possible.
So if you want to put an TextField in a Row, you should wrap it with Flexible(or Expanded) widget.
Try this,
class TestEdit extends StatefulWidget {
#override
_TestEditState createState() => _TestEditState();
}
class _TestEditState extends State<TestEdit> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
//`true` if you want Flutter to automatically add Back Button when needed,
//or `false` if you want to force your own back button every where
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.pop(context, false),
),
),
body: Column(
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
//mainAxisSize: MainAxisSize.min,
//padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
children: <Widget>[
//Row(children:[
//crossAxisAlignment: CrossAxisAlignment.start,
//mainAxisSize: MainAxisSize.min,
Expanded( //TODO: Wrap with `Expanded`
child: Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'InputFirst',
),
),
),
),
Expanded( //TODO: Wrap with Expanded
child: Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'InputSecond',
),
),
),
),
],
),
//],
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(10, 10, 10, 0),
child: TextField(
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Input',
),
),
),
],
),
);
}
}
When ever you need add widgets vertically you need to add column and in horizontal way you need to add row. Both widgets contain children.
import 'package:flutter/material.dart';
class Beginner extends StatefulWidget {
#override
_BeginnerState createState() => _BeginnerState();
}
class _BeginnerState extends State<Beginner> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Row(
children: [
Text('A'),
Text('B'),
Text('C'),
],
),
Row(
children: [
Text('A'),
Text('B'),
Text('C'),
],
),
Row(
children: [
Text('A'),
Text('B'),
Text('C'),
],
),
Row(
children: [
Text('A'),
Text('B'),
Text('C'),
],
),
],
),
);
}
}

Flutter TextField with maxLength and

I need a Text Area with an enforced maxLength of 250 chars and its label aligned with the hint text. My code is below.
However, the widget is rendering the text on top of the hint. Does anyone know a solution?
It's like if the counter widget forces the actual text field to move upwards.
TextField with maxLength = 250 and alignLabelWithHint = true
TextField(
expands: false,
minLines: null,
maxLines: null,
maxLengthEnforced: maxLengthEnforced,
maxLength: maxLength,
controller: controller,
onChanged: (String value){
print(value);
},
cursorColor: Colors.deepOrange,
keyboardType: keyboardType,
decoration: InputDecoration(
alignLabelWithHint: true,
labelText: text,
labelStyle: TextStyle(color: Colors.grey),
hintText: text,
prefixIcon: Container(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 15.0),
child: Material(
elevation: 0,
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(30)),
child: Icon(
icon,
color: Colors.red,
),
),
),
],
),
),
border: InputBorder.none,
contentPadding: EdgeInsets.only(right: 10, top: 5, bottom: 5)),
)
The purpose of a hint is to disappear when the user start typing I tried your code and it worked with no problem.
TextField(
expands: false,
minLines: null,
maxLines: null,
maxLengthEnforced: true,
maxLength: 250,
// controller: controller,
onChanged: (String value) {
print(value);
},
cursorColor: Colors.deepOrange,
// keyboardType: keyboardType,
decoration: InputDecoration(
alignLabelWithHint: true,
labelText: "hi",
labelStyle: TextStyle(color: Colors.grey),
hintText: "hi",
prefixIcon: Container(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 15.0),
child: Material(
elevation: 0,
color: Colors.white,
borderRadius:
BorderRadius.all(Radius.circular(30)),
child: Icon(
Icons.highlight,
color: Colors.red,
),
),
),
],
),
),
border: InputBorder.none,
contentPadding:
EdgeInsets.only(right: 10, top: 5, bottom: 5)),
),

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.