1
This commit is contained in:
1 parent
b03ece33a2
commit
54c3083c24
29 files changed
+2150
-549
No files matched your search
@@ -1,30 +1,61 @@
|
||||
import 'package:code_editor/l10n/app_localizations.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/widgets/file_directory_history_widget.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:code_editor/widgets/project_history_widget.dart';
|
||||
import 'package:code_editor/widgets/file_tree_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class CodeEditorDrawer extends StatelessWidget {
|
||||
const CodeEditorDrawer({super.key});
|
||||
|
||||
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;
|
||||
}
|
||||
return listen ? context.watch<ProjectProvider>() : context.read<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;
|
||||
return listen ? context.watch<TabProvider>() : context.read<TabProvider>();
|
||||
}
|
||||
|
||||
Future<void> _handleNewFile(BuildContext context, String rootPath) async {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final name = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
title: l10n?.newFile ?? '新建文件',
|
||||
hintText: l10n?.fileNameHint ?? '文件名 (例如: main.dart)',
|
||||
);
|
||||
if (name != null && name.trim().isNotEmpty && context.mounted) {
|
||||
try {
|
||||
await _getProjectProvider(context).createFile(rootPath, name.trim());
|
||||
if (context.mounted) {
|
||||
final newFilePath = p.join(rootPath, name.trim());
|
||||
await _getTabProvider(context).openFile(newFilePath);
|
||||
}
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
DialogUtils.showErrorToast(context, l10n?.createFileFailed(e.toString()) ?? '创建文件失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _handleNewFolder(BuildContext context, String rootPath) async {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final name = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
title: l10n?.newFolder ?? '新建文件夹',
|
||||
hintText: l10n?.folderNameHint ?? '文件夹名',
|
||||
);
|
||||
if (name != null && name.trim().isNotEmpty && context.mounted) {
|
||||
try {
|
||||
await _getProjectProvider(context).createDirectory(rootPath, name.trim());
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
DialogUtils.showErrorToast(context, l10n?.createFolderFailed(e.toString()) ?? '创建文件夹失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +65,7 @@ class CodeEditorDrawer extends StatelessWidget {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final projectProvider = _getProjectProvider(context, listen: true);
|
||||
final rootPath = projectProvider.rootPath;
|
||||
final hasProject = rootPath != null && rootPath.trim().isNotEmpty;
|
||||
|
||||
return Drawer(
|
||||
child: SafeArea(
|
||||
@@ -73,9 +105,9 @@ class CodeEditorDrawer extends StatelessWidget {
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.history),
|
||||
tooltip: l10n.viewFileDirectoryHistory,
|
||||
tooltip: l10n.viewProjectHistory,
|
||||
onPressed: () {
|
||||
FileDirectoryHistoryWidget.show(
|
||||
ProjectHistoryWidget.show(
|
||||
context,
|
||||
onSelectHistory: (selectedHistory) async {
|
||||
final canProceed = await _getTabProvider(context).checkUnsavedChanges(context);
|
||||
@@ -87,13 +119,41 @@ class CodeEditorDrawer extends StatelessWidget {
|
||||
),
|
||||
],
|
||||
),
|
||||
Text(
|
||||
rootPath ?? '',
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
rootPath ?? '',
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
),
|
||||
),
|
||||
if (hasProject) ...[
|
||||
const SizedBox(width: 8),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
iconSize: 18,
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(minWidth: 28, minHeight: 28),
|
||||
icon: const Icon(Icons.note_add_outlined),
|
||||
tooltip: l10n.newFile,
|
||||
onPressed: () => _handleNewFile(context, rootPath),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
iconSize: 18,
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(minWidth: 28, minHeight: 28),
|
||||
icon: const Icon(Icons.create_new_folder_outlined),
|
||||
tooltip: l10n.newFolder,
|
||||
onPressed: () => _handleNewFolder(context, rootPath),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user