You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.4 KiB
97 lines
3.4 KiB
2 months ago
|
import 'package:flutter/material.dart';
|
||
|
import 'package:provider/provider.dart';
|
||
|
import 'package:flutter/services.dart'; // 复制功能需要
|
||
|
import 'package:win_text_editor/app/providers/editor_provider.dart';
|
||
|
|
||
|
class TextTab extends StatelessWidget {
|
||
|
final String tabId;
|
||
|
|
||
|
const TextTab({super.key, required this.tabId});
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
final provider = Provider.of<EditorProvider>(context);
|
||
|
final tab = provider.tabs.firstWhere((t) => t.id == tabId);
|
||
|
|
||
|
return Column(
|
||
|
children: [
|
||
|
// 新增工具条
|
||
|
Container(
|
||
|
height: 40,
|
||
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||
|
decoration: BoxDecoration(
|
||
|
color: Colors.grey[100],
|
||
|
border: Border(bottom: BorderSide(color: Colors.grey[300]!)),
|
||
|
),
|
||
|
child: Row(
|
||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||
|
children: [
|
||
|
const Text('源文本', style: TextStyle(fontWeight: FontWeight.bold)),
|
||
|
Row(
|
||
|
children: [
|
||
|
IconButton(
|
||
|
icon: const Icon(Icons.folder_open, size: 20),
|
||
|
tooltip: '打开文件',
|
||
|
onPressed: () => _openFile(context, tabId),
|
||
|
),
|
||
|
IconButton(
|
||
|
icon: const Icon(Icons.content_copy, size: 20),
|
||
|
tooltip: '复制内容',
|
||
|
onPressed: () => _copyToClipboard(context, tab.content),
|
||
|
),
|
||
|
IconButton(
|
||
|
icon: const Icon(Icons.save, size: 20),
|
||
|
tooltip: '保存文件',
|
||
|
onPressed: () => _saveFile(context, tab.content),
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
// 文本编辑区
|
||
|
Expanded(
|
||
|
child: TextField(
|
||
|
maxLines: null,
|
||
|
controller: TextEditingController(text: tab.content),
|
||
|
onChanged: (text) => provider.updateContent(tabId, text),
|
||
|
decoration: const InputDecoration(
|
||
|
border: InputBorder.none,
|
||
|
hintText: '开始输入内容...',
|
||
|
contentPadding: EdgeInsets.all(16),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
],
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// 打开文件模拟功能
|
||
|
Future<void> _openFile(BuildContext context, String tabId) async {
|
||
|
final provider = Provider.of<EditorProvider>(context, listen: false);
|
||
|
// 这里是模拟代码,实际应该使用文件选择器
|
||
|
const mockContent = "从文件加载的示例文本...";
|
||
|
provider.updateContent(tabId, mockContent);
|
||
|
|
||
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('已加载文件内容')));
|
||
|
}
|
||
|
|
||
|
// 复制到剪贴板
|
||
|
Future<void> _copyToClipboard(BuildContext context, String content) async {
|
||
|
await Clipboard.setData(ClipboardData(text: content));
|
||
|
if (context.mounted) {
|
||
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('已复制到剪贴板')));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 保存文件模拟功能
|
||
|
Future<void> _saveFile(BuildContext context, String content) async {
|
||
|
// 这里是模拟代码,实际应该使用文件保存对话框
|
||
|
if (context.mounted) {
|
||
|
ScaffoldMessenger.of(
|
||
|
context,
|
||
|
).showSnackBar(SnackBar(content: Text('已保存 ${content.length} 个字符')));
|
||
|
}
|
||
|
}
|
||
|
}
|