import 'package:flutter/material.dart'; /// 通用确认对话框。 /// /// 用法: /// ```dart /// final ok = await ConfirmDialog.show( /// context, /// title: '删除确认', /// message: '确定要删除该记录吗?', /// confirmLabel: '删除', /// destructive: true, /// ); /// if (ok == true) { /* do it */ } /// ``` class ConfirmDialog extends StatelessWidget { const ConfirmDialog({ super.key, required this.title, required this.message, this.confirmLabel = '确定', this.cancelLabel = '取消', this.destructive = false, }); final String title; final String message; final String confirmLabel; final String cancelLabel; final bool destructive; static Future show( BuildContext context, { required String title, required String message, String confirmLabel = '确定', String cancelLabel = '取消', bool destructive = false, }) { return showDialog( context: context, builder: (_) => ConfirmDialog( title: title, message: message, confirmLabel: confirmLabel, cancelLabel: cancelLabel, destructive: destructive, ), ); } @override Widget build(BuildContext context) { final colorScheme = Theme.of(context).colorScheme; return AlertDialog( title: Text(title), content: Text(message), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(false), child: Text(cancelLabel), ), TextButton( style: destructive ? TextButton.styleFrom(foregroundColor: colorScheme.error) : null, onPressed: () => Navigator.of(context).pop(true), child: Text(confirmLabel), ), ], ); } }