繁体   English   中英

如何在 Flutter/dart 中隐藏带有 if 条件的小部件?

[英]How to hide a widget with if an if condition in Flutter/dart?

这就是我到目前为止所拥有的:此代码返回正确的isShow值。 如果Firebase的值为yes ,则返回isShow为 true;如果Firebase的值不是yes ,则返回isShow为 false。 所以当我尝试它时它似乎工作,但过了一会儿Button隐藏/显示随机,但值仍然是正确的。

所以我认为我在正确的轨道上,只是错过了一些东西。

代码:

bool isShow = true;

  updateToolDialog(BuildContext context, id) async {

    return showDialog(context: context, builder: (context) {
      FirebaseFirestore.instance.collection('users').doc(id).get().then((querySnapshot) {

        statusservice = querySnapshot.get('status').toString();

          setState((){
          if(statusservice.contains('yes')) {
            isShow = true;
            print("should be true $isShow");
           
          }else{
            isShow = false;
            print("should be false $isShow");
            
          }});

      });
      return Dialog(
          child: Container(
            child: SizedBox(
              width: 600,
              child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text('Endre verktøy', style: TextStyle(
                        fontWeight: FontWeight.bold
                    ),),
                    SizedBox(
                      height: 10,
                    ),
                   
                    Visibility(
                      visible: isShow,
                      child: ElevatedButton(
                        onPressed: () async{


                          FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;

                          await firebaseFirestore.collection('users').doc(id).update({

                            'Status' : 'No',

                          });

                          Navigator.pop(context, true);
                          _initializeData();
                        },

                        child: const Text('Change status'),
                      ),
                    ),
                  ]
              ),
            ),
          ));
    });
  }

你可以这样做

if(isSize.width ==100) {
SizedBox(width: 20, height: 100),
}else if(isSize.width ==50) {
SizedBox(width: 20, height: 50),
}
else {
SizedBox(width: 20, height: 20),
}

替换 SizeBox 声明的小部件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM