This commit is contained in:
LZX committed 2026-09-10 22:04:06 +08:00
commit 2379b4476f
141 files changed
+6087

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
import 'dart:convert';
import 'package:code_editor/services/file_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_code_editor/flutter_code_editor.dart';
import 'package:flutter_highlight/themes/atom-one-dark.dart';
import 'package:highlight/languages/dart.dart';
import 'package:path/path.dart' as p;
class CodeEditorWidget extends StatefulWidget {
final String? rootPath;
final String? filePath;
const CodeEditorWidget({
super.key,
required this.rootPath,
required this.filePath
});
@override
State<CodeEditorWidget> createState() => _CodeEditorWidgetState();
}
class _CodeEditorWidgetState extends State<CodeEditorWidget> {
final CodeController _controller = CodeController();
bool _isLoading = false;
int _currentLoadVersion = 0;
String? _errorMessage;
@override
void initState() {
super.initState();
_loadFileContent();
}
@override
void didUpdateWidget(covariant CodeEditorWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.filePath != widget.filePath || oldWidget.rootPath != widget.rootPath) {
_loadFileContent();
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
if(_isLoading) {
return const Center(child: CircularProgressIndicator());
}
return CodeTheme(
data: CodeThemeData(styles: atomOneDarkTheme),
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;
if (filePath != null && filePath.isNotEmpty) {
if (rootPath != null && rootPath.isNotEmpty && !p.isAbsolute(filePath)) {
fullFilePath = p.join(rootPath, filePath);
} else {
fullFilePath = filePath;
}
}
if(fullFilePath == null) {
_controller.text = '';
if(mounted) {
setState(() {
_isLoading = false;
_errorMessage = null;
});
}
return;
}
final int requestVersion = ++_currentLoadVersion;
setState(() {
_isLoading = true;
_errorMessage = null;
});
try {
final content = await FileService.instance.readFileContent(fullFilePath);
if(!mounted || requestVersion != _currentLoadVersion) {
return;
}
_controller.language = _resolveLanguage(fullFilePath);
_controller.text = content;
}catch(e) {
if(!mounted || requestVersion != _currentLoadVersion) return;
setState(() {
_errorMessage = '读取文件失败: $e';
});
}finally {
if(mounted && requestVersion == _currentLoadVersion) {
setState(() => _isLoading = false);
}
}
}
dynamic _resolveLanguage(String path) {
if(path.endsWith('.dart')) return dart;
if(path.endsWith('.json')) return json;
return null;
}
}
+147
View File
@@ -0,0 +1,147 @@
import 'package:code_editor/models/file_item.dart';
import 'package:flutter/material.dart';
class FileItemWidget extends StatefulWidget {
final FileItem fileItem;
const FileItemWidget({
super.key,
required this.fileItem
});
@override
State<FileItemWidget> createState() => _FileItemWidget();
}
class _FileItemWidget extends State<FileItemWidget> {
@override
Widget build(BuildContext context) {
return _buildFileItem(context, widget.fileItem);
}
Widget _buildFileItem(BuildContext context, FileItem item) {
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) {
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);
},
child: ListTile(
contentPadding: indent,
leading: Icon(Icons.file_copy),
title: Text(
item.name,
style: const TextStyle(fontSize: 13),
),
dense: true,
onTap: () {
_openFile(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) {
}
void _showContextMenu(BuildContext context, 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 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('新建文件'),
],
),
),
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)),
],
),
),
],
);
// 用户点击了某一项后的处理逻辑
if(selectedAction != null) {
_handleMenuAction(context, selectedAction);
}
}
void _handleMenuAction(BuildContext context, String action) {
switch (action) {
}
}
}
+59
View File
@@ -0,0 +1,59 @@
import 'package:code_editor/widgets/file_item_widget.dart';
import 'package:flutter/material.dart';
import '../models/file_item.dart';
class FileTreeWidget extends StatelessWidget {
final FileItem? rootFileItem;
const FileTreeWidget({
super.key,
required this.rootFileItem
});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final root = rootFileItem;
if(root == null) {
return Padding(
padding: const EdgeInsets.all(24.0),
child: Center(
child: Text(
"请点击上方按钮打开文件目录",
style: TextStyle(
fontWeight: FontWeight.bold
),
),
),
);
}
final children = root.children;
if(children.isEmpty) {
return Padding(
padding: const EdgeInsets.all(24.0),
child: Center(
child: Text(
"目录为空",
style: TextStyle(
fontWeight: FontWeight.bold
),
),
),
);
}
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
padding: EdgeInsets.zero,
itemCount: children.length,
itemBuilder: (context, index) {
return FileItemWidget(
fileItem: children[index],
);
},
);
}
}