|
|
|
@ -14,131 +14,33 @@ class TemplateParserTab extends StatefulWidget {
@@ -14,131 +14,33 @@ class TemplateParserTab extends StatefulWidget {
|
|
|
|
|
|
|
|
|
|
class TemplateParserTabState extends State<TemplateParserTab> { |
|
|
|
|
late TabProvider _provider; |
|
|
|
|
String? _editor1FileName; |
|
|
|
|
String? _editor2FileName; |
|
|
|
|
String _editor1Content = ''; |
|
|
|
|
String _editor2Content = ''; |
|
|
|
|
int _activeEditorIndex = 0; // 0表示第一个编辑器,1表示第二个 |
|
|
|
|
final GlobalKey<TextEditorState> _editor1Key = GlobalKey(); |
|
|
|
|
final GlobalKey<TextEditorState> _editor2Key = GlobalKey(); |
|
|
|
|
|
|
|
|
|
// 添加焦点监听 |
|
|
|
|
void _setupFocusListeners() { |
|
|
|
|
_editor1Key.currentState?.focusNode.addListener(() { |
|
|
|
|
if (_editor1Key.currentState?.hasFocus ?? false) { |
|
|
|
|
setState(() => _activeEditorIndex = 0); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
_editor2Key.currentState?.focusNode.addListener(() { |
|
|
|
|
if (_editor2Key.currentState?.hasFocus ?? false) { |
|
|
|
|
setState(() => _activeEditorIndex = 1); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
void initState() { |
|
|
|
|
super.initState(); |
|
|
|
|
_provider = Provider.of<TabProvider>(context, listen: false); |
|
|
|
|
_provider.registerTextTabController(widget.tabId, this); |
|
|
|
|
final tab = _provider.getTabById(widget.tabId); |
|
|
|
|
if (tab != null) { |
|
|
|
|
_editor1Content = tab.content; |
|
|
|
|
_editor2Content = tab.content; |
|
|
|
|
_editor1FileName = tab.fileName; |
|
|
|
|
_editor2FileName = tab.fileName; |
|
|
|
|
} |
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) { |
|
|
|
|
_setupFocusListeners(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 修改loadFile方法使用_activeEditorIndex |
|
|
|
|
Future<void> loadFile(BuildContext context, String filePath) async { |
|
|
|
|
if (_activeEditorIndex == 0) { |
|
|
|
|
_editor1Key.currentState?.loadFile(context, filePath); |
|
|
|
|
} else { |
|
|
|
|
_editor2Key.currentState?.loadFile(context, filePath); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Future<void> loadFile(BuildContext context, String filePath) async {} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Widget build(BuildContext context) { |
|
|
|
|
final tab = _provider.getTabById(widget.tabId); |
|
|
|
|
final tab = _provider.tabs.firstWhere((t) => t.id == widget.tabId); |
|
|
|
|
if (tab == null) { |
|
|
|
|
return const Center(child: Text('选项卡不存在')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return SingleChildScrollView( |
|
|
|
|
child: ConstrainedBox( |
|
|
|
|
constraints: const BoxConstraints(minHeight: 400), |
|
|
|
|
child: Column( |
|
|
|
|
mainAxisSize: MainAxisSize.min, |
|
|
|
|
children: [ |
|
|
|
|
Flexible( |
|
|
|
|
child: SizedBox( |
|
|
|
|
height: MediaQuery.of(context).size.height / 2 - 100, |
|
|
|
|
child: Container( |
|
|
|
|
decoration: BoxDecoration( |
|
|
|
|
border: Border.all( |
|
|
|
|
color: _activeEditorIndex == 0 ? Colors.blue : Colors.transparent, |
|
|
|
|
width: 2.0, |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
child: TextEditor( |
|
|
|
|
key: _editor1Key, |
|
|
|
|
tabId: '${widget.tabId}_1', |
|
|
|
|
title: '源文本', // 可配置的标题 |
|
|
|
|
initialContent: _editor1Content, |
|
|
|
|
fileName: _editor1FileName, |
|
|
|
|
onContentChanged: (content, fileName) { |
|
|
|
|
setState(() { |
|
|
|
|
_editor1Content = content; |
|
|
|
|
_editor1FileName = fileName; |
|
|
|
|
}); |
|
|
|
|
_provider.updateContent(widget.tabId, content, fileName); |
|
|
|
|
}, |
|
|
|
|
onFileLoaded: (filePath) { |
|
|
|
|
// 文件加载处理 |
|
|
|
|
}, |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
Flexible( |
|
|
|
|
child: SizedBox( |
|
|
|
|
height: MediaQuery.of(context).size.height / 2 - 100, |
|
|
|
|
child: Container( |
|
|
|
|
decoration: BoxDecoration( |
|
|
|
|
border: Border.all( |
|
|
|
|
color: _activeEditorIndex == 1 ? Colors.blue : Colors.transparent, |
|
|
|
|
width: 2.0, |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
child: TextEditor( |
|
|
|
|
key: _editor2Key, |
|
|
|
|
tabId: '${widget.tabId}_2', |
|
|
|
|
title: '目标文本', // 可配置的标题 |
|
|
|
|
initialContent: _editor2Content, |
|
|
|
|
fileName: _editor2FileName, |
|
|
|
|
onContentChanged: (content, fileName) { |
|
|
|
|
setState(() { |
|
|
|
|
_editor2Content = content; |
|
|
|
|
_editor2FileName = fileName; |
|
|
|
|
}); |
|
|
|
|
_provider.updateContent(widget.tabId, content, fileName); |
|
|
|
|
}, |
|
|
|
|
onFileLoaded: (filePath) { |
|
|
|
|
// 文件加载处理 |
|
|
|
|
}, |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
], |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
return TextEditor( |
|
|
|
|
tabId: widget.tabId, |
|
|
|
|
initialContent: tab.content, |
|
|
|
|
fileName: tab.fileName, |
|
|
|
|
onContentChanged: (content, fileName) { |
|
|
|
|
_provider.updateContent(widget.tabId, content, fileName); |
|
|
|
|
}, |
|
|
|
|
onFileLoaded: (filePath) { |
|
|
|
|
// 可以在这里添加文件加载后的额外处理 |
|
|
|
|
}, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|