flutter - 键盘粉碎文本字段
问题描述
我正在学习用 Flutter 编写代码并重做我的一个 Android/iOS 应用程序。到目前为止,这大致是 UI 的样子:
当我点击一个字段以打开键盘时,它会将所有字段粉碎在一起。
我正在尝试确定处理此问题的最佳方法是什么。我能想到的唯一解决方法是在键盘打开时以某种方式没有所有字段和按钮。但是,如果我这样做,那么在将数据输入其中时,屏幕下方的字段将不可见。有没有好的方法来处理这个?
这是此视图的代码:
class _CarbCalcState extends State<CarbCalc> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Carb Calculator'),
backgroundColor: Colors.purple.shade700,
leading: IconButton(
icon: Icon(Icons.settings),
onPressed: () {},
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.library_books),
onPressed: () {},
)
],
),
body: Container(
margin: EdgeInsets.only(top: 40.0, left: 20.0, right: 20.0),
child: Column(
children: <Widget>[
NutrientValue(
nutrientName: 'Protein',
onChanged: (value) {
print('Protein Changed');
},
),
NutrientValue(
nutrientName: 'Fat',
onChanged: (value) {},
),
NutrientValue(
nutrientName: 'Fiber',
onChanged: (value) {},
),
NutrientValue(
nutrientName: 'Moisture',
onChanged: (value) {},
),
NutrientValue(
nutrientName: 'Ash',
onChanged: (value) {},
),
SizedBox(
height: 30.0,
),
ButtonTheme(
minWidth: double.infinity,
height: 50,
child: FlatButton(
onPressed: () {},
color: Colors.blue.shade700,
child: Text(
'Calculate Carbs',
style: TextStyle(
fontSize: 20,
),
),
),
),
],
),
),
);
}
}
class NutrientValue extends StatelessWidget {
NutrientValue({@required this.nutrientName, @required this.onChanged});
final String nutrientName;
final Function onChanged;
@override
Widget build(BuildContext context) {
return Expanded(
child: Row(
children: <Widget>[
Container(
width: 90,
child: Text(
nutrientName,
style: TextStyle(fontSize: 18.0),
),
),
Expanded(
child: TextField(
keyboardType: TextInputType.numberWithOptions(
signed: false,
decimal: true,
),
style: TextStyle(
color: Colors.black,
fontSize: 18.0,
),
onChanged: this.onChanged,
decoration: InputDecoration(
hintText: 'Enter $nutrientName',
hintStyle: TextStyle(
color: Colors.grey,
),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(15.0),
),
borderSide: BorderSide.none,
),
filled: true,
fillColor: Colors.white,
),
),
),
],
));
}
}
解决方案
您有一个内部有几个 Expanded 的 Column ,注意空间均匀分布。当空间被键盘缩小时,它们会一起移动。基本上,由于键盘的原因,将输入字段放入 Scrollview 是一个好主意。您可以将 SingleChildScrollView 与您的 Column 一起使用,但您需要将 Expanded 小部件替换为 Container,或者只使用纯 TextFields。
推荐阅读
- javascript - imgur api图片上传跨域资源共享错误
- python - 如何编写 Python 类来解决给定方程和其他变量值的任何缺失变量?
- coinbase-api - Coinbase 地址 API 端点返回大部分是空数据
- html5-canvas - 如何使用 KONVA 根据每个像素值(R、G、B)设置自定义(某些计算逻辑)颜色
- asp.net-mvc - 在新数据上刷新 Razor 页面
- azure - 无法通过 ARM 模板注册命名空间“Microsoft.AlertsManagement”
- kubernetes - AKS:对所有 Web 请求的长时间延迟(等待 TTFB 具有很大的价值)
- python - 如何在带有“and”和“or”的if语句中使用括号来使用“or”分隔两个选项。请参阅代码以供参考
- c# - 在 ASP.NET 5 WebAPI 中使用 OData 时如何获取分页信息和单个记录
- ajax - 如何在 ajax-cart-template.liquid 中获取产品变体及其标签的库存?