How to show user data in Text or TextField flutter - 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,

Related

How do I stop my text field from growing horizontally?

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

Dynamically change container size depending on textfield value

In the todoScreen, there is a container containing a column with two text fields, I want the container to change dynamically as the number of characters in the second text field increases, but the container just expands to the bottom. How do I solve this?
Scaffold(
body: SafeArea(
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 20.0),
child: Container(
decoration: BoxDecoration(
color: Color(0xFF414141),
boxShadow: const [
BoxShadow(
color: Color(0xFF414141),
offset: Offset(2.5, 2.5),
blurRadius: 5.0,
spreadRadius: 1.0,
), //B
],
borderRadius: BorderRadius.circular(14.0)),
padding:
const EdgeInsets.symmetric(horizontal: 24.0, vertical: 15.0),
child: Column(
children: [
TextField(
controller: titleEditingController,
autofocus: true,
autocorrect: false,
cursorColor: Colors.grey,
maxLines: 1,
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
hintText: "Title", border: InputBorder.none),
style: GoogleFonts.notoSans(
color: Color(0xFFA8A8A8), fontSize: 20.0),
),
const Divider(
color: Color(0xFF707070),
),
TextField(
controller: detailEditingController,
maxLines: null,
autocorrect: false,
cursorColor: Colors.grey,
textInputAction: TextInputAction.done,
decoration: const InputDecoration(
hintText: "Notes", border: InputBorder.none),
style: GoogleFonts.notoSans(
color: Color(0xFFA8A8A8), fontSize: 20.0),
),
],
)),
),
),
);
-> Use This Code You're Issue Solved
Ex:
body: SafeArea(
child: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 20.0),
child: Container(
decoration: BoxDecoration(
color: Color(0xFF414141),
boxShadow: const [
BoxShadow(
color: Color(0xFF414141),
offset: Offset(2.5, 2.5),
blurRadius: 5.0,
spreadRadius: 1.0,
), //B
],
borderRadius: BorderRadius.circular(14.0)),
padding:
const EdgeInsets.symmetric(horizontal: 24.0, vertical: 15.0),
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
TextField(
controller: titleEditingController,
autofocus: true,
autocorrect: false,
cursorColor: Colors.grey,
maxLines: 1,
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
hintText: "Title", border: InputBorder.none),
style: TextStyle(
color: Color(0xFFA8A8A8), fontSize: 20.0),
),
const Divider(
color: Color(0xFF707070),
),
TextField(
controller: detailEditingController,
maxLines: null,
autocorrect: false,
cursorColor: Colors.grey,
textInputAction: TextInputAction.done,
decoration: const InputDecoration(
hintText: "Notes", border: InputBorder.none),
style:TextStyle(
color: Color(0xFFA8A8A8), fontSize: 20.0),
),
],
)),
),
),
),

Add Scrollbar with TextFormField Flutter

I want to add Scrollbar with TextFormField but when I scroll down I get an error. My Code
Scrollbar(
child: TextFormField(
maxLines: 4,
minLines: 1,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(0.0),
border: InputBorder.none,
isDense: true // text form first
),
),
)
enter image description here
This will not add any Scrollbar but it will be Scrollable.
Container(
height: 60,
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.only(left: 15,right: 15,top: 10),
child: ListView(
children: <Widget>[
TextField(
minLines: 1,
maxLines: 5,
decoration: InputDecoration(
focusedBorder:OutlineInputBorder(
borderSide: BorderSide(color: Colors.black54,)
),
border: InputBorder.none,
isDense: true
),
)
],
),
),

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.