search_app_bar.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
  4. import 'package:get/get.dart';
  5. import 'package:cs_resources/generated/assets.dart';
  6. import 'package:widgets/ext/ex_widget.dart';
  7. import '../utils/dark_theme_util.dart';
  8. import 'my_load_image.dart';
  9. import 'package:cs_resources/constants/color_constants.dart';
  10. class SearchAppBar extends StatefulWidget {
  11. SearchAppBar({
  12. Key? key,
  13. this.value,
  14. this.autoFocus = false,
  15. this.focusNode,
  16. this.controller,
  17. this.actions = const [],
  18. this.hintText,
  19. this.onTap,
  20. this.searchBarHeight = 35,
  21. this.searchBarBorderRadius = 17.25,
  22. this.searchBarBgColor,
  23. this.textHintColor,
  24. this.searchBarBorder,
  25. this.margin,
  26. this.textColor = Colors.white,
  27. this.textFontSize = 15.0,
  28. this.onChanged,
  29. this.onSearch,
  30. }) : super(key: key);
  31. final bool? autoFocus;
  32. final FocusNode? focusNode;
  33. final TextEditingController? controller;
  34. // 搜索框右侧组件
  35. final List<Widget> actions;
  36. // 输入框提示文字
  37. final String? hintText;
  38. // 输入框点击回调
  39. final VoidCallback? onTap;
  40. double searchBarHeight;
  41. double searchBarBorderRadius;
  42. EdgeInsetsGeometry? margin;
  43. Color? searchBarBgColor;
  44. Color? textHintColor;
  45. Color textColor;
  46. double? textFontSize;
  47. BoxBorder? searchBarBorder;
  48. String? value;
  49. // 输入框内容改变
  50. final ValueChanged<String>? onChanged;
  51. // 点击键盘搜索
  52. final ValueChanged<String>? onSearch;
  53. @override
  54. _SearchAppBarState createState() => _SearchAppBarState();
  55. }
  56. class _SearchAppBarState extends State<SearchAppBar> {
  57. TextEditingController? _controller;
  58. FocusNode? _focusNode;
  59. bool get isFocus => _focusNode?.hasFocus ?? false; //是否获取焦点
  60. bool get isTextEmpty => _controller?.text.isEmpty ?? false; //输入框是否为空
  61. bool get isActionEmpty => widget.actions.isEmpty; // 右边布局是否为空
  62. bool isShowCancel = false;
  63. @override
  64. void initState() {
  65. _controller = widget.controller ?? TextEditingController();
  66. _focusNode = widget.focusNode ?? FocusNode();
  67. //赋值要显示的文本
  68. if (widget.value != null) _controller?.text = widget.value ?? "";
  69. super.initState();
  70. }
  71. // 更新输入框的文本内容
  72. void updateValue(String newValue) {
  73. setState(() {
  74. _controller?.text = newValue;
  75. });
  76. }
  77. //失去焦点和隐藏软键盘
  78. void _unFocus() {
  79. setState(() {
  80. _focusNode?.unfocus();
  81. });
  82. }
  83. @override
  84. void didUpdateWidget(covariant SearchAppBar oldWidget) {
  85. super.didUpdateWidget(oldWidget);
  86. if (widget.value != oldWidget.value) {
  87. _controller?.text = widget.value ?? "";
  88. }
  89. }
  90. @override
  91. Widget build(BuildContext context) {
  92. return Container(
  93. height: widget.searchBarHeight,
  94. padding: EdgeInsets.only(left: 15, right: 11),
  95. margin: widget.margin ?? EdgeInsets.only(right: 15),
  96. decoration: BoxDecoration(
  97. color: widget.searchBarBgColor ?? Color(0xFF4DCFF6).withOpacity(0.2), // 设置// 背景颜色和不透明度
  98. borderRadius: BorderRadius.circular(widget.searchBarBorderRadius), // 设置圆角
  99. border: widget.searchBarBorder,
  100. ),
  101. child: Row(
  102. mainAxisSize: MainAxisSize.max,
  103. crossAxisAlignment: CrossAxisAlignment.center,
  104. children: [
  105. //输入框
  106. IgnoreKeyboardDismiss(
  107. child: TextField(
  108. cursorColor: widget.textColor,
  109. cursorWidth: 1.5,
  110. autofocus: false,
  111. maxLines: 1,
  112. minLines: 1,
  113. // 是否自动获取焦点
  114. focusNode: _focusNode,
  115. // 焦点控制
  116. controller: _controller,
  117. // 与输入框交互控制器
  118. //装饰
  119. decoration: InputDecoration(
  120. isDense: true,
  121. //清除垂直方向的填充
  122. isCollapsed: true,
  123. //让文字垂直居中
  124. border: InputBorder.none,
  125. hintText: widget.hintText,
  126. hintStyle: TextStyle(
  127. color: widget.textHintColor ?? Color(0xFFAECAE5),
  128. fontSize: widget.textFontSize?? 15.0,
  129. fontWeight: FontWeight.w400,
  130. ),
  131. ),
  132. style: TextStyle(
  133. color: widget.textColor,
  134. fontSize: 15.0,
  135. fontWeight: FontWeight.w400,
  136. ),
  137. // 键盘动作右下角图标
  138. textInputAction: TextInputAction.search,
  139. textAlignVertical: TextAlignVertical.center,
  140. onSubmitted: (value) {
  141. widget.onSearch?.call(value);
  142. _unFocus();
  143. },
  144. //输入框完成触发
  145. onChanged: (value) {
  146. widget.value = value;
  147. widget.onChanged?.call(value);
  148. },
  149. ),
  150. ).expanded(),
  151. //搜索图标
  152. MyAssetImage(Assets.cptJobSearchIcon, width: 15, height: 15).marginOnly(left: 10).onTap(() {
  153. widget.onSearch?.call(_controller?.text ?? "");
  154. _unFocus();
  155. }),
  156. ],
  157. ),
  158. );
  159. }
  160. @override
  161. void dispose() {
  162. _controller?.dispose();
  163. _focusNode?.dispose();
  164. super.dispose();
  165. }
  166. }