fistextfield.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import 'package:fis_jsonrpc/services/authentication.m.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flyinsonolite/controls/defaultinputdecoration.dart';
  5. import 'package:flyinsonolite/infrastructure/scale.dart';
  6. import 'package:flyinsonolite/jsonrpc/fisLib/services/consultation.m.dart';
  7. import 'package:flyinsonolite/jsonrpc/fisLib/services/log.m.dart';
  8. import 'package:flyinsonolite/jsonrpc/jsonrpcclientforFISLib.dart';
  9. import 'package:flyinsonolite/infrastructure/logger.dart';
  10. import 'package:flyinsonolite/infrastructure/storage.dart';
  11. import 'package:get/get.dart';
  12. class FISTextField extends StatelessWidget {
  13. final Key? uniqueKey;
  14. final TextEditingController? controller;
  15. final List<TextInputFormatter>? inputFormatters;
  16. final bool readOnly;
  17. final String? placeHolder;
  18. final ValueChanged<String>? onChanged;
  19. final bool obscureText;
  20. final bool autofocus;
  21. final bool expands;
  22. final String errorText;
  23. final int? maxLines;
  24. final int? minLines;
  25. final TextAlignVertical? textAlignVertical;
  26. final TextInputType? keyboardType;
  27. final Widget? suffixIcon;
  28. final ValueChanged<String>? onSubmitted;
  29. final GestureTapCallback? onTap;
  30. final TapRegionCallback? onTapOutside;
  31. final Color? fillColor;
  32. final TextStyle? textStyle;
  33. final Color? borderColor;
  34. final Color? enabledBorderColor;
  35. final bool? enabled;
  36. final EdgeInsetsGeometry? contentPadding;
  37. final String route = Get.currentRoute;
  38. FISTextField({
  39. super.key,
  40. required this.uniqueKey,
  41. this.controller,
  42. this.inputFormatters,
  43. this.readOnly = false,
  44. this.placeHolder,
  45. this.onChanged,
  46. this.obscureText = false,
  47. this.autofocus = false,
  48. this.expands = false,
  49. this.errorText = '',
  50. this.maxLines = 1,
  51. this.minLines,
  52. this.textAlignVertical,
  53. this.keyboardType,
  54. this.suffixIcon,
  55. this.onSubmitted,
  56. this.onTap,
  57. this.onTapOutside,
  58. this.fillColor,
  59. this.textStyle,
  60. this.borderColor,
  61. this.enabledBorderColor,
  62. this.enabled,
  63. this.contentPadding,
  64. });
  65. @override
  66. Widget build(BuildContext context) {
  67. FocusNode focusNode = FocusNode();
  68. return TextField(
  69. key: uniqueKey,
  70. scrollPadding: EdgeInsets.all(20.0.s),
  71. cursorWidth: 2.s,
  72. cursorHeight: 25.s,
  73. enableInteractiveSelection: Storage.platform == Platform.Windows,
  74. controller: controller,
  75. inputFormatters: inputFormatters,
  76. style: textStyle ??
  77. Storage.currentTheme.normalTextStyle.copyWith(
  78. color: Colors.black,
  79. fontSize: Storage.currentTheme.normalTextStyle.fontSize!.s),
  80. readOnly: readOnly,
  81. onChanged: onChanged,
  82. obscureText: obscureText,
  83. autofocus: autofocus,
  84. focusNode: focusNode,
  85. maxLines: maxLines,
  86. minLines: minLines,
  87. expands: expands,
  88. textAlignVertical: textAlignVertical,
  89. keyboardType: keyboardType,
  90. onSubmitted: onSubmitted,
  91. enabled: enabled,
  92. decoration: DefaultInputDecoration(
  93. placeHolder: placeHolder,
  94. errorText: errorText,
  95. suffixIcon: suffixIcon,
  96. contentPadding: contentPadding ?? EdgeInsets.fromLTRB(12.s, 0, 12.s, 0),
  97. fillColor: enabled == false ? Colors.grey : fillColor,
  98. borderColor: borderColor,
  99. enabledBorderColor: enabledBorderColor,
  100. ),
  101. onTap: onTap ??
  102. () async {
  103. if (route != Get.currentRoute ||
  104. Storage.platform == Platform.Android) {
  105. return;
  106. }
  107. Offset position = const Offset(0, 0);
  108. Size screenSize = const Size(0, 0);
  109. double controlHeight = 0;
  110. if (uniqueKey is GlobalKey) {
  111. final RenderBox renderBox = (uniqueKey as GlobalKey)
  112. .currentContext!
  113. .findRenderObject() as RenderBox;
  114. position = renderBox.localToGlobal(Offset.zero);
  115. screenSize =
  116. MediaQuery.of((uniqueKey as GlobalKey).currentContext!).size;
  117. controlHeight = renderBox.size.height;
  118. }
  119. var positionY = position.dy + controlHeight > screenSize.height
  120. ? screenSize.height
  121. : position.dy + controlHeight;
  122. if (positionY < 0) {
  123. positionY = 0;
  124. }
  125. var fiscontrolPositionInfo = FISControlPositionInfo(
  126. isShowSoftwareKeyBoard: true,
  127. screenHeight: screenSize.height,
  128. screenWidth: screenSize.width,
  129. positionX: position.dx,
  130. positionY: positionY);
  131. await operateSoftKeyBoard(fiscontrolPositionInfo);
  132. },
  133. onTapOutside: onTapOutside ??
  134. (e) async {
  135. if (route != Get.currentRoute) {
  136. return;
  137. }
  138. focusNode.unfocus();
  139. if (Storage.platform == Platform.Android) {
  140. return;
  141. }
  142. var fiscontrolPositionInfo = FISControlPositionInfo(
  143. isShowSoftwareKeyBoard: false,
  144. screenHeight: 0,
  145. screenWidth: 0,
  146. positionX: 0,
  147. positionY: 0);
  148. await operateSoftKeyBoard(fiscontrolPositionInfo);
  149. },
  150. );
  151. }
  152. }
  153. Future operateSoftKeyBoard(
  154. FISControlPositionInfo fisControlPositionInfo) async {
  155. try {
  156. if (fisControlPositionInfo.isShowSoftwareKeyBoard ||
  157. Storage.isShowSoftKeyBoard !=
  158. fisControlPositionInfo
  159. .isShowSoftwareKeyBoard) //因为OnTapOutSide一个页面如果有多个TextField控件会都触发
  160. {
  161. Storage.isShowSoftKeyBoard =
  162. fisControlPositionInfo.isShowSoftwareKeyBoard;
  163. await jsonRpcProxyForFISLib.consultation
  164. .operateSoftKeyBoard(fisControlPositionInfo);
  165. }
  166. } catch (ex) {
  167. await loggerAsyn(
  168. FISDeviceLogCategory.Error, "OperateSoftKeyBoard error: $ex");
  169. }
  170. }