Browse Source

静态文本框和日期显示实现绑定JSON模板

loki.wu 2 years ago
parent
commit
1813b51407

+ 53 - 0
lib/converts/alignment_convert.dart

@@ -0,0 +1,53 @@
+import 'package:fis_lib_report/report/interfaces/position_layout.dart';
+import 'package:flutter/widgets.dart';
+
+class AlignmentConvert {
+  static TextAlignVertical verticalAlignmentConvert(VerticalLayout? value) {
+    if (value == null) {
+      return TextAlignVertical.center;
+    }
+    switch (value) {
+      case VerticalLayout.Top:
+        return TextAlignVertical.top;
+      case VerticalLayout.Bottom:
+        return TextAlignVertical.bottom;
+      case VerticalLayout.Center:
+        return TextAlignVertical.center;
+      case VerticalLayout.Stretch:
+        return TextAlignVertical.center;
+    }
+  }
+
+  static TextAlign horizontalAlignmentConvert(HorizontalLayout? value) {
+    if (value == null) {
+      return TextAlign.center;
+    } else {
+      switch (value) {
+        case HorizontalLayout.Left:
+          return TextAlign.left;
+        case HorizontalLayout.Center:
+          return TextAlign.center;
+        case HorizontalLayout.Right:
+          return TextAlign.right;
+        case HorizontalLayout.Stretch:
+          return TextAlign.justify;
+      }
+    }
+  }
+
+  static Alignment verticalLayoutConvert(VerticalLayout? value) {
+    if (value == null) {
+      return Alignment.center;
+    }
+    switch (value) {
+      case VerticalLayout.Top:
+        return Alignment.topCenter;
+      case VerticalLayout.Bottom:
+        return Alignment.bottomCenter;
+      case VerticalLayout.Center:
+        return Alignment.center;
+      case VerticalLayout.Stretch:
+        return Alignment.centerLeft;
+    }
+  }
+}

+ 62 - 0
lib/converts/date_to_string_converter.dart

@@ -0,0 +1,62 @@
+class DateToStringConverter {
+  static String dateAndTimeToString(
+      var timestamp, Map<String, String> formart) {
+    if (timestamp == null || timestamp == "") {
+      return "";
+    }
+    String targetString = "";
+    final date = DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);
+    // final String tmp = date.toString();
+    String year = date.year.toString();
+    String month = date.month.toString();
+    if (date.month <= 9) {
+      month = "0" + month;
+    }
+    String day = date.day.toString();
+    if (date.day <= 9) {
+      day = "0" + day;
+    }
+    String hour = date.hour.toString();
+    if (date.hour <= 9) {
+      hour = "0" + hour;
+    }
+    String minute = date.minute.toString();
+    if (date.minute <= 9) {
+      minute = "0" + minute;
+    }
+    String second = date.second.toString();
+    if (date.second <= 9) {
+      second = "0" + second;
+    }
+    // String millisecond = date.millisecond.toString();
+    String morningOrafternoon = "上午";
+    if (date.hour >= 12) {
+      morningOrafternoon = "下午";
+    }
+
+    if (formart["y-m"] != null && formart["m-d"] != null) {
+      targetString = year + formart["y-m"]! + month + formart["m-d"]! + day;
+    } else if (formart["y-m"] == null && formart["m-d"] != null) {
+      targetString = month + formart["m-d"]! + day;
+    } else if (formart["y-m"] != null && formart["m-d"] == null) {
+      targetString = year + formart["y-m"]! + month;
+    }
+
+    targetString += " ";
+
+    if (formart["m-a"] != null) {
+      targetString += morningOrafternoon + " ";
+    }
+
+    if (formart["h-m"] != null && formart["m-s"] != null) {
+      targetString +=
+          hour + formart["h-m"]! + minute + formart["m-s"]! + second;
+    } else if (formart["h-m"] == null && formart["m-s"] != null) {
+      targetString += minute + formart["m-s"]! + second;
+    } else if (formart["h-m"] != null && formart["m-s"] == null) {
+      targetString += hour + formart["h-m"]! + minute;
+    }
+
+    return targetString;
+  }
+}

