Эх сурвалжийг харах

修正多行文本输入时换行问题

gavin.chen 1 жил өмнө
parent
commit
f91d39c0ed

+ 66 - 9
lib/pages/components/input_text.dart

@@ -38,7 +38,7 @@ class FInputText extends FStatefulWidget implements FInteractiveContainer {
 
 class _FInputTextState extends FState<FInputText> {
   _FInputTextState();
-  final _controller = TextEditingController();
+  final _controller = CustomTextEditingController();
   final FocusNode _focusNode = FocusNode();
   final ButtonStyle _buttonStyle = const ButtonStyle(
     padding: MaterialStatePropertyAll(EdgeInsets.all(0)),
@@ -56,13 +56,14 @@ class _FInputTextState extends FState<FInputText> {
   int _rowCount = 1;
   InputDecoration _inputDecoration =
       const InputDecoration(border: OutlineInputBorder());
+  double? _freezeHeight; // 输入框高度冻结
+  /// 组件Key
+  final GlobalKey _textFieldConatinerKey = GlobalKey();
+
   @override
   initState() {
-    _focusNode.addListener(() {
-      if (!_focusNode.hasFocus) {
-        _onInputChanged(_textStyle!, _controller.text);
-      }
-    });
+    _focusNode.addListener(_textFieldFocusNodeListener);
+    _controller.addComposeListener(_composeListener);
     _initStyle();
     WidgetsBinding.instance.addPostFrameCallback((_) {
       _heightAdaptation();
@@ -70,6 +71,28 @@ class _FInputTextState extends FState<FInputText> {
     super.initState();
   }
 
+  /// 焦点事件监听
+  void _textFieldFocusNodeListener() {
+    if (!_focusNode.hasFocus) {
+      _onInputChanged(_textStyle!, _controller.text);
+    }
+  }
+
+  // 开始进行中文输入时触发
+  void _composeListener(bool isComposing) {
+    if (isComposing) {
+      final RenderBox renderBox = _textFieldConatinerKey.currentContext!
+          .findRenderObject() as RenderBox;
+      setState(() {
+        _freezeHeight = renderBox.size.height;
+      });
+    } else {
+      setState(() {
+        _freezeHeight = null;
+      });
+    }
+  }
+
   @override
   void didUpdateWidget(FInputText oldWidget) {
     _initStyle();
@@ -134,8 +157,9 @@ class _FInputTextState extends FState<FInputText> {
       mainAxisSize: MainAxisSize.min,
       children: [
         FContainer(
+          key: _textFieldConatinerKey,
           width: _lineWidth,
-          height: _textWrap ? null : _allHeight,
+          height: _textWrap ? _freezeHeight : _allHeight,
           alignment: Alignment.center,
           child: body,
         ),
@@ -193,6 +217,8 @@ class _FInputTextState extends FState<FInputText> {
   void dispose() {
     _inputTextInfo?.onTextChange.dispose();
     _controller.dispose();
+    _focusNode.removeListener(_textFieldFocusNodeListener);
+    _controller.removeComposeListener(_composeListener);
     super.dispose();
   }
 
@@ -357,13 +383,13 @@ class _FInputTextState extends FState<FInputText> {
   void _heightAdaptation() {
     var cellPostion = widget.cellPostion;
     if (cellPostion != null) {
-      final rowCount = _rowCount > 5 ? 1.5 : 1.0;
+      final rowHeightScale = _rowCount > 5 ? 1.5 : 1.0;
       //触发高度适配
       if (widget.parentTableId.isNotEmpty) {
         FReportInfo.instance.cellRowChange.emit(
           this,
           TableScaleInfo(
-            rowCount < 1 ? 1 : rowCount,
+            rowHeightScale < 1 ? 1 : rowHeightScale,
             cellPostion,
             widget.parentTableId,
           ),
@@ -372,3 +398,34 @@ class _FInputTextState extends FState<FInputText> {
     }
   }
 }
+
+/// 自定义TextEditingController,会在输入法开始输入时触发监听
+class CustomTextEditingController extends TextEditingController {
+  final List<ValueChanged<bool>> _composeListeners = [];
+
+  void addComposeListener(ValueChanged<bool> listener) {
+    _composeListeners.add(listener);
+  }
+
+  void removeComposeListener(ValueChanged<bool> listener) {
+    _composeListeners.remove(listener);
+  }
+
+  @override
+  set value(TextEditingValue newValue) {
+    final bool isComposingBefore = value.composing.isValid;
+    final bool isComposingAfter = newValue.composing.isValid;
+
+    if (isComposingBefore != isComposingAfter) {
+      _notifyComposeListeners(isComposingAfter);
+    }
+
+    super.value = newValue;
+  }
+
+  void _notifyComposeListeners(bool isComposing) {
+    for (final listener in _composeListeners) {
+      listener(isComposing);
+    }
+  }
+}