简体   繁体   English

flutter showModalBottomSheet 在单击后退按钮时不弹出

[英]flutter showModalBottomSheet not pop when back button clicked

i want to pop showModalBottomSheet when i clicked the button of back in android mobile.当我单击 android 手机中的返回按钮时,我想弹出 showModalBottomSheet。 this is my showModalBottomSheet example:这是我的 showModalBottomSheet 示例:

    showModalBottomSheet(
   context: contextt,
   useRootNavigator: true,
   isScrollControlled: true,
   isDismissible: true,
   enableDrag: true,
   shape: RoundedRectangleBorder(
   borderRadius: BorderRadius.vertical(top: Radius.circular(mySize.curve_large),
     ),),
   clipBehavior: Clip.antiAliasWithSaveLayer,
   builder: (contextt) {
      FocusScope.of(contextt).requestFocus(focusNode);
      return SizedBox(); });},

I use WillPopScope before my example widget "SizedBox", but its not work:我在示例小部件“SizedBox”之前使用WillPopScope ,但它不起作用:

WillPopScope(
 onWillPop: () async {
 FocusScope.of(contextt).requestFocus(FocusNode());
 Navigator.of(contextt).pop();
 return false;
 }, 

can anyone help me how can I do that?谁能帮助我我该怎么做?

and by the way, when I clicked the back button twice in a row, showModalBottomSheet popped.顺便说一句,当我连续两次单击后退按钮时,showModalBottomSheet 弹出。 I want to do that with one click..我想一键搞定..

I have implemented the same modal as you, but when I click android system back button the modal is closing as it's supposed to.我已经实现了与您相同的模式,但是当我单击 android 系统后退按钮时,模式正在按预期关闭。 Check the following code for builder implementation:检查以下代码以获取构建器实现:

import 'package:flutter/material.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        body: MyStatelessWidget()
      ),
    ),
  );
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        FocusScope.of(context).requestFocus(FocusNode());
        Navigator.of(context).pop();
        return false;
      },
      child: Center(
        child: ElevatedButton(
          child: const Text('showModalBottomSheet'),
          onPressed: () {
            showModalBottomSheet<void>(
              useRootNavigator: true,
              isScrollControlled: true,
              isDismissible: true,
              enableDrag: true,
              context: context,
              clipBehavior: Clip.antiAliasWithSaveLayer,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.vertical(top: Radius.circular(80),
                ),),
              builder: (BuildContext context) {
                return Container(
                  height: 200,
                  color: Colors.amber,
                  child: Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        const Text('Modal BottomSheet'),
                        ElevatedButton(
                          child: const Text('Close BottomSheet'),
                          onPressed: () => Navigator.pop(context),
                        )
                      ],
                    ),
                  ),
                );
              },
            );
          },
        ),
      ),
    );
  }
}

try this尝试这个

WillPopScope(
    onWillPop: () async {
        return (
            await showModalBottomSheet(
                //your code
            )
        )
    }
)

Not the exact solution Another workaround can be不是确切的解决方案 另一种解决方法是

  showModalBottomSheet().then((value)=>showModalBottomSheet());

The whole idea is calling again the bottom sheet when it is getting closed整个想法是在关闭时再次调用底部表

OR For the exact solution try this或者对于确切的解决方案试试这个

You can also use willpopscope to the builder/ bottomsheet which you are displaying您还可以将 willpopscope 用于您正在显示的构建器/底页

WillPopScope(
 onWillPop:() async{
   return false;
    },
    child:YourBottomSheet());

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

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