【问题标题】:How to show AllertDialog if Textfield is empty in Flutter如果 Flutter 中的 Textfield 为空,如何显示 AllertDialog
【发布时间】:2021-09-23 17:59:53
【问题描述】:

我正在 Flutter 上构建应用程序,但由于列表而出现问题。

在颤振中,我做了一个空列表,在这个列表中由用户填写。当用户输入一个值时会发生分配,但这总是在输入一个值时发生。如果未输入任何值,则它反映列表中的先前值。如果在 TextField 中没有输入字符串值,我想要程序创建一个 Alertdialog。

这是我的清单 (TaskData) enter image description here

还有我的 TextField enter image description here

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    在你的 addTask 方法中试试这个

    void addTask() {
    if (name = '') showMyAlertDialog();
    else {nameList.add(name);
    notifyListeners();}
    name = ''; 
    }
    

    【讨论】:

    • 另外,如果有人想与提供者一起实施,这也是一种选择
    【解决方案2】:

    尝试添加errorText

    完整的example

    final _text = TextEditingController();
    
    TextField(
      controller: _text,
      decoration: InputDecoration(
        labelText: 'Enter the Value',
        errorText: _validate ? 'Value Can\'t Be Empty' : null, //here
      ),
    ),
    

    在按钮内部使用:

    bool _validate = false;
    
    RaisedButton(
      onPressed: () {
        setState(() {
          _text.text.isEmpty ? _validate = true : _validate = false;
        });
      },
      child: Text('Submit')    
      textColor: Colors.white,
      color: Colors.blueAccent,
    ),
    

    【讨论】:

    猜你喜欢
    • 2019-01-25
    • 2019-07-10
    • 2022-12-21
    • 1970-01-01
    • 2019-11-17
    • 2020-10-27
    • 2020-08-17
    • 1970-01-01
    • 2019-10-13
    相关资源
    最近更新 更多