颤动警报对话框

时间:2020-10-09 02:29:39

标签: flutter flutter-layout

是否有一种方法可以弹出或显示对话框,以在文本字段中输入无效的情况下显示错误消息?这是代码和输出,请给我一些建议。

代码:

TextFormField(
  decoration: InputDecoration(
    hintText: 'Email',
    hintStyle: TextStyle(
      fontSize: 14,
      fontWeight: FontWeight.normal,
      color: Hexcolor('#9fa6b4'),
    ),
    border: InputBorder.none,
    prefixIcon: Icon(
      Icons.mail,
      color: Hexcolor('#9fa6b4'),
    ),
  ),
  keyboardType: TextInputType.emailAddress,
  validator: (value) {
    if (value.isEmpty || !value.contains('@')) {
      return 'Invalid email!';
    }
  },
  onSaved: (value) {
    _authData['email'] = value;
  },
),

输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

在添加Dialog之前,必须调用showDialog函数来更改当前屏幕状态以显示中间Dialog弹出窗口。 在这里,我们使用AlertDialog小部件在主体中显示带有标题和一些文本的简单Dialog。

showDialog(
              context: context,
              builder: (BuildContext context){
                  return AlertDialog(
                    title: Text("Alert Dialog"),
                    content: Text("Dialog Content"),
                  );
              }
            )

在内容下,不需要仅添加文本,您可以添加任何小部件(例如图像)或其他内容。但是因为我们在简单的用例中使用“警告对话框”,所以最好在内容内部显示简单的文本或图像。

在这里,我们需要在单击“关闭”按钮时关闭当前对话框。 我们可以在Navigator类中使用pop方法。

FlatButton(
             child: Text("Close"),
             onPressed: (){
             Navigator.of(context).pop();
          },
      )