瀏覽代碼

输入框优化&Bug修复

loki.wu 2 年之前
父節點
當前提交
c6ecad04ce
共有 3 個文件被更改,包括 43 次插入26 次删除
  1. 39 23
      lib/pages/components/input_text.dart
  2. 2 2
      lib/report/element.dart
  3. 2 1
      lib/report/element_tag.dart

+ 39 - 23
lib/pages/components/input_text.dart

@@ -1,8 +1,10 @@
 import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
 import 'package:fis_lib_report/converts/vertical_alignment.dart';
 import 'package:fis_lib_report/report/inputText.dart';
+import 'package:fis_lib_report/report/interfaces/position_layout.dart';
 import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
+import 'package:flutter/rendering.dart';
 
 class RInputText extends StatefulWidget {
   final InputText inputText;
@@ -27,6 +29,7 @@ class _RInputTextState extends State<RInputText> {
   Color _fontColor = const Color.fromARGB(255, 0, 0, 0);
   Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
   int? _lineLength = 1;
+  TextStyle? _textStyle;
   @override
   initState() {
     final fontColor = inputText.fontColor;
@@ -39,6 +42,10 @@ class _RInputTextState extends State<RInputText> {
       _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
           backgroundColor.g!, backgroundColor.b!);
     }
+    _textStyle = TextStyle(
+      fontSize: PtToPxConverter.ptToPx(inputText.fontSize),
+      color: _fontColor,
+    );
     //TODO(Loki):set FontName in TextField
     final fontName = inputText.fontName;
     //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
@@ -48,7 +55,6 @@ class _RInputTextState extends State<RInputText> {
     _textWrap = inputText.textWrap;
     _fontSize = PtToPxConverter.ptToPx(inputText.fontSize);
     _height = _fontSize! > 30 ? 36.5 : 22;
-
     print(_lineLength);
     super.initState();
   }
@@ -93,34 +99,16 @@ class _RInputTextState extends State<RInputText> {
         textAlign: TextAlign.start,
         style: _textStyle,
         onChanged: (v) {
-          if (_lineWidth! <
-              boundingTextSize(_controller.text, _textStyle).width) {
-            setState(() {
-              if (_lineWidth! < 600) {
-                _lineWidth = _lineWidth! + 12;
-              }
-            });
-          } else if (_controller.text.isEmpty) {
-            setState(() {
-              _lineWidth = inputText.lineWidth;
-            });
-          } else if (boundingTextSize(_controller.text, _textStyle).width >
-              inputText.lineWidth!) {
-            setState(() {
-              if (_lineWidth! < 600) {
-                _lineWidth =
-                    boundingTextSize(_controller.text, _textStyle).width;
-              }
-            });
-          }
+          _onInputChanged(_textStyle, v);
         },
       ),
     );
   }
 
-  static Size boundingTextSize(String text, TextStyle style,
+  ///计算文本Size
+  static Size _boundingTextSize(String text, TextStyle style,
       {int maxLines = 2 ^ 31, double maxWidth = double.infinity}) {
-    if (text == null || text.isEmpty) {
+    if (text.isEmpty) {
       return Size.zero;
     }
     final TextPainter textPainter = TextPainter(
@@ -130,4 +118,32 @@ class _RInputTextState extends State<RInputText> {
       ..layout(maxWidth: maxWidth);
     return textPainter.size;
   }
+
+  //onchange 事件
+  void _onInputChanged(TextStyle _textStyle, String value) {
+    final width = _boundingTextSize(value, _textStyle).width;
+    // ignore: todo
+    //TODO(LOki):此处需要区分不同的输入框
+    if (inputText.tag!.name == 'HospitalName') {
+      if (_lineWidth! < width) {
+        setState(() {
+          if (width < 500) {
+            _lineWidth = width;
+          }
+        });
+      } else if (_controller.text.isEmpty) {
+        setState(() {
+          //重置长度
+          _lineWidth = inputText.lineWidth;
+        });
+      } else if (_boundingTextSize(_controller.text, _textStyle).width >
+          inputText.lineWidth!) {
+        setState(() {
+          if (width < 500) {
+            _lineWidth = width;
+          }
+        });
+      }
+    }
+  }
 }

+ 2 - 2
lib/report/element.dart

@@ -53,10 +53,10 @@ class Element implements IElement {
     } else {
       measureTag = MeasureTag.fromJson(json['MeasureTag']);
     }
-    if (json['ElementTag'] == null) {
+    if (json['Tag'] == null) {
       tag = null;
     } else {
-      tag = ElementTag.fromJson(json['ElementTag']);
+      tag = ElementTag.fromJson(json['Tag']);
     }
     horizontalAlignment = HorizontalLayout.values
         .firstWhere((e) => e.name == json['HorizontalAlignment']);

+ 2 - 1
lib/report/element_tag.dart

@@ -27,6 +27,7 @@ class ElementTag {
     name = json['Name'];
     displayName = json['DisplayName'];
     isDefault = json['IsDefault'];
-    elementTagType = json['ElementTagType'];
+    elementTagType = ElementTagType.values
+        .firstWhere((element) => element.name == json['ElementTagType']);
   }
 }