从底部向上弹出操作菜单。

    OBJECT参数说明

    参数 类型 必填 说明
    context context 是 每个组件对应的context
    list Array 是 按钮的文字数组。例:[‘摄像头’, ‘从相册选取’]
    success Function 否 接口调用成功的回调函数,详见返回参数说明
    fail Function 否 接口调用失败的回调函数

    success返回参数说明

    参数 类型 说明
    tapIndex int 用户点击的按钮,从上到下的顺序,从0开始
    1. import 'package:flutter/material.dart';
    2. import 'package:flutter_screenutil/flutter_screenutil.dart';
    3. import 'package:flutter_xb_app/common/unify_color/unify_color.dart';
    4. Future ShowActionSheet({
    5. @required BuildContext context,
    6. @required List list,
    7. Function success,
    8. Function fail,
    9. }) {
    10. success != null ? success : () {};
    11. fail != null ? fail : () {};
    12. showModalBottomSheet(
    13. context: context,
    14. shape: RoundedRectangleBorder(
    15. borderRadius: BorderRadius.vertical(
    16. top: Radius.circular(
    17. ScreenUtil().radius(36),
    18. ),
    19. ),
    20. ),
    21. builder: (BuildContext context) {
    22. return Container(
    23. height: ScreenUtil().setHeight((list.length + 1) * 100 + (list.length - 1) * 1 + 24),
    24. child: Column(
    25. children: [
    26. Container(
    27. child: Column(
    28. children: getItemListWidget(context, list, success),
    29. ),
    30. ),
    31. Container(
    32. height: ScreenUtil().setHeight(24),
    33. color: Color(UnifyColor.borderColor),
    34. ),
    35. InkWell(
    36. onTap: () {
    37. Navigator.of(context).pop();
    38. fail();
    39. },
    40. child: Container(
    41. height: ScreenUtil().setHeight(100),
    42. child: Center(
    43. child: Text(
    44. "取消",
    45. style: TextStyle(
    46. fontSize: ScreenUtil().setSp(30),
    47. color: Color(UnifyColor.fontTitleColor),
    48. ),
    49. ),
    50. ),
    51. ),
    52. ),
    53. ],
    54. ),
    55. );
    56. },
    57. );
    58. }
    59. getItemListWidget(context, List itemList, Function success) {
    60. List<Widget> itemListWidget = [];
    61. for (var i = 0; i < itemList.length; i++) {
    62. itemListWidget.add(
    63. Container(
    64. child: Column(
    65. children: [
    66. InkWell(
    67. onTap: () {
    68. Navigator.of(context).pop();
    69. success(i);
    70. },
    71. child: Container(
    72. height: ScreenUtil().setHeight(100),
    73. child: Center(
    74. child: Text(
    75. itemList[i],
    76. style: TextStyle(
    77. fontSize: ScreenUtil().setSp(30),
    78. color: Color(UnifyColor.fontTitleColor),
    79. ),
    80. ),
    81. ),
    82. ),
    83. ),
    84. i != itemList.length - 1
    85. ? Container(
    86. height: ScreenUtil().setHeight(1),
    87. color: Color(UnifyColor.dividerLineColor),
    88. )
    89. : Container(),
    90. ],
    91. ),
    92. ),
    93. );
    94. }
    95. return itemListWidget;
    96. }

    示例:

    1. List<String> itemList = ['拍照', '从手机相册选择', '保存图片'];
    2. ShowActionSheet(
    3. context: context,
    4. list: itemList,
    5. success: (tapIndex) {
    6. if (tapIndex == 0) {
    7. getImage(ImageSource.camera);
    8. }
    9. if (tapIndex == 1) {
    10. getImage(ImageSource.gallery);
    11. print("点击了从相册获取按钮");
    12. }
    13. if (tapIndex == 2) {
    14. print("点击了保存图片按钮");
    15. }
    16. },
    17. );