首页 > 解决方案 > 键盘粉碎文本字段

问题描述

我正在学习用 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,
            ),
          ),
        ),
      ],
    ));
  }
}

标签: flutterflutter-layout

解决方案


您有一个内部有几个 Expanded 的 Column ,注意空间均匀分布。当空间被键盘缩小时,它们会一起移动。基本上,由于键盘的原因,将输入字段放入 Scrollview 是一个好主意。您可以将 SingleChildScrollView 与您的 Column 一起使用,但您需要将 Expanded 小部件替换为 Container,或者只使用纯 TextFields。


推荐阅读