search_app_bar.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import 'package:cs_resources/theme/app_colors_theme.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.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 = 33,
  21. this.searchBarBorderRadius = 17.25,
  22. this.searchBarBorderColor,
  23. this.textHintColor,
  24. this.searchBarBorder,
  25. this.margin,
  26. this.textColor = Colors.black,
  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? searchBarBorderColor;
  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: const EdgeInsets.only(left: 15, right: 11),
  93. margin: widget.margin ?? const EdgeInsets.only(right: 15),
  94. decoration: BoxDecoration(
  95. color: Colors.transparent,
  96. borderRadius: BorderRadius.circular(widget.searchBarBorderRadius), // 设置圆角
  97. border: widget.searchBarBorder ?? Border.all(
  98. color: widget.searchBarBorderColor ?? context.appColors.searchFiledBorder, // 边框颜色
  99. width: 1.0, // 边框宽度
  100. ),
  101. ),
  102. child: Row(
  103. mainAxisSize: MainAxisSize.max,
  104. crossAxisAlignment: CrossAxisAlignment.center,
  105. children: [
  106. //输入框
  107. IgnoreKeyboardDismiss(
  108. child: TextField(
  109. cursorColor: widget.textColor,
  110. cursorWidth: 1.5,
  111. autofocus: false,
  112. maxLines: 1,
  113. minLines: 1,
  114. // 是否自动获取焦点
  115. focusNode: _focusNode,
  116. // 焦点控制
  117. controller: _controller,
  118. // 与输入框交互控制器
  119. //装饰
  120. decoration: InputDecoration(
  121. isDense: true,
  122. //清除垂直方向的填充
  123. isCollapsed: true,
  124. //让文字垂直居中
  125. border: InputBorder.none,
  126. hintText: widget.hintText,
  127. hintStyle: TextStyle(
  128. color: widget.textHintColor ?? Color(0xFFAECAE5),
  129. fontSize: 15.0,
  130. fontWeight: FontWeight.w400,
  131. ),
  132. ),
  133. style: TextStyle(
  134. color: widget.textColor,
  135. fontSize: 15.0,
  136. fontWeight: FontWeight.w400,
  137. ),
  138. // 键盘动作右下角图标
  139. textInputAction: TextInputAction.search,
  140. textAlignVertical: TextAlignVertical.center,
  141. onSubmitted: (value) {
  142. widget.onSearch?.call(value);
  143. _unFocus();
  144. },
  145. //输入框完成触发
  146. onChanged: (value) {
  147. widget.value = value;
  148. widget.onChanged?.call(value);
  149. },
  150. ),
  151. ).expanded(),
  152. //搜索图标
  153. const MyAssetImage(Assets.baseLibSearchIcon, width: 15, height: 15).marginOnly(left: 10).onTap(() {
  154. widget.onSearch?.call(_controller?.text ?? "");
  155. _unFocus();
  156. }),
  157. ],
  158. ),
  159. );
  160. }
  161. @override
  162. void dispose() {
  163. _controller?.dispose();
  164. _focusNode?.dispose();
  165. super.dispose();
  166. }
  167. }