'Automatically scroll multiline TextFormField when it extends the maxLines attribute
I'm implementing a TextFormField with the maxLines attribute set to 3. How can I make the TextFormField scroll down once the user starts with his fourth line? At the moment the cursor is just not visible anymore until the user scrolls down by hand. Is there a way to do this automatically?
This behaviour is actually featured in the flutter_gallery app in the 'Text fields' example. Just type a long text to the 'Live story' input until it reaches the fourth line.
The important parts of my code actually look like this:
import 'package:flutter/material.dart';
class TextFormFieldDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Form(
child: new TextFormField(
maxLines: 3,
),
),
);
}
}
So far I have found no workaround for this issue.
This issue affects both iOS and android.
Solution 1:[1]
Our team accomplished this by nesting some existing widgets:
// create the illusion of a beautifully scrolling text box
return new Container(
color: Colors.gray,
padding: new EdgeInsets.all(7.0),
child: new ConstrainedBox(
constraints: new BoxConstraints(
minWidth: _contextWidth(),
maxWidth: _contextWidth(),
minHeight: AppMeasurements.isLandscapePhone(context) ? 25.0 : 25.0,
maxHeight: 55.0,
),
child: new SingleChildScrollView(
scrollDirection: Axis.vertical,
reverse: true,
// here's the actual text box
child: new TextField(
keyboardType: TextInputType.multiline,
maxLines: null, //grow automatically
focusNode: mrFocus,
controller: _textController,
onSubmitted: currentIsComposing ? _handleSubmitted : null,
decoration: new InputDecoration.collapsed(
hintText: ''Please enter a lot of text',
),
),
// ends the actual text box
),
),
);
}
We had help from Darky to get widget ordering and the correct widgets to make it work.
Solution 2:[2]
You can use BoxConstraints and set maxHeight of your TextField
Container(
constraints: BoxConstraints(maxHeight: 100),
child: SingleChildScrollView(
child: TextField(
maxLines: null,
),
),
);
Solution 3:[3]
With the latest flutter 1.20.4, this text field will scroll when it rich the max hight.
Container(
constraints: BoxConstraints(maxHeight: 200),
child: TextField(
maxLines: null,
.......
)
)
If you are using the Textfield inside Raw or column wrap it in Expanded widget
Solution 4:[4]
I made it work like this. Hope it helps!
return new Card(
shape: RoundedRectangleBorder(
side: BorderSide(
color: Colors.deepPurpleAccent,
width: 1
),
borderRadius: BorderRadius.circular(3)
),
child: Container(
height: 50,
child: SingleChildScrollView(
child: TextField(
maxLines: null,
),
),
),
);
Solution 5:[5]
Just Use TextFormField() widget & set minLines= and maxLines= .
The missing thing is here Scroll indicator.
TextFormField(
minLines: 3,
maxLines: 3,
key: _messageValueKey,
decoration: _inputDecoration,
),
Solution 6:[6]
I solve this with ScrollController.
ScrollController textFieldScrollController = ScrollController();
TextField(
scrollController: textFieldScrollController,
keyboardType: TextInputType.multiline,
minLines: null,
maxLines: null,
onChanged: (value) {
textFieldScrollController.jumpTo(textFieldScrollController.position.maxScrollExtent);
},
),
Solution 7:[7]
Just add the 'reverse: true' to your Scrollable Widget, like so:
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
height: 130.0,
decoration: BoxDecoration(
border: Border.all(color: Colors.purpleAccent),
),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
reverse: true,
child: Text(
'$message',
maxLines: 2,
style: TextStyle(fontSize: 30),
),
),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Deborah |
| Solution 2 | Abdurahman Popal |
| Solution 3 | Sinshaw Demisse |
| Solution 4 | user187293 |
| Solution 5 | Yeasin Sheikh |
| Solution 6 | include life |
| Solution 7 | Chukwu3meka |
