【问题标题】:Move up Flutter AlertDialog上移 Flutter AlertDialog
【发布时间】:2018-03-19 13:13:27
【问题描述】:

我正在尝试用 Flutter.io 做一些事情 但我有一些问题。它没有很好地记录在我的操作中。

我想获得一个带有 AlertDialog 的 Android 应用程序。但是我的键盘在 Alertdialog 上方。所以我找到了一种解决方法,可以在键盘打开时将对话框向上推,但我不太明白。

所以我的计划是向上移动 AlertDialog。此刻居中。我想在“AlertDialog”中添加一个边距底部。

这是我的函数,如果我单击 Fabbutton,它将打开对话框。

void _openNewInputPopup() {
    print("popup will open");

    AlertDialog dialog = new AlertDialog(
      content: new SingleChildScrollView(
        child: new Column(
          children: <Widget>[
            new Text('Wie lautet das Versteck?',
            style: new TextStyle(fontSize: 18.0),),
            new TextField(onChanged: (String value) {
              {
                print("changed $value");
              }
            },),
          ],
        ),
      ),

      actions: <Widget> [
        new FlatButton(onPressed: () {
          _dialogResult(MyDialogAction.Abbrechen);
        }, child: new Text('Abbrechen')),

        new FlatButton(onPressed: () {
          _dialogResult(MyDialogAction.Anlegen);
          }, child: new Text('Anlegen')),
      ]
    );


    showDialog(context: context, child: dialog);
  }

如果你们当中有人对 Flutter 有更多经验并且可以帮助我,那就太好了:) 谢谢

【问题讨论】:

  • 有一个修复工作正在进行中,当键盘出现github.com/flutter/flutter/pull/15426
  • 谢谢,但我很愚蠢,无法理解这是如何工作的......
  • 我自己没有仔细看这个。我想您需要使用 MediaQuery 来获取当前大小并相应地调整布局

标签: dart flutter


【解决方案1】:

我根据文本字段是否有焦点来调整填充。我希望它会有所帮助..

return SingleChildScrollView(
                padding: EdgeInsets.only(top: yourFocus.hasFocus ? 
                        MediaQuery.of(context).size.height / 2 - 250 // adjust values according to your need
                        : MediaQuery.of(context).size.height / 2 - 130),// adjust values according to your need
                child: AlertDialog(
                    title: Text("your Alert header text"),
                    content: TextField(
                             focusNode: yourFocus,
                             controller: yourTextController,
                             decoration: InputDecoration.collapsed(
                                        hintText: "your hint text."),
                             ),
                    actions: <Widget>[
                      FlatButton(
                          child: Text("Your button Title"),
                          onPressed: () {
                           yourFunction();
                          })
                    ]));

【讨论】:

    【解决方案2】:

    在父 Widget 底部添加内边距或外边距:

    margin: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
    

    padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
    

    并将 resizeToAvoidBottomInset: false, 添加到您的 Scaffold 小部件

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 2020-02-14
      • 2019-10-10
      • 2022-01-08
      • 2018-10-23
      • 1970-01-01
      • 2019-09-16
      • 2019-05-30
      • 2020-12-09
      相关资源
      最近更新 更多