123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- import 'package:flutter/material.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:get/get.dart';
-
- typedef ModalFunc = dynamic Function();
-
- /// func 函数如果返回 bool 值, 则只有 true 的情况下才默认关闭弹窗
- /// 如果返回 Future , 则只有正常结束的时候 才关闭弹窗
- /// 其余情况, 都默认关闭弹窗
- void _handleFunc(ModalFunc? func, bool lazy) {
- // 是否立即关闭
- _close(bool canClose) {
- if (canClose) {
- _closeModal();
- }
- }
-
- ;
-
- // 如果 lazy 是 true 则不会立即关闭
- _close(!lazy);
-
- if (null != func) {
- dynamic res = func();
- if (res.runtimeType == bool) {
- if (res && lazy) {
- _close(lazy);
- }
- } else if (res is Future) {
- res.then((_) => _close(lazy));
- } else {
- _close(lazy);
- }
- } else {
- _close(lazy);
- }
- }
-
- /// 打开一个 dialog
- void showDialog(
- {required String title,
- lazy = false,
- String? message,
- Widget? content,
- ModalFunc? onConfirm,
- ModalFunc? onCancel}) {
- Get.dialog(Modal(
- type: 'dialog',
- title: title,
- message: message,
- content: content,
- onConfirm: () => _handleFunc(onConfirm, lazy),
- onCancel: () => _handleFunc(onCancel, lazy),
- ));
- }
-
- /// 打开一个 alert
- void showAlert(
- {required String title,
- bool lazy = false,
- String? message,
- Widget? content,
- ModalFunc? onConfirm}) {
- Get.dialog(Modal(
- type: 'alert',
- title: title,
- message: message,
- content: content,
- onConfirm: () => _handleFunc(onConfirm, lazy),
- ));
- }
-
- void _closeModal() {
- // Get.back(closeOverlays: true);
- Navigator.of(Get.overlayContext!, rootNavigator: true).pop();
- }
-
- class Modal extends StatelessWidget {
- String title;
- String type;
- String? message;
- Widget? content;
- VoidCallback? onConfirm;
- VoidCallback? onCancel;
-
- final _titleStyle = TextStyle(
- color: const Color(0xFF222222),
- fontSize: 20.sp,
- fontWeight: FontWeight.bold);
- final _messageStyle =
- TextStyle(color: const Color(0xFF666666), fontSize: 18.sp);
- final _primaryColor = const Color(0xFFFF703B);
- final _btnSize = Size(90.w, 36.w);
- final _btnShape = RoundedRectangleBorder(
- side: const BorderSide(color: Color(0xFFFF703B), width: 2.0),
- borderRadius: BorderRadius.all(Radius.circular(24.5.w)),
- );
-
- void _handleConfirm() {
- if (null != onConfirm) {
- onConfirm!();
- }
- }
-
- void _handleCancel() {
- if (null != onCancel) {
- onCancel!();
- }
- }
-
- Modal({
- Key? key,
- this.type = 'alert',
- required this.title,
- this.message,
- this.content,
- this.onConfirm,
- this.onCancel,
- }) : super(key: key);
-
- Widget _confirmBtn() {
- return ElevatedButton(
- child: Text("确定",
- style: TextStyle(
- color: Colors.white, fontSize: 18.sp, letterSpacing: 5.sp)),
- style: ElevatedButton.styleFrom(
- primary: _primaryColor,
- elevation: 0,
- minimumSize: _btnSize,
- shape: _btnShape,
- ),
- onPressed: _handleConfirm,
- );
- }
-
- Widget _cancelBtn() {
- return ElevatedButton(
- child: Text("取消",
- style: TextStyle(
- color: _primaryColor, fontSize: 18.sp, letterSpacing: 5.sp)),
- style: ElevatedButton.styleFrom(
- primary: Colors.white,
- elevation: 0,
- minimumSize: _btnSize,
- shape: _btnShape,
- ),
- onPressed: _handleCancel,
- );
- }
-
- Widget _alert() {
- return SimpleDialog(
- shape:
- RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
- titleTextStyle: _titleStyle,
- title: Text(title, textAlign: TextAlign.center),
- children: [
- if (null != message)
- Text(message!, textAlign: TextAlign.center, style: _messageStyle),
- if (null != content) content!,
- SizedBox(height: 16.w),
- Center(
- child: _confirmBtn(),
- ),
- SizedBox(height: 10.w),
- ]);
- }
-
- Widget _dialog() {
- Widget hSpacer = SizedBox(width: 16.w);
-
- return SimpleDialog(
- shape:
- RoundedRectangleBorder(borderRadius: BorderRadius.circular(24.w)),
- titleTextStyle: _titleStyle,
- title: Text(title, textAlign: TextAlign.center),
- alignment: Alignment.center,
- children: [
- if (null != message)
- Text(message!, textAlign: TextAlign.center, style: _messageStyle),
- if (null != content) content!,
- SizedBox(height: 16.w),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- hSpacer,
- Center(
- child: _confirmBtn(),
- ),
- hSpacer,
- Center(
- child: _cancelBtn(),
- ),
- hSpacer,
- ],
- ),
- SizedBox(height: 10.w),
- ]);
- }
-
- @override
- Widget build(BuildContext context) {
- return type == 'alert' ? _alert() : _dialog();
- }
- }
|