1
This commit is contained in:
1 parent
2379b4476f
commit
8531403c33
21 files changed
+2498
-354
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
final String? filePath;
|
||||
final VoidCallback? onUndo;
|
||||
final VoidCallback? onRedo;
|
||||
final VoidCallback? onSave;
|
||||
final VoidCallback? onRun;
|
||||
final VoidCallback? onSettings;
|
||||
|
||||
const CodeEditorAppBar({
|
||||
super.key,
|
||||
required this.filePath,
|
||||
this.onUndo,
|
||||
this.onRedo,
|
||||
this.onSave,
|
||||
this.onRun,
|
||||
this.onSettings,
|
||||
});
|
||||
|
||||
@override
|
||||
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final path = filePath;
|
||||
|
||||
return AppBar(
|
||||
backgroundColor: theme.colorScheme.primaryContainer,
|
||||
foregroundColor: theme.colorScheme.onPrimaryContainer,
|
||||
title: Text(path != null && path.isNotEmpty ? p.basename(path) : ''),
|
||||
actions: [
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.undo),
|
||||
tooltip: l10n.undo,
|
||||
onPressed: onUndo ?? () {},
|
||||
),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.redo),
|
||||
tooltip: l10n.redo,
|
||||
onPressed: onRedo ?? () {},
|
||||
),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.save_outlined),
|
||||
tooltip: l10n.save,
|
||||
onPressed: onSave ?? () {},
|
||||
),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.play_arrow),
|
||||
tooltip: l10n.run,
|
||||
onPressed: onRun ?? () {},
|
||||
),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.settings),
|
||||
tooltip: l10n.settings,
|
||||
onPressed: onSettings ?? () {},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/providers/editor_provider.dart';
|
||||
import 'package:code_editor/widgets/file_directory_history_widget.dart';
|
||||
import 'package:code_editor/widgets/file_tree_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class CodeEditorDrawer extends StatelessWidget {
|
||||
const CodeEditorDrawer({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final rootPath = context.select<EditorProvider, String?>((p) => p.rootPath);
|
||||
|
||||
return Drawer(
|
||||
child: SafeArea(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.primaryContainer,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
l10n.fileDirectory,
|
||||
style: theme.textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
tooltip: '刷新目录',
|
||||
onPressed: () => context.read<EditorProvider>().refreshTree(),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.folder_open),
|
||||
tooltip: l10n.openFileDirectory,
|
||||
onPressed: () => context.read<EditorProvider>().openDirectory(),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.history),
|
||||
tooltip: l10n.viewFileDirectoryHistory,
|
||||
onPressed: () {
|
||||
FileDirectoryHistoryWidget.show(
|
||||
context,
|
||||
onSelectHistory: (selectedHistory) {
|
||||
context.read<EditorProvider>().switchProject(selectedHistory);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
Text(
|
||||
rootPath ?? '',
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onPrimaryContainer.withValues(alpha: 0.8),
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Expanded(
|
||||
child: FileTreeWidget(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -49,37 +49,79 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if(_isLoading) {
|
||||
final hasNoProject = widget.rootPath == null || widget.rootPath!.trim().isEmpty;
|
||||
final hasNoFile = widget.filePath == null || widget.filePath!.trim().isEmpty;
|
||||
|
||||
if (hasNoProject && hasNoFile) {
|
||||
return Center(
|
||||
child: Text(
|
||||
"当前未打开文件目录",
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (_isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
return CodeTheme(
|
||||
data: CodeThemeData(styles: atomOneDarkTheme),
|
||||
child: CodeField(
|
||||
controller: _controller,
|
||||
gutterStyle: const GutterStyle(
|
||||
showLineNumbers: true,
|
||||
showFoldingHandles: true,
|
||||
if (_errorMessage != null) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Text(
|
||||
_errorMessage!,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (hasNoFile) {
|
||||
return Center(
|
||||
child: Text(
|
||||
"当前未打开文件",
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// atomOneDark 默认背景色为 0xFF282C34
|
||||
const editorBgColor = Color(0xFF282C34);
|
||||
|
||||
return Container(
|
||||
color: editorBgColor,
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
child: CodeTheme(
|
||||
data: CodeThemeData(styles: atomOneDarkTheme),
|
||||
child: SingleChildScrollView(
|
||||
child: CodeField(
|
||||
controller: _controller,
|
||||
gutterStyle: const GutterStyle(
|
||||
showLineNumbers: true,
|
||||
showFoldingHandles: true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _loadFileContent() async {
|
||||
String? fullFilePath;
|
||||
final filePath = widget.filePath;
|
||||
final rootPath = widget.rootPath;
|
||||
final filePath = widget.filePath;
|
||||
final hasNoProject = rootPath == null || rootPath.trim().isEmpty;
|
||||
final hasNoFile = filePath == null || filePath.trim().isEmpty;
|
||||
|
||||
if (filePath != null && filePath.isNotEmpty) {
|
||||
if (rootPath != null && rootPath.isNotEmpty && !p.isAbsolute(filePath)) {
|
||||
fullFilePath = p.join(rootPath, filePath);
|
||||
} else {
|
||||
fullFilePath = filePath;
|
||||
}
|
||||
}
|
||||
|
||||
if(fullFilePath == null) {
|
||||
if (hasNoFile) {
|
||||
_controller.text = '';
|
||||
if(mounted) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
_errorMessage = null;
|
||||
@@ -88,6 +130,10 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
return;
|
||||
}
|
||||
|
||||
final String fullFilePath = (hasNoProject || p.isAbsolute(filePath))
|
||||
? filePath
|
||||
: p.join(rootPath, filePath);
|
||||
|
||||
final int requestVersion = ++_currentLoadVersion;
|
||||
|
||||
setState(() {
|
||||
@@ -106,7 +152,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
}catch(e) {
|
||||
if(!mounted || requestVersion != _currentLoadVersion) return;
|
||||
setState(() {
|
||||
_errorMessage = '读取文件失败: $e';
|
||||
_errorMessage = '$e';
|
||||
});
|
||||
}finally {
|
||||
if(mounted && requestVersion == _currentLoadVersion) {
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
import 'package:code_editor/models/file_directory_history.dart';
|
||||
import 'package:code_editor/services/file_directory_history_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
class FileDirectoryHistoryWidget extends StatefulWidget {
|
||||
final ValueChanged<FileDirectoryHistory>? onSelectHistory;
|
||||
|
||||
const FileDirectoryHistoryWidget({
|
||||
super.key,
|
||||
this.onSelectHistory,
|
||||
});
|
||||
|
||||
/// 打开历史弹窗(不关闭后方的 Drawer)
|
||||
static Future<void> show(
|
||||
BuildContext context, {
|
||||
ValueChanged<FileDirectoryHistory>? onSelectHistory,
|
||||
}) {
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: true,
|
||||
builder: (BuildContext dialogContext) {
|
||||
return FileDirectoryHistoryWidget(
|
||||
onSelectHistory: onSelectHistory,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
State<FileDirectoryHistoryWidget> createState() =>
|
||||
_FileDirectoryHistoryWidgetState();
|
||||
}
|
||||
|
||||
class _FileDirectoryHistoryWidgetState
|
||||
extends State<FileDirectoryHistoryWidget> {
|
||||
List<FileDirectoryHistory> _historyList = [];
|
||||
bool _isLoading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadHistory();
|
||||
}
|
||||
|
||||
Future<void> _loadHistory() async {
|
||||
final list = await FileDirectoryHistoryService.instance.getFullHistory();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_historyList = list;
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _deleteHistory(FileDirectoryHistory item) async {
|
||||
final root = item.rootPath;
|
||||
if (root != null && root.isNotEmpty) {
|
||||
await FileDirectoryHistoryService.instance.removeHistory(root);
|
||||
await _loadHistory();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return Dialog(
|
||||
clipBehavior: Clip.antiAlias,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
insetPadding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 32.0),
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(
|
||||
maxWidth: 480,
|
||||
maxHeight: 520,
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// 1. Title 区域:加粗、与 AppBar 相同背景颜色、右上角 X 关闭
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
|
||||
color: theme.colorScheme.primaryContainer,
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
'历史文件目录',
|
||||
style: theme.textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: theme.colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.close,
|
||||
color: theme.colorScheme.onPrimaryContainer,
|
||||
),
|
||||
tooltip: '关闭',
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// 2. 列表区域
|
||||
Flexible(
|
||||
child: _isLoading
|
||||
? const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(32.0),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
)
|
||||
: _historyList.isEmpty
|
||||
? Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32.0),
|
||||
child: Text(
|
||||
'暂无历史记录',
|
||||
style: TextStyle(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
: ListView.separated(
|
||||
shrinkWrap: true,
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
itemCount: _historyList.length,
|
||||
separatorBuilder: (context, index) => const Divider(
|
||||
height: 1,
|
||||
indent: 16,
|
||||
endIndent: 16,
|
||||
),
|
||||
itemBuilder: (context, index) {
|
||||
final item = _historyList[index];
|
||||
final lastFile = item.lastOpenedFilePath;
|
||||
final fileName = (lastFile != null &&
|
||||
lastFile.trim().isNotEmpty)
|
||||
? p.basename(lastFile)
|
||||
: '未打开文件';
|
||||
final rootDir = item.rootPath ?? '未知目录';
|
||||
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
widget.onSelectHistory?.call(item);
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16.0,
|
||||
vertical: 12.0,
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// 左边:历史图标
|
||||
Icon(
|
||||
Icons.history,
|
||||
size: 24,
|
||||
color: theme.colorScheme.primary,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
|
||||
// 中间:撑开,占据最大空间,两行字
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment:
|
||||
CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 上面:大的上一次打开的文件名称
|
||||
Text(
|
||||
fileName,
|
||||
style: theme.textTheme.titleSmall?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
// 下面:一行小字表示根目录
|
||||
Text(
|
||||
rootDir,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
|
||||
// 右边:删除历史记录的图标,点击后删除
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.delete_outline,
|
||||
size: 20,
|
||||
color: theme.colorScheme.error,
|
||||
),
|
||||
tooltip: '删除此记录',
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: () => _deleteHistory(item),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,147 +1,596 @@
|
||||
import 'package:code_editor/models/file_item.dart';
|
||||
import 'package:code_editor/providers/editor_provider.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class FileItemWidget extends StatefulWidget {
|
||||
final FileItem fileItem;
|
||||
|
||||
const FileItemWidget({
|
||||
super.key,
|
||||
required this.fileItem
|
||||
super.key,
|
||||
required this.fileItem,
|
||||
});
|
||||
|
||||
@override
|
||||
State<FileItemWidget> createState() => _FileItemWidget();
|
||||
State<FileItemWidget> createState() => _FileItemWidgetState();
|
||||
}
|
||||
|
||||
class _FileItemWidget extends State<FileItemWidget> {
|
||||
class _FileItemWidgetState extends State<FileItemWidget> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _buildFileItem(context, widget.fileItem);
|
||||
}
|
||||
|
||||
Widget _buildFileItem(BuildContext context, FileItem item) {
|
||||
final theme = Theme.of(context);
|
||||
final provider = context.watch<EditorProvider>();
|
||||
final indent = EdgeInsets.only(left: item.depth * 16.0 + 8.0, right: 8.0);
|
||||
if(item.isDirectory) {
|
||||
if(item.isOpen) {
|
||||
return GestureDetector(
|
||||
onLongPressStart: (details) {
|
||||
_showContextMenu(context, item, details.globalPosition);
|
||||
},
|
||||
child: ExpansionTile(
|
||||
initiallyExpanded: item.isOpen,
|
||||
onExpansionChanged: (bool expanded) {
|
||||
|
||||
// 判断该文件是否处于被剪切状态
|
||||
final isCut = provider.isItemCut(item.path);
|
||||
|
||||
// 判断该文件是否为当前正在编辑/修改的文件
|
||||
final isSelected = !item.isDirectory && provider.isFileSelected(item.path);
|
||||
|
||||
// 文字样式:剪切时变灰,选中时高亮,其余正常
|
||||
final itemTextStyle = theme.textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
|
||||
color: isCut
|
||||
? theme.colorScheme.onSurface.withValues(alpha: 0.38)
|
||||
: isSelected
|
||||
? theme.colorScheme.primary
|
||||
: null,
|
||||
);
|
||||
|
||||
final iconColor = isCut
|
||||
? theme.colorScheme.onSurface.withValues(alpha: 0.38)
|
||||
: isSelected
|
||||
? theme.colorScheme.primary
|
||||
: null;
|
||||
|
||||
if (item.isDirectory) {
|
||||
return GestureDetector(
|
||||
onLongPressStart: (details) {
|
||||
_showContextMenu(item, details.globalPosition);
|
||||
},
|
||||
child: ExpansionTile(
|
||||
key: PageStorageKey(item.path),
|
||||
initiallyExpanded: item.isOpen,
|
||||
tilePadding: indent,
|
||||
leading: Icon(
|
||||
item.isOpen ? Icons.folder_open_outlined : Icons.folder_outlined,
|
||||
color: isCut ? iconColor : null,
|
||||
size: 20,
|
||||
),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: itemTextStyle,
|
||||
),
|
||||
dense: true,
|
||||
onExpansionChanged: (bool expanded) {
|
||||
setState(() {
|
||||
item.isOpen = expanded;
|
||||
},
|
||||
tilePadding: indent,
|
||||
leading: const Icon(Icons.folder_outlined, color: Colors.amber, size: 20),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
|
||||
),
|
||||
dense: true,
|
||||
children: item.children.map((child) => _buildFileItem(context, child)).toList()
|
||||
)
|
||||
);
|
||||
}else {
|
||||
return GestureDetector(
|
||||
onLongPressStart: (details) {
|
||||
_showContextMenu(context, item, details.globalPosition);
|
||||
});
|
||||
context.read<EditorProvider>().toggleDirectory(item, expanded);
|
||||
},
|
||||
children: item.children
|
||||
.map((child) => FileItemWidget(fileItem: child))
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return GestureDetector(
|
||||
onLongPressStart: (details) {
|
||||
_showContextMenu(item, details.globalPosition);
|
||||
},
|
||||
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,
|
||||
leading: Icon(Icons.file_copy),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
selected: isSelected,
|
||||
selectedTileColor: Colors.transparent,
|
||||
leading: Icon(
|
||||
_getFileIcon(item),
|
||||
size: 20,
|
||||
color: iconColor,
|
||||
),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: const TextStyle(fontSize: 13),
|
||||
style: itemTextStyle,
|
||||
),
|
||||
dense: true,
|
||||
onTap: () {
|
||||
_openFile(item);
|
||||
Navigator.of(context).maybePop(); // 关闭 Drawer
|
||||
context.read<EditorProvider>().selectFile(item);
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
}else {
|
||||
return GestureDetector(
|
||||
onLongPressStart: (details) {
|
||||
_showContextMenu(context, item, details.globalPosition);
|
||||
},
|
||||
child: ListTile(
|
||||
contentPadding: indent,
|
||||
leading: Icon(Icons.file_copy),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: const TextStyle(fontSize: 13),
|
||||
),
|
||||
dense: true,
|
||||
onTap: () {
|
||||
_openFile(item);
|
||||
},
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//打开文件
|
||||
void _openFile(FileItem item) {
|
||||
IconData _getFileIcon(FileItem item) {
|
||||
final lowerName = item.name.toLowerCase();
|
||||
if (lowerName == '.gitignore' || lowerName == '.gitattributes' || lowerName == '.gitmodules') {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void _showContextMenu(BuildContext context, FileItem item, Offset tapPosition) async {
|
||||
void _showContextMenu(FileItem item, Offset tapPosition) async {
|
||||
final overlay = Overlay.of(context).context.findRenderObject() as RenderBox;
|
||||
final position = RelativeRect.fromRect(
|
||||
Rect.fromLTWH(tapPosition.dx, tapPosition.dy, 0, 0),
|
||||
Offset.zero & overlay.size,
|
||||
);
|
||||
// 弹出 Material 原生菜单
|
||||
|
||||
final theme = Theme.of(context);
|
||||
final onSurfaceColor = theme.colorScheme.onSurface;
|
||||
final canPaste = context.read<EditorProvider>().canPaste;
|
||||
final menuItems = <PopupMenuEntry<String>>[];
|
||||
|
||||
if (item.isDirectory) {
|
||||
menuItems.addAll([
|
||||
_buildMenuItem('new_file', Icons.note_add_outlined, '新建文件', onSurfaceColor),
|
||||
_buildMenuItem('new_folder', Icons.create_new_folder_outlined, '新建文件夹', onSurfaceColor),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('cut', Icons.content_cut, '剪切', onSurfaceColor),
|
||||
_buildMenuItem('copy', Icons.copy, '复制', onSurfaceColor),
|
||||
PopupMenuItem<String>(
|
||||
value: 'paste',
|
||||
enabled: canPaste,
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.paste,
|
||||
size: 18,
|
||||
color: canPaste
|
||||
? onSurfaceColor
|
||||
: onSurfaceColor.withValues(alpha: 0.38),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'粘贴',
|
||||
style: TextStyle(
|
||||
color: canPaste
|
||||
? onSurfaceColor
|
||||
: onSurfaceColor.withValues(alpha: 0.38),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('copy_path', Icons.link, '复制路径', onSurfaceColor),
|
||||
_buildMenuItem('copy_relative_path', Icons.short_text, '复制相对路径', onSurfaceColor),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('rename', Icons.edit_outlined, '重命名', onSurfaceColor),
|
||||
_buildMenuItem(
|
||||
'delete',
|
||||
Icons.delete_outline,
|
||||
'删除',
|
||||
theme.colorScheme.error,
|
||||
isDestructive: true,
|
||||
),
|
||||
]);
|
||||
} else {
|
||||
menuItems.addAll([
|
||||
_buildMenuItem('cut', Icons.content_cut, '剪切', onSurfaceColor),
|
||||
_buildMenuItem('copy', Icons.copy, '复制', onSurfaceColor),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('copy_path', Icons.link, '复制路径', onSurfaceColor),
|
||||
_buildMenuItem('copy_relative_path', Icons.short_text, '复制相对路径', onSurfaceColor),
|
||||
const PopupMenuDivider(),
|
||||
_buildMenuItem('rename', Icons.edit_outlined, '重命名', onSurfaceColor),
|
||||
_buildMenuItem(
|
||||
'delete',
|
||||
Icons.delete_outline,
|
||||
'删除',
|
||||
theme.colorScheme.error,
|
||||
isDestructive: true,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
final selectedAction = await showMenu<String>(
|
||||
context: context,
|
||||
position: position,
|
||||
color: const Color(0xFF252526),
|
||||
elevation: 8,
|
||||
items: const [
|
||||
PopupMenuItem(
|
||||
value: 'new_file',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.note_add_outlined, size: 18, color: Colors.blueAccent),
|
||||
SizedBox(width: 8),
|
||||
Text('新建文件'),
|
||||
],
|
||||
),
|
||||
color: theme.colorScheme.surface,
|
||||
surfaceTintColor: theme.colorScheme.surfaceTint,
|
||||
elevation: 6,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'rename',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.edit_outlined, size: 18, color: Colors.orangeAccent),
|
||||
SizedBox(width: 8),
|
||||
Text('重命名'),
|
||||
],
|
||||
),
|
||||
),
|
||||
PopupMenuDivider(), // 分割线
|
||||
PopupMenuItem(
|
||||
value: 'delete',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.delete_outline, size: 18, color: Colors.redAccent),
|
||||
SizedBox(width: 8),
|
||||
Text('删除', style: TextStyle(color: Colors.redAccent)),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
items: menuItems,
|
||||
);
|
||||
// 用户点击了某一项后的处理逻辑
|
||||
if(selectedAction != null) {
|
||||
_handleMenuAction(context, selectedAction);
|
||||
|
||||
if (selectedAction != null && mounted) {
|
||||
_handleMenuAction(item, selectedAction);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleMenuAction(BuildContext context, String action) {
|
||||
PopupMenuItem<String> _buildMenuItem(
|
||||
String value,
|
||||
IconData icon,
|
||||
String title,
|
||||
Color iconColor, {
|
||||
bool isDestructive = false,
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
return PopupMenuItem<String>(
|
||||
value: value,
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, size: 18, color: iconColor),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: isDestructive
|
||||
? TextStyle(color: theme.colorScheme.error)
|
||||
: TextStyle(color: theme.colorScheme.onSurface),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _handleMenuAction(FileItem item, String action) async {
|
||||
final provider = context.read<EditorProvider>();
|
||||
switch (action) {
|
||||
case 'new_file':
|
||||
await _showCreateInputDialog(
|
||||
title: '新建文件',
|
||||
hintText: '文件名 (例如: main.dart)',
|
||||
onConfirm: (name) async {
|
||||
await provider.createFile(item.path, name);
|
||||
},
|
||||
);
|
||||
break;
|
||||
|
||||
case 'new_folder':
|
||||
await _showCreateInputDialog(
|
||||
title: '新建文件夹',
|
||||
hintText: '文件夹名',
|
||||
onConfirm: (name) async {
|
||||
await provider.createDirectory(item.path, name);
|
||||
},
|
||||
);
|
||||
break;
|
||||
|
||||
case 'cut':
|
||||
provider.cut(item);
|
||||
if (mounted) _showToast(context, '已剪切: ${item.name}');
|
||||
break;
|
||||
|
||||
case 'copy':
|
||||
provider.copy(item);
|
||||
if (mounted) _showToast(context, '已复制: ${item.name}');
|
||||
break;
|
||||
|
||||
case 'paste':
|
||||
try {
|
||||
await provider.paste(item);
|
||||
if (mounted) _showToast(context, '操作成功');
|
||||
} catch (e) {
|
||||
if (mounted) _showToast(context, '操作失败: $e', isError: true);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'copy_path':
|
||||
await Clipboard.setData(ClipboardData(text: item.fullPath));
|
||||
if (mounted) {
|
||||
_showToast(context, '已复制完整路径到剪贴板');
|
||||
}
|
||||
break;
|
||||
|
||||
case 'copy_relative_path':
|
||||
await Clipboard.setData(ClipboardData(text: item.getRelativePath(provider.rootPath)));
|
||||
if (mounted) {
|
||||
_showToast(context, '已复制相对路径到剪贴板');
|
||||
}
|
||||
break;
|
||||
|
||||
case 'rename':
|
||||
await _showCreateInputDialog(
|
||||
title: '重命名',
|
||||
hintText: '新名称',
|
||||
initialValue: item.name,
|
||||
onConfirm: (newName) async {
|
||||
if (newName.trim() == item.name) return;
|
||||
await provider.rename(item, newName.trim());
|
||||
},
|
||||
);
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
final confirmed = await _showConfirmDialog(
|
||||
title: '确认删除',
|
||||
content: '确定要删除 "${item.name}" 吗?此操作不可恢复。',
|
||||
);
|
||||
if (confirmed == true && mounted) {
|
||||
await provider.delete(item);
|
||||
}
|
||||
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('删除'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,46 @@
|
||||
import 'package:code_editor/models/file_item.dart';
|
||||
import 'package:code_editor/providers/editor_provider.dart';
|
||||
import 'package:code_editor/widgets/file_item_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../models/file_item.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class FileTreeWidget extends StatelessWidget {
|
||||
final FileItem? rootFileItem;
|
||||
|
||||
const FileTreeWidget({
|
||||
super.key,
|
||||
required this.rootFileItem
|
||||
});
|
||||
const FileTreeWidget({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final rootPath = context.select<EditorProvider, String?>((p) => p.rootPath);
|
||||
final items = context.select<EditorProvider, List<FileItem>>((p) => p.items);
|
||||
|
||||
final root = rootFileItem;
|
||||
if(root == null) {
|
||||
// 1. 如果用户还没有打开/选择任何根目录
|
||||
if (rootPath == null || rootPath.trim().isEmpty) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Center(
|
||||
child: Text(
|
||||
"请点击上方按钮打开文件目录",
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold
|
||||
textAlign: TextAlign.center,
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final children = root.children;
|
||||
if(children.isEmpty) {
|
||||
// 2. 如果已经选择了根目录,但该目录下没有任何文件或子目录
|
||||
if (items.isEmpty) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Center(
|
||||
child: Text(
|
||||
"目录为空",
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold
|
||||
textAlign: TextAlign.center,
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -45,13 +48,12 @@ class FileTreeWidget extends StatelessWidget {
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: children.length,
|
||||
key: const PageStorageKey('code_editor_file_tree_list'),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4.0),
|
||||
itemCount: items.length,
|
||||
itemBuilder: (context, index) {
|
||||
return FileItemWidget(
|
||||
fileItem: children[index],
|
||||
fileItem: items[index],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user