简体   繁体   中英

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

This is what I have so far: This code returns the correct isShow values. If the value from Firebase is yes it returns isShow with true and if the value from Firebase is something other than yes it returns isShow with false. So it seems to work when I try it, but after a while the Button hide/show at random, but the values are still correct.

So I think I am on the right track, just missing something.

Code:

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'),
                      ),
                    ),
                  ]
              ),
            ),
          ));
    });
  }

You Can Do Like This

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),
}

Replace Your Widgets where SizeBox Declare.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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