【问题标题】:How to Enable Button on certain boolean condition in Flutter?如何在 Flutter 中的某些布尔条件下启用按钮?
【发布时间】:2020-05-28 05:42:05
【问题描述】:

我正在尝试在从 firestore 文档派生的某些布尔条件下启用颤振应用程序中的按钮。在下面的代码中,应该调用共享函数checkTrueFalse(),并且应该派生checkFinalValue1和checkFinalValue2的bool值,如果checkFinalValue1是true,那么Button1应该在被按下时给出结果,并且如果checkFinalValue2 是true,则应启用Button2,否则应显示为禁用。

我们如何在 Flutter 应用程序中做到这一点?我无法找到 enable button 的 Flatbutton 属性,因为我们在 Android Studio 或 Xcode 中有它

Widget build(BuildContext context) {
    String listid1 = widget.listid1;
    bool checkFinalValue1, checkFinalValue2;


  checkTrueFalse() { final DocumentReference docRef =  Firestore.instance.collection("list").document(listid1);


    docRef.get().then((DocumentSnapshot ds) {
      checkFinalValue1 = ds.data["value1"];
      checkFinalValue2 = ds.data["value2"];

       });}

       final button1 = FlatButton();
    return Card(child: ListTile(title:Text(widget.itemTitle),subtitle: Column(
      children: <Widget>[
        Text(widget.commentId),
        Container(
            child: Row(
              children: <Widget>[
                FlatButton(
                  child: Text("Button1"),
                  onPressed: () {

                  print("Check 1");


                  },
                ),
                FlatButton(
                  child: Text("Button2"),
                  onPressed: () { 

                    print("Button Enabled");

                  },
                ),
              ],
            ))
      ],
    ), ),);

  }

【问题讨论】:

  • 所以你想隐藏按钮还是禁用它?
  • @jitsm555 我最初想禁用Button2,但是如果checkFinalValue2 的值是true,那么应该启用该按钮,但是对于Button1,调用的操作应该仅在checkFinalValue1 是真的,但Button1 应该始终启用
  • 禁用按钮,可以设置onPressed函数为null

标签: firebase flutter button google-cloud-firestore


【解决方案1】:

禁用扁平按钮喜欢

      FlatButton(
                child: Text("Button1"),
                onPressed: checkFinalValue1
                    ? () {
                        print("Check 1");
                      }
                    : null),

            FlatButton(
                child: Text("Button2"),
                onPressed: checkFinalValue2
                    ? () {
                        print("Button Enabled");

                      }
                    : null),

【讨论】:

  • checkFinalValue1和checkFinalValue2的值在调用checkTrueFalse()函数时会得到,应该怎么调用呢?
  • 你必须从其他地方调用它,比如 initState() 或任何小部件点击
猜你喜欢
  • 2016-04-13
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 2017-08-15
  • 1970-01-01
相关资源
最近更新 更多