Is there any way to create something like these dots, which can help in expanding TextField
.
Asked
Active
Viewed 2,268 times
1 Answers
18
Screenshot:
Create a widget.
class ExpandableTextField extends StatefulWidget {
final double height;
final double maxHeight;
final double dividerHeight;
final double dividerSpace;
final Widget child;
const ExpandableTextField({
Key key,
@required this.child,
this.height = 44,
this.maxHeight = 300,
this.dividerHeight = 40,
this.dividerSpace = 2,
}) : super(key: key);
@override
_ExpandableTextFieldState createState() => _ExpandableTextFieldState();
}
class _ExpandableTextFieldState extends State<ExpandableTextField> {
double _height, _maxHeight, _dividerHeight, _dividerSpace;
@override
void initState() {
super.initState();
_height = widget.height;
_maxHeight = widget.maxHeight;
_dividerHeight = widget.dividerHeight;
_dividerSpace = widget.dividerSpace;
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: _maxHeight,
child: Column(
children: <Widget>[
SizedBox(
height: _height,
child: widget.child,
),
SizedBox(height: _dividerSpace),
Container(
height: _dividerHeight,
width: 60,
child: GestureDetector(
child: FittedBox(child: Text("----")),
onPanUpdate: (details) {
setState(() {
_height += details.delta.dy;
// prevent overflow if height is more/less than available space
var maxLimit = _maxHeight - _dividerHeight - _dividerSpace;
var minLimit = 44.0;
if (_height > maxLimit)
_height = maxLimit;
else if (_height < minLimit)
_height = minLimit;
});
},
),
)
],
),
);
}
}
Use it like:
ExpandableTextField(
child: TextField(
decoration: InputDecoration(hintText: "Enter a message"),
maxLines: 100, // give any number, but make sure it is not small
),
)

CopsOnRoad
- 237,138
- 77
- 654
- 440
-
3you save my life, not time :D i learn flutter with your answered to my questions more than the other documents – DolDurma Sep 09 '19 at 05:52
-
this widget can be have `minLines`? – DolDurma Sep 09 '19 at 05:53
-
@DolDurma Thank you so much for your kind words, it really means a lot !!! – CopsOnRoad Sep 09 '19 at 05:59
-
You don't really need `minLines`, by default it is going to be `1`. And if you really need that, I can update the answer. – CopsOnRoad Sep 09 '19 at 06:00
-
You could use `_height = _height.clamp(_minLimit, _maxLimit)` at the end. I think it would be clearer. https://api.flutter.dev/flutter/dart-core/num/clamp.html – Mateus Felipe Jun 18 '20 at 23:31