1
This commit is contained in:
1 parent
8531403c33
commit
b03ece33a2
37 files changed
+6388
-908
No files matched your search
+160
-343
@@ -1,5 +1,9 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/models/file_item.dart';
|
||||
import 'package:code_editor/providers/editor_provider.dart';
|
||||
import 'package:code_editor/providers/project_provider.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
@@ -17,6 +21,24 @@ class FileItemWidget extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
static ProjectProvider _getProjectProvider(BuildContext context, {bool listen = false}) {
|
||||
try {
|
||||
return listen ? context.watch<ProjectProvider>() : context.read<ProjectProvider>();
|
||||
} catch (_) {
|
||||
final editor = listen ? context.watch<EditorProvider>() : context.read<EditorProvider>();
|
||||
return editor.projectProvider;
|
||||
}
|
||||
}
|
||||
|
||||
static TabProvider _getTabProvider(BuildContext context, {bool listen = false}) {
|
||||
try {
|
||||
return listen ? context.watch<TabProvider>() : context.read<TabProvider>();
|
||||
} catch (_) {
|
||||
final editor = listen ? context.watch<EditorProvider>() : context.read<EditorProvider>();
|
||||
return editor.tabProvider;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _buildFileItem(context, widget.fileItem);
|
||||
@@ -24,14 +46,15 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
|
||||
Widget _buildFileItem(BuildContext context, FileItem item) {
|
||||
final theme = Theme.of(context);
|
||||
final provider = context.watch<EditorProvider>();
|
||||
final projectProvider = _getProjectProvider(context, listen: true);
|
||||
final tabProvider = _getTabProvider(context, listen: true);
|
||||
final indent = EdgeInsets.only(left: item.depth * 16.0 + 8.0, right: 8.0);
|
||||
|
||||
// 判断该文件是否处于被剪切状态
|
||||
final isCut = provider.isItemCut(item.path);
|
||||
final isCut = projectProvider.isItemCut(item.path);
|
||||
|
||||
// 判断该文件是否为当前正在编辑/修改的文件
|
||||
final isSelected = !item.isDirectory && provider.isFileSelected(item.path);
|
||||
final isSelected = !item.isDirectory && tabProvider.isFileSelected(item.path);
|
||||
|
||||
// 文字样式:剪切时变灰,选中时高亮,其余正常
|
||||
final itemTextStyle = theme.textTheme.bodyMedium?.copyWith(
|
||||
@@ -72,7 +95,7 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
setState(() {
|
||||
item.isOpen = expanded;
|
||||
});
|
||||
context.read<EditorProvider>().toggleDirectory(item, expanded);
|
||||
_getProjectProvider(context).toggleDirectory(item, expanded);
|
||||
},
|
||||
children: item.children
|
||||
.map((child) => FileItemWidget(fileItem: child))
|
||||
@@ -86,33 +109,36 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
},
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 1.0),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
? theme.colorScheme.primaryContainer.withValues(alpha: 0.5)
|
||||
: Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
child: ListTile(
|
||||
contentPadding: indent,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: ListTile(
|
||||
contentPadding: indent,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
selected: isSelected,
|
||||
selectedTileColor: theme.colorScheme.primaryContainer.withValues(alpha: 0.5),
|
||||
leading: Icon(
|
||||
_getFileIcon(item),
|
||||
size: 20,
|
||||
color: iconColor,
|
||||
),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: itemTextStyle,
|
||||
),
|
||||
dense: true,
|
||||
onTap: () {
|
||||
final scaffold = Scaffold.maybeOf(context);
|
||||
if (scaffold != null && scaffold.isDrawerOpen) {
|
||||
scaffold.closeDrawer();
|
||||
}
|
||||
if (tabProvider.isFileSelected(item.path)) {
|
||||
return;
|
||||
}
|
||||
_getTabProvider(context).selectFile(item);
|
||||
},
|
||||
),
|
||||
selected: isSelected,
|
||||
selectedTileColor: Colors.transparent,
|
||||
leading: Icon(
|
||||
_getFileIcon(item),
|
||||
size: 20,
|
||||
color: iconColor,
|
||||
),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: itemTextStyle,
|
||||
),
|
||||
dense: true,
|
||||
onTap: () {
|
||||
Navigator.of(context).maybePop(); // 关闭 Drawer
|
||||
context.read<EditorProvider>().selectFile(item);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -125,107 +151,31 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
return Icons.commit_outlined;
|
||||
}
|
||||
|
||||
switch (item.extension) {
|
||||
case '.dart':
|
||||
return Icons.flutter_dash;
|
||||
case '.html':
|
||||
case '.htm':
|
||||
return Icons.html;
|
||||
case '.css':
|
||||
case '.scss':
|
||||
case '.sass':
|
||||
case '.less':
|
||||
return Icons.css;
|
||||
case '.js':
|
||||
case '.mjs':
|
||||
case '.cjs':
|
||||
return Icons.javascript;
|
||||
case '.ts':
|
||||
case '.tsx':
|
||||
case '.jsx':
|
||||
case '.vue':
|
||||
case '.svelte':
|
||||
return Icons.code;
|
||||
case '.json':
|
||||
return Icons.data_object;
|
||||
case '.yaml':
|
||||
case '.yml':
|
||||
case '.toml':
|
||||
case '.ini':
|
||||
case '.env':
|
||||
case '.conf':
|
||||
case '.config':
|
||||
case '.properties':
|
||||
return Icons.settings_suggest_outlined;
|
||||
case '.xml':
|
||||
return Icons.code;
|
||||
case '.md':
|
||||
case '.markdown':
|
||||
return Icons.article_outlined;
|
||||
case '.pdf':
|
||||
return Icons.picture_as_pdf_outlined;
|
||||
case '.py':
|
||||
case '.pyw':
|
||||
case '.java':
|
||||
case '.kt':
|
||||
case '.kts':
|
||||
case '.c':
|
||||
case '.cpp':
|
||||
case '.cc':
|
||||
case '.h':
|
||||
case '.hpp':
|
||||
case '.cs':
|
||||
case '.go':
|
||||
case '.rs':
|
||||
case '.swift':
|
||||
case '.rb':
|
||||
case '.php':
|
||||
return Icons.code;
|
||||
case '.sh':
|
||||
case '.bash':
|
||||
case '.zsh':
|
||||
case '.bat':
|
||||
case '.cmd':
|
||||
case '.ps1':
|
||||
return Icons.terminal;
|
||||
case '.sql':
|
||||
case '.db':
|
||||
case '.sqlite':
|
||||
return Icons.storage_outlined;
|
||||
case '.png':
|
||||
case '.jpg':
|
||||
case '.jpeg':
|
||||
case '.gif':
|
||||
case '.webp':
|
||||
case '.svg':
|
||||
case '.ico':
|
||||
case '.bmp':
|
||||
return Icons.image_outlined;
|
||||
case '.mp3':
|
||||
case '.wav':
|
||||
case '.ogg':
|
||||
case '.flac':
|
||||
case '.aac':
|
||||
return Icons.audio_file_outlined;
|
||||
case '.mp4':
|
||||
case '.avi':
|
||||
case '.mov':
|
||||
case '.mkv':
|
||||
case '.flv':
|
||||
case '.webm':
|
||||
return Icons.video_file_outlined;
|
||||
case '.zip':
|
||||
case '.rar':
|
||||
case '.7z':
|
||||
case '.tar':
|
||||
case '.gz':
|
||||
return Icons.folder_zip_outlined;
|
||||
case '.lock':
|
||||
return Icons.lock_outline;
|
||||
case '.txt':
|
||||
default:
|
||||
return Icons.description_outlined;
|
||||
}
|
||||
return switch (item.extension) {
|
||||
'.dart' => Icons.flutter_dash,
|
||||
'.html' || '.htm' => Icons.html,
|
||||
'.css' || '.scss' || '.sass' || '.less' => Icons.css,
|
||||
'.js' || '.mjs' || '.cjs' => Icons.javascript,
|
||||
'.ts' || '.tsx' || '.jsx' || '.vue' || '.svelte' => Icons.code,
|
||||
'.json' => Icons.data_object,
|
||||
'.yaml' || '.yml' || '.toml' || '.ini' || '.env' || '.conf' || '.config' || '.properties' =>
|
||||
Icons.settings_suggest_outlined,
|
||||
'.xml' => Icons.code,
|
||||
'.md' || '.markdown' => Icons.article_outlined,
|
||||
'.pdf' => Icons.picture_as_pdf_outlined,
|
||||
'.py' || '.pyw' || '.java' || '.kt' || '.kts' || '.c' || '.cpp' || '.cc' ||
|
||||
'.h' || '.hpp' || '.cs' || '.go' || '.rs' || '.swift' || '.rb' || '.php' =>
|
||||
Icons.code,
|
||||
'.sh' || '.bash' || '.zsh' || '.bat' || '.cmd' || '.ps1' => Icons.terminal,
|
||||
'.sql' || '.db' || '.sqlite' => Icons.storage_outlined,
|
||||
'.png' || '.jpg' || '.jpeg' || '.gif' || '.webp' || '.svg' || '.ico' || '.bmp' =>
|
||||
Icons.image_outlined,
|
||||
'.mp3' || '.wav' || '.ogg' || '.flac' || '.aac' => Icons.audio_file_outlined,
|
||||
'.mp4' || '.avi' || '.mov' || '.mkv' || '.flv' || '.webm' => Icons.video_file_outlined,
|
||||
'.zip' || '.rar' || '.7z' || '.tar' || '.gz' => Icons.folder_zip_outlined,
|
||||
'.lock' => Icons.lock_outline,
|
||||
_ => Icons.description_outlined,
|
||||
};
|
||||
}
|
||||
|
||||
void _showContextMenu(FileItem item, Offset tapPosition) async {
|
||||
@@ -236,17 +186,28 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
);
|
||||
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final onSurfaceColor = theme.colorScheme.onSurface;
|
||||
final canPaste = context.read<EditorProvider>().canPaste;
|
||||
final canPaste = _getProjectProvider(context).canPaste;
|
||||
final menuItems = <PopupMenuEntry<String>>[];
|
||||
|
||||
final strNewFile = l10n?.newFile ?? '新建文件';
|
||||
final strNewFolder = l10n?.newFolder ?? '新建文件夹';
|
||||
final strCut = l10n?.cut ?? '剪切';
|
||||
final strCopy = l10n?.copy ?? '复制';
|
||||
final strPaste = l10n?.paste ?? '粘贴';
|
||||
final strCopyPath = l10n?.copyPath ?? '复制路径';
|
||||
final strCopyRelativePath = l10n?.copyRelativePath ?? '复制相对路径';
|
||||
final strRename = l10n?.rename ?? '重命名';
|
||||
final strDelete = l10n?.delete ?? '删除';
|
||||
|
||||
if (item.isDirectory) {
|
||||
menuItems.addAll([
|
||||
_buildMenuItem('new_file', Icons.note_add_outlined, '新建文件', onSurfaceColor),
|
||||
_buildMenuItem('new_folder', Icons.create_new_folder_outlined, '新建文件夹', onSurfaceColor),
|
||||
_buildMenuItem('new_file', Icons.note_add_outlined, strNewFile, onSurfaceColor),
|
||||
_buildMenuItem('new_folder', Icons.create_new_folder_outlined, strNewFolder, onSurfaceColor),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('cut', Icons.content_cut, '剪切', onSurfaceColor),
|
||||
_buildMenuItem('copy', Icons.copy, '复制', onSurfaceColor),
|
||||
_buildMenuItem('cut', Icons.content_cut, strCut, onSurfaceColor),
|
||||
_buildMenuItem('copy', Icons.copy, strCopy, onSurfaceColor),
|
||||
PopupMenuItem<String>(
|
||||
value: 'paste',
|
||||
enabled: canPaste,
|
||||
@@ -261,7 +222,7 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'粘贴',
|
||||
strPaste,
|
||||
style: TextStyle(
|
||||
color: canPaste
|
||||
? onSurfaceColor
|
||||
@@ -272,31 +233,31 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
),
|
||||
),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('copy_path', Icons.link, '复制路径', onSurfaceColor),
|
||||
_buildMenuItem('copy_relative_path', Icons.short_text, '复制相对路径', onSurfaceColor),
|
||||
_buildMenuItem('copy_path', Icons.link, strCopyPath, onSurfaceColor),
|
||||
_buildMenuItem('copy_relative_path', Icons.short_text, strCopyRelativePath, onSurfaceColor),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('rename', Icons.edit_outlined, '重命名', onSurfaceColor),
|
||||
_buildMenuItem('rename', Icons.edit_outlined, strRename, onSurfaceColor),
|
||||
_buildMenuItem(
|
||||
'delete',
|
||||
Icons.delete_outline,
|
||||
'删除',
|
||||
strDelete,
|
||||
theme.colorScheme.error,
|
||||
isDestructive: true,
|
||||
),
|
||||
]);
|
||||
} else {
|
||||
menuItems.addAll([
|
||||
_buildMenuItem('cut', Icons.content_cut, '剪切', onSurfaceColor),
|
||||
_buildMenuItem('copy', Icons.copy, '复制', onSurfaceColor),
|
||||
_buildMenuItem('cut', Icons.content_cut, strCut, onSurfaceColor),
|
||||
_buildMenuItem('copy', Icons.copy, strCopy, onSurfaceColor),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('copy_path', Icons.link, '复制路径', onSurfaceColor),
|
||||
_buildMenuItem('copy_relative_path', Icons.short_text, '复制相对路径', onSurfaceColor),
|
||||
_buildMenuItem('copy_path', Icons.link, strCopyPath, onSurfaceColor),
|
||||
_buildMenuItem('copy_relative_path', Icons.short_text, strCopyRelativePath, onSurfaceColor),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('rename', Icons.edit_outlined, '重命名', onSurfaceColor),
|
||||
_buildMenuItem('rename', Icons.edit_outlined, strRename, onSurfaceColor),
|
||||
_buildMenuItem(
|
||||
'delete',
|
||||
Icons.delete_outline,
|
||||
'删除',
|
||||
strDelete,
|
||||
theme.colorScheme.error,
|
||||
isDestructive: true,
|
||||
),
|
||||
@@ -350,247 +311,103 @@ class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
}
|
||||
|
||||
void _handleMenuAction(FileItem item, String action) async {
|
||||
final provider = context.read<EditorProvider>();
|
||||
final provider = _getProjectProvider(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
|
||||
switch (action) {
|
||||
case 'new_file':
|
||||
await _showCreateInputDialog(
|
||||
title: '新建文件',
|
||||
hintText: '文件名 (例如: main.dart)',
|
||||
onConfirm: (name) async {
|
||||
await provider.createFile(item.path, name);
|
||||
},
|
||||
final name = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
title: l10n?.newFile ?? '新建文件',
|
||||
hintText: l10n?.fileNameHint ?? '文件名 (例如: main.dart)',
|
||||
);
|
||||
if (name != null && name.trim().isNotEmpty) {
|
||||
try {
|
||||
await provider.createFile(item.path, name.trim());
|
||||
} catch (e) {
|
||||
if (mounted) DialogUtils.showErrorToast(context, l10n?.createFileFailed(e.toString()) ?? '创建文件失败: $e');
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'new_folder':
|
||||
await _showCreateInputDialog(
|
||||
title: '新建文件夹',
|
||||
hintText: '文件夹名',
|
||||
onConfirm: (name) async {
|
||||
await provider.createDirectory(item.path, name);
|
||||
},
|
||||
final name = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
title: l10n?.newFolder ?? '新建文件夹',
|
||||
hintText: l10n?.folderNameHint ?? '文件夹名',
|
||||
);
|
||||
if (name != null && name.trim().isNotEmpty) {
|
||||
try {
|
||||
await provider.createDirectory(item.path, name.trim());
|
||||
} catch (e) {
|
||||
if (mounted) DialogUtils.showErrorToast(context, l10n?.createFolderFailed(e.toString()) ?? '创建文件夹失败: $e');
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'cut':
|
||||
provider.cut(item);
|
||||
if (mounted) _showToast(context, '已剪切: ${item.name}');
|
||||
if (mounted) DialogUtils.showInfoToast(context, l10n?.cutItem(item.name) ?? '已剪切: ${item.name}');
|
||||
break;
|
||||
|
||||
case 'copy':
|
||||
provider.copy(item);
|
||||
if (mounted) _showToast(context, '已复制: ${item.name}');
|
||||
if (mounted) DialogUtils.showInfoToast(context, l10n?.copiedItem(item.name) ?? '已复制: ${item.name}');
|
||||
break;
|
||||
|
||||
case 'paste':
|
||||
try {
|
||||
await provider.paste(item);
|
||||
if (mounted) _showToast(context, '操作成功');
|
||||
if (mounted) DialogUtils.showSuccessToast(context, l10n?.operationSuccess ?? '操作成功');
|
||||
} catch (e) {
|
||||
if (mounted) _showToast(context, '操作失败: $e', isError: true);
|
||||
if (mounted) DialogUtils.showErrorToast(context, l10n?.operationFailed(e.toString()) ?? '操作失败: $e');
|
||||
}
|
||||
break;
|
||||
|
||||
case 'copy_path':
|
||||
await Clipboard.setData(ClipboardData(text: item.fullPath));
|
||||
if (mounted) {
|
||||
_showToast(context, '已复制完整路径到剪贴板');
|
||||
DialogUtils.showSuccessToast(context, l10n?.copiedPathToClipboard ?? '已复制完整路径到剪贴板');
|
||||
}
|
||||
break;
|
||||
|
||||
case 'copy_relative_path':
|
||||
await Clipboard.setData(ClipboardData(text: item.getRelativePath(provider.rootPath)));
|
||||
if (mounted) {
|
||||
_showToast(context, '已复制相对路径到剪贴板');
|
||||
DialogUtils.showSuccessToast(context, l10n?.copiedRelativePathToClipboard ?? '已复制相对路径到剪贴板');
|
||||
}
|
||||
break;
|
||||
|
||||
case 'rename':
|
||||
await _showCreateInputDialog(
|
||||
title: '重命名',
|
||||
hintText: '新名称',
|
||||
final newName = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
title: l10n?.rename ?? '重命名',
|
||||
hintText: l10n?.newName ?? '新名称',
|
||||
initialValue: item.name,
|
||||
onConfirm: (newName) async {
|
||||
if (newName.trim() == item.name) return;
|
||||
await provider.rename(item, newName.trim());
|
||||
},
|
||||
);
|
||||
if (newName != null && newName.trim().isNotEmpty && newName.trim() != item.name) {
|
||||
try {
|
||||
await provider.rename(item, newName.trim());
|
||||
} catch (e) {
|
||||
if (mounted) DialogUtils.showErrorToast(context, l10n?.renameFailed(e.toString()) ?? '重命名失败: $e');
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
final confirmed = await _showConfirmDialog(
|
||||
title: '确认删除',
|
||||
content: '确定要删除 "${item.name}" 吗?此操作不可恢复。',
|
||||
final confirmed = await DialogUtils.showDestructiveConfirmDialog(
|
||||
context,
|
||||
title: l10n?.confirmDelete ?? '确认删除',
|
||||
message: l10n?.confirmDeleteMessage(item.name) ?? '确定要删除 "${item.name}" 吗?此操作不可恢复。',
|
||||
);
|
||||
if (confirmed == true && mounted) {
|
||||
await provider.delete(item);
|
||||
if (confirmed && mounted) {
|
||||
try {
|
||||
await provider.delete(item);
|
||||
} catch (e) {
|
||||
if (mounted) DialogUtils.showErrorToast(context, l10n?.deleteFailed(e.toString()) ?? '删除失败: $e');
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _showToast(BuildContext context, String message, {bool isError = false}) {
|
||||
final overlay = Overlay.of(context, rootOverlay: true);
|
||||
final theme = Theme.of(context);
|
||||
|
||||
late OverlayEntry entry;
|
||||
entry = OverlayEntry(
|
||||
builder: (ctx) => Positioned(
|
||||
bottom: 48,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: Center(
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: isError
|
||||
? theme.colorScheme.errorContainer
|
||||
: theme.colorScheme.inverseSurface,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: isError
|
||||
? theme.colorScheme.error
|
||||
: theme.colorScheme.outlineVariant,
|
||||
width: 1,
|
||||
),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Colors.black26,
|
||||
blurRadius: 10,
|
||||
offset: Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Text(
|
||||
message,
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
color: isError
|
||||
? theme.colorScheme.onErrorContainer
|
||||
: theme.colorScheme.onInverseSurface,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
overlay.insert(entry);
|
||||
Future.delayed(const Duration(seconds: 2), () {
|
||||
if (entry.mounted) {
|
||||
entry.remove();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _showCreateInputDialog({
|
||||
required String title,
|
||||
required String hintText,
|
||||
String? initialValue,
|
||||
required Future<void> Function(String value) onConfirm,
|
||||
}) async {
|
||||
final controller = TextEditingController(text: initialValue);
|
||||
final formKey = GlobalKey<FormState>();
|
||||
|
||||
final theme = Theme.of(context);
|
||||
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
title: Text(title),
|
||||
content: Form(
|
||||
key: formKey,
|
||||
child: TextFormField(
|
||||
controller: controller,
|
||||
autofocus: true,
|
||||
decoration: InputDecoration(
|
||||
hintText: hintText,
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
validator: (val) {
|
||||
if (val == null || val.trim().isEmpty) {
|
||||
return '名称不能为空';
|
||||
}
|
||||
if (val.contains(RegExp(r'[\\/:*?"<>|]'))) {
|
||||
return '名称不能包含非法字符 (\\/:*?"<>|)';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () async {
|
||||
if (formKey.currentState?.validate() ?? false) {
|
||||
final inputVal = controller.text;
|
||||
Navigator.of(dialogContext).pop();
|
||||
try {
|
||||
await onConfirm(inputVal);
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
_showToast(context, '操作失败: $e', isError: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text('确定'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool?> _showConfirmDialog({
|
||||
required String title,
|
||||
required String content,
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
return showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
title: Text(title),
|
||||
content: Text(content),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
FilledButton(
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: theme.colorScheme.error,
|
||||
foregroundColor: theme.colorScheme.onError,
|
||||
),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
child: const Text('删除'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user