Flutter resize TextField by dragging. Use the following example code to resize TextField by dragging.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 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; }); }, ), ) ], ), ); } } |
You can use like it:
1 2 3 4 5 6 | ExpandableTextField( child: TextField( decoration: InputDecoration(hintText: "Enter a message"), maxLines: 100, // give any number, but make sure it is not small ), ) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.