How can I increase/decrease the number of maxLines
in TextField
with dragging and clicking button?
Flutter - Text widget with read more or expand feature. In the mobile applications, sometimes we have to create an item that shows only two or three lines of the complete paragraph. To create such kind of view, we have a to add a button that displays complete content. When the user clicks on that button.
Flutter TextField and Button Events using Controller - YOC. Manage textField value with controller with onpress ontap click event to change, send or set value of a text field to another with button in Flutter. Press "Enter" to skip to content.
See the example below to make a draggable and expandable floating action button in the Flutter app. First, add floatingpanel Futter package in your dependency by adding the following lines in pubspec.yaml file. This code will generate a movable Floating action button, when you click on button, it will expand with its menu buttons.
The read more or expandable feature in a Text and other widgets will help you to show large sentences or paragraphs. It shows complete paragraphs when a user clicks on read more button. We can display these feature at the end of sentences, as you can see below. The Flutter framework provides a Text widget to display content.
Screenshot:
Full code:
class YourPage extends StatefulWidget {
@override
_YourPageState createState() => _YourPageState();
}
class _YourPageState extends State<YourPage> {
double _maxHeight = 200, _minHeight = 44, _height = 44, _dividerHeight = 56, _offset = 19;
int _maxLines = 1;
static final Duration _fixDuration = Duration(milliseconds: 500);
Duration _duration = _fixDuration;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(20),
child: SizedBox(
height: _maxHeight,
child: Column(
children: <Widget>[
AnimatedContainer(
duration: _duration,
height: _height,
child: TextField(
decoration: InputDecoration(hintText: "Enter a message"),
maxLines: _maxLines,
),
),
Container(
height: _dividerHeight,
width: 200,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_downward),
onPressed: () {
if (_height <= _maxHeight - _offset - _dividerHeight) {
setState(() {
_duration = _fixDuration;
_height += _offset;
_maxLines++;
});
}
},
),
GestureDetector(
child: Icon(Icons.drag_handle),
onPanUpdate: (details) {
setState(() {
_height += details.delta.dy;
_duration = Duration.zero;
// prevent overflow if height is more/less than available space
var maxLimit = _maxHeight - _dividerHeight;
var minLimit = 44.0;
if (_height > maxLimit)
_height = maxLimit;
else if (_height < minLimit) _height = minLimit;
_maxLines = 100;
});
},
),
IconButton(
icon: Icon(Icons.arrow_upward),
onPressed: () {
if (_height >= _minHeight + _offset) {
setState(() {
_duration = _fixDuration;
_height -= _offset;
});
}
},
),
],
),
)
],
),
),
),
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With