flutter - Flutter 通过拖动调整 TextField 的大小
解决方案
截屏:
创建一个小部件。
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;
});
},
),
)
],
),
);
}
}
像这样使用它:
ExpandableTextField(
child: TextField(
decoration: InputDecoration(hintText: "Enter a message"),
maxLines: 100, // give any number, but make sure it is not small
),
)
推荐阅读
- javascript - 在 Angular 项目中使用 javascript 时出错
- facebook-graph-api - 如何将 facebook 用户 ID(来自 v1.x 的真实 ID)转换为范围用户 ID(v2.x)?
- java - 如果 Singleton 不是线程安全的,这到底意味着什么?
- python - Python:如何将所有记录器的日志记录级别设置为 INFO
- javascript - 如何使用 Javascript 中的 Flow 正确创建带有常量的类型?
- javascript - 如何在javascript中构建数组并在c#mvc控制器中接收它
- citrix - PlasticSCM,Citrix 设置:如何强制plasticscm 每次都请求登录到塑料服务器
- oracle - 为 Oracle 配置 SSL 连接
- pygame - 如何为 google colab 创建视频设备?
- javascript - 使每个“阅读更多”事件单击在 HTML 表格中不同时显示数据