skip to Main Content

how can i controll the default pop property of bottom sheet.Like I want to asign a value to a variable when showModalBottomSheet is popped .I have tried to do with controllers

5

Answers


  1. Chosen as BEST ANSWER

    thanks for your help

    I solved the problem with WillPopScope

    popfunction() {
          SelectedValue = tempValue;
          Navigator.pop(context, true);
        }
    
    
    onWillPop: () async {
            return popfunction() ?? false;
          },
    

  2. Why don’t you just do :

     showModalBottomSheet(
                        context: context,
                        builder: (context) {
                            var a = "desired value";
                            return Widget;
    
    Login or Signup to reply.
  3. you can trigger when the bottom sheet is popped/dismissed with an AnimationController like this:

    in your StatefulWidget‘s State:

      late AnimationController _controller;
    
      @override
      void initState() {
        _controller = AnimationController(
          vsync: this,
          duration: const Duration(milliseconds: 300),
        );
    
        _controller.addListener(() {
          if (_controller.isDismissed) {
            print("dismissed");
          }
        });
        super.initState();
      }
    
    @override
      void dispose() {
        _controller.dispose;
        super.dispose();
      }
    

    in your showModalBottomSheet:

    showModalBottomSheet(
              context: context,
              builder: (context) => Container(),
              transitionAnimationController: _controller, //  assign the controller
            );
    
    Login or Signup to reply.
  4. You can set isDismissible: false, and than add one button (Close button) on tap of button, you have to do your code and pop the bottomSheet.

    showModalBottomSheet(
       isScrollControlled: true,
       isDismissible: false,
       shape: const RoundedRectangleBorder(
         borderRadius: BorderRadius.vertical(
           top: Radius.circular(15),
         ),
       ),
       context: context,
       builder: (context) {
       return SizedBox(
              height:
                   MediaQuery.of(context).size.height * (0.6),
              child: Padding(
                   padding: const EdgeInsets.only(top: 15),
                   child: Column(
                          children: [
                           Row(
                            mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                              children: [
                                 InkWell(
                                    onTap: () {
    // Add your code here. which you want to perform before closing bottomSheet
                                        Navigator.pop(context);
                                     },
                                     child: const Icon(Icons.close)),
                                 InkWell(
                                     onTap: () {},
                                     child: const Text(
                                     "Reset",
                                      )),
                                ],
                              ),
                            const SizedBox(height: 15),
                            //Other widgets of bottomSheet
                            Container(
                               height:
                                 MediaQuery.of(context).size.height * (0.5),
                               color: Colors.amber,
                            )
                       ],
                  ),
             ),
         );
    });
    
    Login or Signup to reply.
  5. if you want to click on the back button , showModalBottomSheet closed ,
    I suggest used useRootNavigator: true in showModalBottomSheet

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search