优化
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 通用带输入框的确认弹窗组件
|
||||
/// [title]:弹窗标题
|
||||
/// [hintText]:输入框提示文字
|
||||
/// [labelText]:输入框标签文字
|
||||
/// [confirmText]:确认按钮文字(默认"保存")
|
||||
/// [cancelText]:取消按钮文字(默认"取消")
|
||||
/// [onConfirm]:确认按钮回调,参数为输入的文本,返回bool表示是否关闭弹窗
|
||||
/// [inputValidator]:输入内容校验器,返回null表示校验通过,返回字符串表示错误提示
|
||||
Future<void> showInputConfirmDialog({
|
||||
required BuildContext context,
|
||||
required String title,
|
||||
required String hintText,
|
||||
String labelText = '',
|
||||
String confirmText = '保存',
|
||||
String cancelText = '取消',
|
||||
required Function(String inputText) onConfirm,
|
||||
String? Function(String inputText)? inputValidator,
|
||||
}) async {
|
||||
// 输入框控制器
|
||||
final TextEditingController _controller = TextEditingController();
|
||||
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(title),
|
||||
content: TextField(
|
||||
controller: _controller,
|
||||
decoration: InputDecoration(
|
||||
hintText: hintText,
|
||||
border: const OutlineInputBorder(),
|
||||
labelText: labelText,
|
||||
),
|
||||
autofocus: true, // 自动聚焦输入框
|
||||
),
|
||||
actions: [
|
||||
// 取消按钮
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Text(cancelText),
|
||||
),
|
||||
// 确认按钮
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
// 获取输入文本并去空格
|
||||
final String inputText = _controller.text.trim();
|
||||
|
||||
// 1. 执行自定义校验
|
||||
if (inputValidator != null) {
|
||||
final String? errorMsg = inputValidator(inputText);
|
||||
if (errorMsg != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(errorMsg),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 执行确认回调
|
||||
onConfirm(inputText);
|
||||
|
||||
// 3. 关闭弹窗
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Text(confirmText),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user