123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- 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/alignment_convert.dart';
- import 'package:fis_lib_report/report/element_tag.dart';
- import 'package:fis_lib_report/report/inputText.dart';
- import 'package:fis_lib_report/report_info/element_tag_names.dart';
- import 'package:fis_lib_report/report_info/input_text_info.dart';
- import 'package:fis_lib_report/report_info/report_info.dart';
- import 'package:flutter/material.dart';
- class RInputText extends StatefulWidget {
- final InputText inputText;
- const RInputText({Key? key, required this.inputText}) : super(key: key);
- @override
- State<StatefulWidget> createState() {
- return _RInputTextState();
- }
- }
- class _RInputTextState extends State<RInputText> {
- _RInputTextState();
- final _controller = TextEditingController();
- final _focusNode = FocusNode();
- double _lineWidth = 136.0;
- double _fontSize = 22.0;
- double _height = 22.0;
- double _allHeight = 0.0;
- bool _textWrap = false;
- Color _fontColor = const Color.fromARGB(255, 0, 0, 0);
- Color _backgroundColor = const Color.fromARGB(255, 255, 255, 255);
- int _lineLength = 1;
- TextStyle? _textStyle;
- InputTextInfo? _inputTextInfo;
- bool _isReadOnly = false;
- bool _widthLock = false;
- int _rowCount = 1;
- @override
- initState() {
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- _checkInputTextInfo();
- _initStyle();
- return Column(
- children: [
- Container(
- width: _lineWidth,
- height: _allHeight,
- padding: const EdgeInsets.only(right: 5, left: 5),
- decoration: BoxDecoration(
- border: Border.all(
- width: 1,
- color: _isReadOnly ? Colors.transparent : Colors.grey,
- ),
- color: _backgroundColor,
- ),
- child: TextField(
- focusNode: _focusNode,
- readOnly: _isReadOnly,
- decoration: null,
- textAlignVertical: AlignmentConvert.verticalAlignmentConvert(
- widget.inputText.verticalAlignment),
- minLines: 1,
- maxLines: _rowCount,
- controller: _controller,
- textAlign: TextAlign.start,
- style: _textStyle,
- onChanged: (v) {
- // _onInputChanged(_textStyle!, _controller.text);
- },
- onEditingComplete: () {
- _onInputChanged(_textStyle!, _controller.text);
- }),
- ),
- const SizedBox(height: 3),
- if (_inputTextInfo!.tag != null &&
- _inputTextInfo!.tag!.elementTagType ==
- ElementTagType.DiagnosticEntry) ...[
- Row(
- mainAxisAlignment: MainAxisAlignment.end,
- children: [
- ElevatedButton(
- onPressed: () {
- ReportInfo.instance.onDiagnosticTap
- .emit(this, _inputTextInfo!.tag!.name ?? '');
- },
- child: const Text('选择词条'),
- ),
- const SizedBox(width: 10),
- ElevatedButton(
- onPressed: () {
- setState(() {
- _inputTextInfo!.text = '';
- });
- },
- child: const Text('撤销'),
- ),
- const SizedBox(width: 5),
- ],
- ),
- ],
- ],
- );
- }
- //onchange 事件
- void _onInputChanged(TextStyle _textStyle, String value) {
- if (_inputTextInfo != null) {
- _inputTextInfo!.text = value;
- }
- }
- @override
- void dispose() {
- _inputTextInfo!.onTextChange.dispose();
- _controller.dispose();
- super.dispose();
- }
- void _initDatas() {
- final inputText = widget.inputText;
- _controller.text = '';
- _focusNode.addListener(() {
- if (!_focusNode.hasFocus) {
- _onInputChanged(_textStyle!, _controller.text);
- }
- });
- final inputTextInfo = ReportInfo.instance.getElementInfo(inputText);
- _inputTextInfo = inputTextInfo as InputTextInfo;
- _inputTextInfo!.onTextChange.addListener((sender, e) {
- setState(() {
- _controller.text = e;
- _syncWidth(
- inputText,
- e,
- );
- });
- });
- if (_inputTextInfo!.isReadOnly!) {
- if (_inputTextInfo!.tag!.name == TagNames.RPHYSICIAN) {
- _controller.text = ReportInfo.instance.reporter ?? '';
- }
- }
- final fontColor = inputText.fontColor;
- if (fontColor != null) {
- _fontColor = Color.fromARGB(
- fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!);
- }
- final backgroundColor = inputText.background;
- if (backgroundColor != null) {
- _backgroundColor = Color.fromARGB(backgroundColor.a!, backgroundColor.r!,
- backgroundColor.g!, backgroundColor.b!);
- }
- ReportInfo.instance.onReloadFinsh.addListener((sender, e) {
- _initDatas();
- });
- setState(() {});
- }
- void _syncWidth(InputText inputText, String value) {
- final width = TextSizeConvert.getTextSize(value, _textStyle!).width + 15;
- if (inputText.tag!.name == TagNames.HOSPITALNAME) {
- if (_lineWidth < width) {
- setState(() {
- if (width < 480) {
- _lineWidth = width;
- _widthLock = true;
- }
- });
- } else if (_controller.text.isEmpty) {
- setState(() {
- //重置长度
- _lineWidth = PtToPxConverter.ptToPx(inputText.lineWidth);
- _widthLock = false;
- });
- } else if (TextSizeConvert.getTextSize(_controller.text, _textStyle!)
- .width >
- inputText.lineWidth!) {
- setState(() {
- if (width < 480) {
- _lineWidth = width;
- _widthLock = true;
- }
- });
- }
- }
- }
- void _initStyle() {
- final inputText = widget.inputText;
- _textStyle ??= TextStyle(
- fontSize: PtToPxConverter.ptToPx(inputText.fontSize),
- color: _fontColor,
- );
- _isReadOnly = inputText.isReadOnly!;
- //TODO(Loki):set FontName in TextField
- final fontName = inputText.fontName;
- //TODO(Loki):常规模板暂未设置fontStyles,后续再支持
- final fontStyles = inputText.fontStyles;
- _lineLength = inputText.lineLength ?? 7;
- if (!_widthLock) {
- _lineWidth = PtToPxConverter.ptToPx(inputText.lineWidth);
- }
- _widthLock = false;
- _textWrap = inputText.textWrap ?? false;
- _rowCount = _textWrap ? 6 : 1;
- if (_inputTextInfo != null && _inputTextInfo!.text.isNotEmpty) {
- final text = _inputTextInfo!.text;
- if (text.contains('\r\n')) {
- final rowList = text.split('\r\n');
- _rowCount = rowList.length > _rowCount ? rowList.length : _rowCount;
- }
- }
- _fontSize = PtToPxConverter.ptToPx(inputText.fontSize);
- _height = _fontSize > 30 ? 44 : 22;
- _allHeight = _textWrap
- ? _height * (_rowCount - 1)
- : _fontSize > 30
- ? (_height - 2)
- : _height;
- if (_inputTextInfo != null && _textWrap) {
- final height =
- TextSizeConvert.getTextSize(_controller.text, _textStyle!).height + 5;
- if (height > _allHeight) {
- _allHeight = height;
- }
- }
- }
- void _checkInputTextInfo() {
- try {
- final inputTextInfo =
- ReportInfo.instance.getElementInfo(widget.inputText);
- if (_inputTextInfo != inputTextInfo) {
- if (_inputTextInfo != null) _inputTextInfo!.onTextChange.dispose();
- _inputTextInfo = inputTextInfo as InputTextInfo;
- }
- if (!_inputTextInfo!.isListening!) {
- _initDatas();
- _inputTextInfo!.isListening = true;
- }
- if (_inputTextInfo!.text != _controller.text) {
- _controller.text = _inputTextInfo!.text;
- }
- } catch (e) {
- print(e);
- }
- }
- }
|