|
|
|
@ -11,23 +11,33 @@ class TextEditor extends StatefulWidget {
@@ -11,23 +11,33 @@ class TextEditor extends StatefulWidget {
|
|
|
|
|
final String tabId; |
|
|
|
|
final String? initialContent; |
|
|
|
|
final String title; |
|
|
|
|
final ValueChanged<String>? onContentChanged; |
|
|
|
|
|
|
|
|
|
const TextEditor({super.key, required this.tabId, this.initialContent, this.title = '未命名'}); |
|
|
|
|
const TextEditor({ |
|
|
|
|
super.key, |
|
|
|
|
required this.tabId, |
|
|
|
|
this.initialContent, |
|
|
|
|
this.title = '未命名', |
|
|
|
|
this.onContentChanged, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
State<TextEditor> createState() => TextEditorState(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class TextEditorState extends State<TextEditor> { |
|
|
|
|
class TextEditorState extends State<TextEditor> with AutomaticKeepAliveClientMixin { |
|
|
|
|
final TextEditingController _textController = TextEditingController(); |
|
|
|
|
final FocusNode _focusNode = FocusNode(); |
|
|
|
|
bool _isLoading = false; |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
bool get wantKeepAlive => true; |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
void initState() { |
|
|
|
|
super.initState(); |
|
|
|
|
_textController.text = widget.initialContent ?? ''; |
|
|
|
|
// 移除监听器,改为在需要时直接获取内容 |
|
|
|
|
_textController.addListener(_handleTextChanged); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
@ -38,12 +48,15 @@ class TextEditorState extends State<TextEditor> {
@@ -38,12 +48,15 @@ class TextEditorState extends State<TextEditor> {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String getContent() { |
|
|
|
|
return _textController.text; |
|
|
|
|
void _handleTextChanged() { |
|
|
|
|
if (widget.onContentChanged != null) { |
|
|
|
|
widget.onContentChanged!(_textController.text); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
void dispose() { |
|
|
|
|
_textController.removeListener(_handleTextChanged); |
|
|
|
|
_textController.dispose(); |
|
|
|
|
_focusNode.dispose(); |
|
|
|
|
super.dispose(); |
|
|
|
@ -51,6 +64,7 @@ class TextEditorState extends State<TextEditor> {
@@ -51,6 +64,7 @@ class TextEditorState extends State<TextEditor> {
|
|
|
|
|
|
|
|
|
|
@override |
|
|
|
|
Widget build(BuildContext context) { |
|
|
|
|
super.build(context); |
|
|
|
|
return Column( |
|
|
|
|
children: [ |
|
|
|
|
EditorToolbar( |
|
|
|
|