显示文本字段对话框而不被键盘覆盖?

时间:2017-10-20 02:41:00

标签: dart flutter

我正在尝试创建允许用户输入其姓名的SimpleDialog。但是当它显示时,对话框被屏幕键盘隐藏了一半:

Screenshot

如何让Dialog完全可见?

编辑:我觉得奇怪的是,主页小部件(FocusVisibilityDemo)识别出降低的高度,因此调整了“推送我”的位置。按钮保持在中心。不幸的是,对话框的行为方式并不相同。

这是我的代码:

import 'package:flutter/material.dart';

class FocusVisibilityDemo extends StatefulWidget {
  @override
  _FocusVisibilityDemoState createState() => new _FocusVisibilityDemoState();
}

class _FocusVisibilityDemoState extends State<FocusVisibilityDemo> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text('Text Dialog Demo')),
      body: new Center(
        child: new RaisedButton(
          onPressed: _showDialog,
          child: new Text("Push Me"),
        ),
      ),
    );
  }

  _showDialog() async {
    await showDialog<String>(
      context: context,
      child: new AlertDialog(
        contentPadding: const EdgeInsets.all(16.0),
        content: new Row(
          children: <Widget>[
            new Expanded(
              child: new TextField(
                autofocus: true,
                decoration: new InputDecoration(
                    labelText: 'Full Name', hintText: 'eg. John Smith'),
              ),
            )
          ],
        ),
        actions: <Widget>[
          new FlatButton(
              child: const Text('CANCEL'),
              onPressed: () {
                Navigator.pop(context);
              }),
          new FlatButton(
              child: const Text('OPEN'),
              onPressed: () {
                Navigator.pop(context);
              })
        ],
      ),
    );
  }
}

void main() {
  runApp(new MaterialApp(home: new FocusVisibilityDemo()));
}

1 个答案:

答案 0 :(得分:11)

如果您的用例是在TextFields内添加多个Dialog,那么您的主Form不会变得拥挤,我认为如果您构建的内容比{{{{{} { 1}}和AlertDialog因为它们用于简单的活动(确认,无线电......等)。

否则,为什么要将SimpleDialog用于单个Dialog

当我们添加多个TextField时,我们应该注意我们的设计选择,因为其他人会与此视图交互以填充数据,在这种情况下,我更喜欢使用TextField属性{ {3}}课程。我不确定fullscreenDialog是否适合Flutter中的那个。

以下是有关如何使用SimpleDialog的快速示例,我希望此帮助您应该能够按照自己的方式进行修改:

FullScreenDialog

修改

在做了一些研究之后,在当前的Flutter版本中似乎PageRoute,临时修复也记录在这个问题中。

this is a bug

import 'package:flutter/material.dart';


void main() {
  runApp(new MaterialApp(home: new MyApp(),));
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => new MyAppState();
}

class MyAppState extends State<MyApp> {
  FullScreenDialog _myDialog = new FullScreenDialog();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text("Fill this form"),
        ),
        body: new Column(
          children: <Widget>[
            new TextField(controller: new TextEditingController(
                text: "Add a single text field"),),

            new Card(child: new ListTile(
              title: new Text("Click to add your top 3 amazing skills"),
              subtitle: new Text(
                  "${_myDialog._skillOne} ${_myDialog._skillTwo} ${_myDialog
                      ._skillThree}"),
              onTap: () {
                Navigator.push(context, new MaterialPageRoute(
                  builder: (BuildContext context) => _myDialog,
                  fullscreenDialog: true,
                ));
              },
            ),
            ),
          ],
        )
    );
  }

}


class FullScreenDialog extends StatefulWidget {
  String _skillOne = "You have";
  String _skillTwo = "not Added";
  String _skillThree = "any skills yet";

  @override
  FullScreenDialogState createState() => new FullScreenDialogState();
}

class FullScreenDialogState extends State<FullScreenDialog> {
  TextEditingController _skillOneController = new TextEditingController();
  TextEditingController _skillTwoController = new TextEditingController();

  TextEditingController _skillThreeController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text("Add your top 3 skills"),
        ),
        body: new Padding(child: new ListView(
          children: <Widget>[
            new TextField(controller: _skillOneController,),
            new TextField(controller: _skillTwoController,),
            new TextField(controller: _skillThreeController,),
            new Row(
              children: <Widget>[
                new Expanded(child: new RaisedButton(onPressed: () {
                  widget._skillThree = _skillThreeController.text;
                  widget._skillTwo = _skillTwoController.text;
                  widget._skillOne = _skillOneController.text;
                  Navigator.pop(context);
                }, child: new Text("Save"),))
              ],
            )
          ],
        ), padding: const EdgeInsets.symmetric(horizontal: 20.0),)
    );
  }


}