search_app_bar.dart 5.3 KB

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