| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- import 'package:electronic_assistant/popup/talk_popup.dart';
- import 'package:electronic_assistant/utils/expand.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/widgets.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
- class EAAlertDialog {
- static void show({
- String? title,
- String? contentText,
- Widget? contentWidget,
- String? confirmText,
- String? cancelText,
- VoidCallback? cancelOnTap,
- VoidCallback? confirmOnTap,
- VoidCallback? onDismiss,
- }) {
- SmartDialog.show(
- builder: (_) {
- return _EAAlertDialog(
- title: title,
- content: contentText,
- contentWidget: contentWidget,
- confirmText: confirmText,
- cancelText: cancelText,
- confirmOnTap: confirmOnTap,
- cancelOnTap: cancelOnTap);
- },
- tag: "EAAlertDialog",
- onDismiss: onDismiss,
- clickMaskDismiss: false,
- );
- }
- static void dismiss() {
- SmartDialog.dismiss(tag: "EAAlertDialog");
- }
- }
- class _EAAlertDialog extends Dialog {
- // 标题
- final String? title;
- final Widget? contentWidget;
- // 内容
- final String? content;
- // 取消按钮文字
- final String? cancelText;
- // 取消按钮回调
- final VoidCallback? cancelOnTap;
- // 确认按钮文字
- final String? confirmText;
- // 确认按钮文字回调
- final VoidCallback? confirmOnTap;
- const _EAAlertDialog({
- this.title,
- this.content,
- this.contentWidget,
- required this.cancelText,
- required this.confirmText,
- this.cancelOnTap,
- this.confirmOnTap,
- });
- @override
- Widget build(BuildContext context) {
- return Center(
- child: Column(
- children: [
- const Spacer(),
- Container(
- alignment: Alignment.center,
- padding: EdgeInsets.symmetric(vertical: 24.h, horizontal: 16.w),
- width: ScreenUtil().screenWidth - 80.w,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(12.w)),
- color: "#FFFFFF".toColor(),
- ),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- /// 标题
- if (title != null)
- Text(
- title!,
- style: TextStyle(
- color: "#25262A".toColor(),
- fontSize: 15.sp,
- fontWeight: FontWeight.w500,
- decoration: TextDecoration.none,
- ),
- ),
- if (title != null) SizedBox(height: 12.h),
- if (content != null)
- /// 内容
- Text(
- content ?? "",
- textAlign: TextAlign.center,
- style: TextStyle(
- color: "#5F5F61".toColor(),
- fontWeight: FontWeight.w500,
- fontSize: 14.sp,
- decoration: TextDecoration.none,
- ),
- ),
- if (contentWidget != null) contentWidget!,
- SizedBox(height: 34.h),
- ///
- _buttonWidget(context),
- ],
- ),
- ),
- //
- const Spacer(),
- ],
- ),
- );
- }
- Widget _buttonWidget(BuildContext context) {
- if (cancelText == null) {
- return Row(
- children: [
- const Spacer(),
- ///确认按钮
- GestureDetector(
- onTap: () {
- _clickConfirm();
- },
- child: _button(text: confirmText ?? "", color: "#6177F2".toColor()),
- ),
- //
- const Spacer(),
- ],
- );
- } else {
- return Row(
- children: [
- /// 取消按钮
- Expanded(
- child: InkWell(
- onTap: () {
- _clickCancel();
- },
- child: _button(
- text: cancelText ?? "",
- color: "#F0F0F0".toColor(),
- textColor: "#5F5F61".toColor(),
- ),
- ),
- ),
- ///
- const SizedBox(width: 8),
- /// 确认按钮
- Expanded(
- child: InkWell(
- onTap: () {
- _clickConfirm();
- },
- child:
- _button(text: confirmText ?? "", color: "#6177F2".toColor()),
- ),
- ),
- ],
- );
- }
- }
- Widget _button({
- required String text,
- Color? color,
- BoxBorder? border,
- Color? textColor = Colors.white,
- }) {
- return Container(
- height: 36.w,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(8.w)),
- color: color,
- border: border,
- ),
- alignment: Alignment.center,
- child: Text(
- text,
- style: TextStyle(
- color: textColor,
- fontSize: 14.sp,
- fontWeight: FontWeight.w500,
- ),
- ),
- );
- }
- void _clickConfirm() {
- EAAlertDialog.dismiss();
- if (confirmOnTap != null) {
- confirmOnTap!();
- }
- }
- void _clickCancel() {
- EAAlertDialog.dismiss();
- if (cancelOnTap != null) {
- cancelOnTap!();
- }
- }
- }
|