Flutter 点击按钮弹出底部弹框

点击弹出底部弹框如下:

先看效果图:

特点是页面不跳转

 

 点击弹出弹框,并且添加了一个返回按钮

按钮部分:

class MyApp1 extends StatefulWidget {
  const MyApp1({Key? key}) : super(key: key);

  @override
  _MyApp1State createState() => _MyApp1State();
}

class _MyApp1State extends State<MyApp1> {
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Column(children: [
          SizedBox(
            height: 60,
          ),
          FlatButton(
            height: 50,//高
            color: Colors.blue.withOpacity(.2),//颜色
            onPressed: () {//点击事件
              int a = (_show(context)) as int;//调用底部弹框
            },
            child: Text("点击弹出底部弹框"),
          ),
        ]),
      ],
    );
  }
}

然后是弹框部分代码 

Future<int?> _show(context) async {
  //使用了一个异步
  var size = MediaQuery.of(context).size; //获取屏幕宽高和Widget大小
  return showModalBottomSheet(
    //showModalBottomSheet函数底部面板相当于一个新的页面
    backgroundColor: Colors.transparent,
    //颜色空白
    isDismissible: true,
    //能否点击消失
    isScrollControlled: true,
    //能否拖动消失
    context: context,
    //接受cotext
    builder: (context) {
      return Container(
        width: size.width,
        height: size.height * 0.7, //使用屏幕宽高比设置大小
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            //圆角
            topLeft: Radius.circular(30), //左上角的圆角
            topRight: Radius.circular(30), //右上角的圆角
          ),
        ),
        child: Column(
          children: [
            Row(
              //如果想要设置一个返回的按钮可以这样写
              children: [
                IconButton(
                  icon: Icon(Icons.chevron_left_outlined),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                )
              ],
            )
          ],
        ),
      );
    },
  );
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值