【问题标题】:How to use if else condition in a button - flutter如何在按钮中使用 if else 条件 - 颤动
【发布时间】:2019-03-20 09:03:29
【问题描述】:

在满足特定条件后用户按下此按钮后,我试图显示一个警告对话框。如果文本为空,它将弹出一个对话框,但是使用我在下面尝试的方法,即使按下按钮后文本不为空,它仍然会弹出对话框。

RaisedButton(
                onPressed: priceController.text == ""
                    ? () => showDialog(
                        context: context,
                        builder: (BuildContext context) {
                          return AlertDialog(
                            title: Text("Enter a price"),
                          );
                        })
                    : () => apiRequest(url, {
                          'price': priceController.text,
                          'user_id': "user2"
                        }),
                child: Text("Set Level"),
              );

【问题讨论】:

  • 确实很奇怪,它应该可以工作,使用String.isEmpty 方法,或者使用if/else bloc 编码的折射器怎么样
  • 你能展示你的完整代码吗,也许你的控制器有问题?

标签: button flutter conditional-statements


【解决方案1】:

与其拥有两个函数,不如只拥有一个并在其中编写您的逻辑。 像这样:

RaisedButton(
  onPressed: () {
    if (priceController.text == "") {
      showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text("Enter a price"),
            );
          });
    } else {
      apiRequest(url, {'price': priceController.text, 'user_id': "user2"});
    }
  },
  child: Text("Set Level"),
);

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多