search_app_bar.dart 5.7 KB

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