+ 17 - 0
lib/converts/text_size_converter.dart

@@ -0,0 +1,17 @@
+import 'package:flutter/cupertino.dart';
+
+class TextSizeConvert {
+  ///计算文本Size
+  static Size getTextSize(String text, TextStyle style,
+      {int maxLines = 2 ^ 31, double maxWidth = double.infinity}) {
+    if (text.isEmpty) {
+      return Size.zero;
+    }
+    final TextPainter textPainter = TextPainter(
+        textDirection: TextDirection.ltr,
+        text: TextSpan(text: text, style: style),
+        maxLines: maxLines)
+      ..layout(maxWidth: maxWidth);
+    return textPainter.size;
+  }
+}

+ 0 - 20
lib/converts/vertical_alignment.dart

@@ -1,20 +0,0 @@
-import 'package:fis_lib_report/report/interfaces/position_layout.dart';
-import 'package:flutter/widgets.dart';
-
-class VerticalAlignmentToAlignVertical {
-  static TextAlignVertical VerticalAlignmentConvert(VerticalLayout? value) {
-    if (value == null) {
-      return TextAlignVertical.center;
-    }
-    switch (value) {
-      case VerticalLayout.Top:
-        return TextAlignVertical.top;
-      case VerticalLayout.Bottom:
-        return TextAlignVertical.bottom;
-      case VerticalLayout.Center:
-        return TextAlignVertical.center;
-      case VerticalLayout.Stretch:
-        return TextAlignVertical.center;
-    }
-  }
-}

+ 105 - 0
lib/pages/components/RDateTime.dart

@@ -0,0 +1,105 @@
+import 'package:fis_lib_report/converts/date_to_string_converter.dart';
+import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
+import 'package:fis_lib_report/converts/text_size_converter.dart';
+import 'package:fis_lib_report/converts/vertical_alignment.dart';
+import 'package:fis_lib_report/report/dateTimeElement.dart';
+import 'package:flutter/cupertino.dart';
+import 'package:flutter/material.dart';
+
+class RDateTime extends StatefulWidget {
+  final DateTimeElement dateTimeElement;
+  RDateTime(this.dateTimeElement);
+  @override
+  State<StatefulWidget> createState() {
+    return _RDateTimeState(dateTimeElement);
+  }
+}
+
+class _RDateTimeState extends State<RDateTime> {
+  final DateTimeElement dateTimeElement;
+  _RDateTimeState(this.dateTimeElement);
+
+  String _dateTime = '';
+  double _fontSize = 15.0;
+  double _width = 0;
+  TextStyle _style = TextStyle();
+  Color _fontColor = Colors.black;
+  Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
+  EdgeInsets _margin = EdgeInsets.all(0);
+
+  @override
+  initState() {
+    final currentDateTime = DateTime.now();
+    final timestamp = currentDateTime.millisecondsSinceEpoch;
+    _dateTime = currentDateTime.toString().substring(0, 10);
+    final format = dateTimeElement.dateTimeFormat;
+    if (format != null) {
+      if (format == 'yyyy-MM-dd') {
+        _dateTime = DateToStringConverter.dateAndTimeToString(
+            timestamp, {"y-m": "-", "m-d": "-"});
+      } else if (format == 'yyyy年M月d日 HH:mm') {
+        _dateTime = DateToStringConverter.dateAndTimeToString(
+            timestamp, {"y-m": "年", "m-d": "月", "d-h": "日", "h-m": ":"});
+      } else if (format == 'yyyy-MM-dd HH:mm') {
+        _dateTime = DateToStringConverter.dateAndTimeToString(
+            timestamp, {"y-m": "-", "m-d": "-", "h-m": ":"});
+      } else if (format == "yyyy年M月d日") {
+        _dateTime = DateToStringConverter.dateAndTimeToString(
+            timestamp, {"y-m": "年", "m-d": "月", "d-h": "日"});
+      } else if (format == 'yyyy.MM.dd') {
+        _dateTime = DateToStringConverter.dateAndTimeToString(
+            timestamp, {"y-m": ".", "m-d": "."});
+      } else if (format == 'yyyy/MM/dd') {
+        _dateTime = DateToStringConverter.dateAndTimeToString(
+            timestamp, {"y-m": "/", "m-d": "/"});
+      }
+    }
+
+    _fontSize = dateTimeElement.fontSize ?? 15.0;
+    //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
+    final fontStyles = dateTimeElement.fontStyles;
+    final fontColor = dateTimeElement.fontColor;
+    if (fontColor != null) {
+      _fontColor = Color.fromARGB(
+          fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!);
+    }
+    final backgroundColor = dateTimeElement.background;
+    if (backgroundColor != null) {
+      _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
+          backgroundColor.g!, backgroundColor.b!);
+    }
+    final margin = dateTimeElement.margin;
+    if (margin != null) {
+      _margin = EdgeInsets.only(
+          top: margin.top ?? 0,
+          bottom: margin.bottom ?? 0,
+          left: margin.left ?? 0,
+          right: margin.right ?? 0);
+    }
+    _style = TextStyle(
+      fontSize: PtToPxConverter.ptToPx(_fontSize),
+      color: _fontColor,
+      backgroundColor: _backgroundColor,
+    );
+
+    _width = PtToPxConverter.ptToPx(
+        TextSizeConvert.getTextSize(_dateTime, _style).width);
+    super.initState();
+  }
+
+  @override
+  Widget build(BuildContext context) {
+    return Container(
+      margin: _margin,
+      alignment: AlignmentConvert.verticalLayoutConvert(
+          dateTimeElement.verticalAlignment!),
+      width: _width,
+      child: Text(
+        _dateTime,
+        style: _style,
+        textAlign: AlignmentConvert.horizontalAlignmentConvert(
+            dateTimeElement.horizontalAlignment),
+      ),
+    );
+  }
+}

