Close modal bottom sheet programmatically in flutter

后端 未结 4 2042
迷失自我
迷失自我 2020-12-25 09:23

I am displaying a BottomSheet via showModalBottomSheet() and inside several widgets with a GestureDetector. I would like to see the BottomSheet clos

4条回答
  •  时光取名叫无心
    2020-12-25 10:05

    class _FABState extends State {
      bool isOpen = false;
    
      var bottomSheetController;
    
      @override
      Widget build(BuildContext context) {
        return FloatingActionButton(
          onPressed: () {
            setState(() {
              isOpen = !isOpen;
            });
            print('tapped on the bottom sheet');
            if(isOpen) {
              bottomSheetController = showBottomSheet(
                  backgroundColor: Colors.transparent,
                  context: context,
                  builder: (ctx) {
                    return ClipRRect(
                      borderRadius: BorderRadius.only(
                        topRight: Radius.circular(20),
                        topLeft: Radius.circular(20),
                      ),
                      child: Container(
                        height: 150,
                        color: Colors.black,
                        child: TextField()
                      ),
                    );
                  });
              bottomSheetController.closed.then((value) {
                setState(() {
                  isOpen = !isOpen;
                });
              });
            } else {
              Navigator.of(context).pop();
              setState(() {
                isOpen = !isOpen;
              });
            }
          },
          child: isOpen?Icon(Icons.arrow_downward):Icon(Icons.arrow_upward),
        );
      }
    }
    

提交回复
热议问题