显示模态弹窗,可以只有一个确定按钮,也可以同时有确定和取消按钮。

    OBJECT参数说明

    参数 类型 必填 默认值 说明
    context BuildContext 是 每个组件对应的context
    title String 否 提示 提示的标题
    content String 否
    提示的内容
    showTitle bool 否 true 是否显示标题
    barrierDismissible bool 否 true 点击遮罩关闭弹窗
    transitionType TransitionType 否 TransitionType.scale 动画方式
    showCancel bool 否 true 是否显示取消按钮
    cancelText String 否 取消 取消按钮文字
    affirmText String 否 确认 确认按钮文字
    cancelTextColor Color 否 Color(0xff606266) 取消按钮文字颜色
    affirmTextColor Color 否 Color(0xff546bf2) 确认按钮文字颜色
    affirm Function 否 点击确认按钮事件
    cancel Function 否 点击取消按钮事件

    transitionType值说明

    参数 说明
    TransitionType.inFromLeft 从左边滑入
    TransitionType.inFromRight 从右边滑入
    TransitionType.inFromTop 从上边滑入
    TransitionType.inFromBottom 从下边滑入
    scale 放大缩小
    fade 渐入渐出
    rotation 旋转
    size
    1. import 'package:flutter/material.dart';
    2. import 'package:flutter_screenutil/flutter_screenutil.dart';
    3. import 'package:flutter_xb_app/utils/show_dialog/show_animation_dialog.dart';
    4. Future<T> showModelDialog<T>({
    5. @required BuildContext context,
    6. String title = "提示", //标题
    7. String content, //提示的内容
    8. bool showTitle = true, //是否显示标题
    9. bool barrierDismissible = true, //点击遮罩关闭弹窗
    10. TransitionType transitionType = TransitionType.scale, //动画方式
    11. bool showCancel = true, //是否显示取消按钮
    12. String cancelText = "取消", //取消按钮文字
    13. String affirmText = "确认", //确认按钮文字
    14. Color cancelTextColor = const Color(0xff606266), //取消按钮文字颜色
    15. Color affirmTextColor = const Color(0xff546bf2), //确认按钮文字颜色
    16. Function cancel, //点击取消按钮事件
    17. Function affirm, //点击确认按钮事件
    18. }) {
    19. showAnimationDialog(
    20. context: context,
    21. transitionType: transitionType,
    22. barrierDismissible: barrierDismissible,
    23. builder: (context) {
    24. return Center(
    25. child: Container(
    26. width: ScreenUtil().setWidth(600),
    27. height: ScreenUtil().setHeight(320),
    28. decoration: BoxDecoration(
    29. color: Colors.white,
    30. borderRadius: BorderRadius.all(
    31. Radius.circular(ScreenUtil().radius(12)),
    32. ),
    33. ),
    34. child: Column(
    35. children: [
    36. Expanded(
    37. child: Column(
    38. children: [
    39. showTitle ? _showModelTitle(title) : Container(),
    40. Expanded(child: _showModelContent(content)),
    41. ],
    42. ),
    43. ),
    44. Container(
    45. width: ScreenUtil().setWidth(600),
    46. height: ScreenUtil().setHeight(2),
    47. decoration: BoxDecoration(
    48. color: Color(0xffecedf0),
    49. ),
    50. ),
    51. Container(
    52. height: ScreenUtil().setHeight(98),
    53. child: Row(
    54. children: [
    55. showCancel
    56. ? Expanded(
    57. child: InkWell(
    58. onTap: () {
    59. Navigator.pop(context);
    60. cancel();
    61. },
    62. child:
    63. _showModelCancel(cancelText, cancelTextColor),
    64. ),
    65. )
    66. : Container(),
    67. Expanded(
    68. child: InkWell(
    69. onTap: () {
    70. Navigator.pop(context);
    71. affirm();
    72. },
    73. child: _showModelAffirm(affirmText, affirmTextColor),
    74. ),
    75. ),
    76. ],
    77. ),
    78. )
    79. ],
    80. ),
    81. ),
    82. );
    83. },
    84. );
    85. }
    86. //标题
    87. Widget _showModelTitle(title) {
    88. Widget titleWidget = Container(
    89. margin: EdgeInsets.only(top: ScreenUtil().setHeight(40)),
    90. child: Text(
    91. title,
    92. style: TextStyle(
    93. fontSize: ScreenUtil().setSp(32),
    94. fontWeight: FontWeight.w600,
    95. color: Color(0xff333333),
    96. ),
    97. ),
    98. );
    99. return titleWidget;
    100. }
    101. //内容
    102. Widget _showModelContent(content) {
    103. Widget contentWidget = Container(
    104. width: ScreenUtil().setWidth(600),
    105. padding: EdgeInsets.fromLTRB(
    106. ScreenUtil().setWidth(30),
    107. 0,
    108. ScreenUtil().setWidth(30),
    109. 0,
    110. ),
    111. child: Center(
    112. child: Text(
    113. content,
    114. style: TextStyle(
    115. fontSize: ScreenUtil().setSp(30),
    116. color: Color(0xff333333),
    117. ),
    118. ),
    119. ),
    120. );
    121. return contentWidget;
    122. }
    123. //取消按钮
    124. Widget _showModelCancel(cancelText, cancelTextColor) {
    125. Widget cancelWidget = Center(
    126. child: Text(
    127. cancelText,
    128. style: TextStyle(
    129. fontSize: ScreenUtil().setSp(30),
    130. color: cancelTextColor,
    131. fontWeight: FontWeight.w300,
    132. ),
    133. ),
    134. );
    135. return cancelWidget;
    136. }
    137. //确认按钮
    138. Widget _showModelAffirm(affirmText, affirmTextColor) {
    139. Widget affirmWidget = Center(
    140. child: Text(
    141. affirmText,
    142. style: TextStyle(
    143. fontSize: ScreenUtil().setSp(30),
    144. color: affirmTextColor,
    145. fontWeight: FontWeight.w300,
    146. ),
    147. ),
    148. );
    149. return affirmWidget;
    150. }

    示例:

    1. ShowModelDialog(
    2. context: context,
    3. title: "提示",
    4. content: "这是一个模态弹窗",
    5. cancel: () {print("点击了取消按钮")},
    6. affirm: () {print("点击了确认按钮")},
    7. );