+ 6 - 19
lib/pages/components/input_text.dart

@@ -1,4 +1,5 @@
 import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
+import 'package:fis_lib_report/converts/text_size_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';
@@ -90,9 +91,8 @@ class _RInputTextState extends State<RInputText> {
           fillColor: _backgroundColor,
           filled: true,
         ),
-        textAlignVertical:
-            VerticalAlignmentToAlignVertical.VerticalAlignmentConvert(
-                inputText.verticalAlignment),
+        textAlignVertical: AlignmentConvert.verticalAlignmentConvert(
+            inputText.verticalAlignment),
         maxLines: _textWrap! ? 6 : 1,
         minLines: _textWrap! ? 6 : 1,
         controller: _controller,
@@ -105,23 +105,9 @@ class _RInputTextState extends State<RInputText> {
     );
   }
 
-  ///计算文本Size
-  static Size _boundingTextSize(String text, TextStyle style,
-      {int maxLines = 2 ^ 31, double maxWidth = double.infinity}) {
-    if (text.isEmpty) {
-      return Size.zero;
-    }
-    final TextPainter textPainter = TextPainter(
-        textDirection: TextDirection.ltr,
-        text: TextSpan(text: text, style: style),
-        maxLines: maxLines)
-      ..layout(maxWidth: maxWidth);
-    return textPainter.size;
-  }
-
   //onchange 事件
   void _onInputChanged(TextStyle _textStyle, String value) {
-    final width = _boundingTextSize(value, _textStyle).width;
+    final width = TextSizeConvert.getTextSize(value, _textStyle).width;
     // ignore: todo
     //TODO(LOki):此处需要区分不同的输入框
     if (inputText.tag!.name == 'HospitalName') {
@@ -136,7 +122,8 @@ class _RInputTextState extends State<RInputText> {
           //重置长度
           _lineWidth = inputText.lineWidth;
         });
-      } else if (_boundingTextSize(_controller.text, _textStyle).width >
+      } else if (TextSizeConvert.getTextSize(_controller.text, _textStyle)
+              .width >
           inputText.lineWidth!) {
         setState(() {
           if (width < 500) {

+ 77 - 0
lib/pages/components/static_text.dart

@@ -0,0 +1,77 @@
+import 'package:fis_lib_report/converts/alignment_convert.dart';
+import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
+import 'package:fis_lib_report/converts/text_size_converter.dart';
+import 'package:fis_lib_report/report/staticText.dart';
+import 'package:flutter/material.dart';
+
+class RStaticText extends StatefulWidget {
+  RStaticText(this.staticText);
+  final StaticText? staticText;
+
+  @override
+  State<StatefulWidget> createState() {
+    return _RStaticTextState(staticText!);
+  }
+}
+
+class _RStaticTextState extends State<RStaticText> {
+  final StaticText staticText;
+
+  _RStaticTextState(this.staticText);
+
+  double _fontSize = 15.0;
+  double _width = 0;
+  TextStyle _style = TextStyle();
+  Color _fontColor = Colors.black;
+  Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
+  EdgeInsets _margin = EdgeInsets.all(0);
+  @override
+  initState() {
+    _fontSize = staticText.fontSize ?? 15.0;
+    //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
+    final fontStyles = staticText.fontStyles;
+    final fontColor = staticText.fontColor;
+    if (fontColor != null) {
+      _fontColor = Color.fromARGB(
+          fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!);
+    }
+    final backgroundColor = staticText.background;
+    if (backgroundColor != null) {
+      _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
+          backgroundColor.g!, backgroundColor.b!);
+    }
+    final margin = staticText.margin;
+    if (margin != null) {
+      _margin = EdgeInsets.only(
+          top: margin.top ?? 0,
+          bottom: margin.bottom ?? 0,
+          left: margin.left ?? 0,
+          right: margin.right ?? 0);
+    }
+    _style = TextStyle(
+      fontSize: PtToPxConverter.ptToPx(_fontSize),
+      color: _fontColor,
+      backgroundColor: _backgroundColor,
+    );
+
+    _width = PtToPxConverter.ptToPx(
+        TextSizeConvert.getTextSize(staticText.text ?? '', _style).width * 2);
+    super.initState();
+  }
+
+  @override
+  Widget build(BuildContext context) {
+    return Container(
+      margin: _margin,
+      alignment:
+          AlignmentConvert.verticalLayoutConvert(staticText.verticalAlignment!),
+      width: _width,
+      child: Text(
+        staticText.text ?? '',
+        style: _style,
+        textAlign: AlignmentConvert.horizontalAlignmentConvert(
+            staticText.horizontalAlignment),
+      ),
+    );
+  }
+}

+ 4 - 10
lib/pages/paragraph_page.dart

@@ -1,5 +1,7 @@
+import 'package:fis_lib_report/pages/components/RDateTime.dart';
 import 'package:fis_lib_report/pages/components/input_text.dart';
 import 'package:fis_lib_report/pages/components/multi_select.dart';
+import 'package:fis_lib_report/pages/components/static_Text.dart';
 import 'package:fis_lib_report/pages/helpler.dart';
 import 'package:fis_lib_report/report/dateTimeElement.dart';
 import 'package:fis_lib_report/report/element_type.dart';
@@ -56,10 +58,7 @@ class _ParagraphState extends State<ParagraphPage> {
           } else if (element.elementType!.name ==
               ElementType.staticText!.name) {
             StaticText staticText = element as StaticText;
-            return SizedBox(
-              width: staticText.text!.length > 10 ? 300 : 110,
-              child: Text(staticText.text!),
-            );
+            return RStaticText(staticText);
           } else if (element.elementType!.name ==
               ElementType.singleSelected!.name) {
             SingleSelected singleSelected = element as SingleSelected;
@@ -94,12 +93,7 @@ class _ParagraphState extends State<ParagraphPage> {
             );
           } else if (element.elementType!.name == ElementType.dateTime!.name) {
             final dateTime = element as DateTimeElement;
-            final currentDateTime = DateTime.now();
-            final text = currentDateTime.toString().substring(0, 10);
-            return Container(
-              width: 110,
-              child: Text(text),
-            );
+            return RDateTime(dateTime);
           } else if (element.elementType!.name ==
               ElementType.multiSelected!.name) {
             final multiSelected = element as MultiSelected;