显示模态弹窗,可以只有一个确定按钮,也可以同时有确定和取消按钮。
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 |
import 'package:flutter/material.dart';import 'package:flutter_screenutil/flutter_screenutil.dart';import 'package:flutter_xb_app/utils/show_dialog/show_animation_dialog.dart';Future<T> showModelDialog<T>({@required BuildContext context,String title = "提示", //标题String content, //提示的内容bool showTitle = true, //是否显示标题bool barrierDismissible = true, //点击遮罩关闭弹窗TransitionType transitionType = TransitionType.scale, //动画方式bool showCancel = true, //是否显示取消按钮String cancelText = "取消", //取消按钮文字String affirmText = "确认", //确认按钮文字Color cancelTextColor = const Color(0xff606266), //取消按钮文字颜色Color affirmTextColor = const Color(0xff546bf2), //确认按钮文字颜色Function cancel, //点击取消按钮事件Function affirm, //点击确认按钮事件}) {showAnimationDialog(context: context,transitionType: transitionType,barrierDismissible: barrierDismissible,builder: (context) {return Center(child: Container(width: ScreenUtil().setWidth(600),height: ScreenUtil().setHeight(320),decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().radius(12)),),),child: Column(children: [Expanded(child: Column(children: [showTitle ? _showModelTitle(title) : Container(),Expanded(child: _showModelContent(content)),],),),Container(width: ScreenUtil().setWidth(600),height: ScreenUtil().setHeight(2),decoration: BoxDecoration(color: Color(0xffecedf0),),),Container(height: ScreenUtil().setHeight(98),child: Row(children: [showCancel? Expanded(child: InkWell(onTap: () {Navigator.pop(context);cancel();},child:_showModelCancel(cancelText, cancelTextColor),),): Container(),Expanded(child: InkWell(onTap: () {Navigator.pop(context);affirm();},child: _showModelAffirm(affirmText, affirmTextColor),),),],),)],),),);},);}//标题Widget _showModelTitle(title) {Widget titleWidget = Container(margin: EdgeInsets.only(top: ScreenUtil().setHeight(40)),child: Text(title,style: TextStyle(fontSize: ScreenUtil().setSp(32),fontWeight: FontWeight.w600,color: Color(0xff333333),),),);return titleWidget;}//内容Widget _showModelContent(content) {Widget contentWidget = Container(width: ScreenUtil().setWidth(600),padding: EdgeInsets.fromLTRB(ScreenUtil().setWidth(30),0,ScreenUtil().setWidth(30),0,),child: Center(child: Text(content,style: TextStyle(fontSize: ScreenUtil().setSp(30),color: Color(0xff333333),),),),);return contentWidget;}//取消按钮Widget _showModelCancel(cancelText, cancelTextColor) {Widget cancelWidget = Center(child: Text(cancelText,style: TextStyle(fontSize: ScreenUtil().setSp(30),color: cancelTextColor,fontWeight: FontWeight.w300,),),);return cancelWidget;}//确认按钮Widget _showModelAffirm(affirmText, affirmTextColor) {Widget affirmWidget = Center(child: Text(affirmText,style: TextStyle(fontSize: ScreenUtil().setSp(30),color: affirmTextColor,fontWeight: FontWeight.w300,),),);return affirmWidget;}
示例:
ShowModelDialog(context: context,title: "提示",content: "这是一个模态弹窗",cancel: () {print("点击了取消按钮")},affirm: () {print("点击了确认按钮")},);
