searchtextfield.dart 5.7 KB

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