app_search_bar.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:liftmanager/res/colors.dart';
  4. import 'package:liftmanager/res/dimens.dart';
  5. import 'package:liftmanager/res/gaps.dart';
  6. import 'package:liftmanager/res/iconfont.dart';
  7. import 'package:liftmanager/widgets/search_app_bar.dart' as search;
  8. /// 自定义AppBar
  9. class SearchAppBar extends StatelessWidget implements PreferredSizeWidget {
  10. const SearchAppBar(
  11. {Key key,
  12. this.backgroundColor,
  13. this.title: "",
  14. this.centerTitle: "",
  15. this.actions,
  16. this.searchWidth,
  17. this.onPressed,
  18. this.isBack: true})
  19. : super(key: key);
  20. final Color backgroundColor;
  21. final String title;
  22. final double searchWidth;
  23. final String centerTitle;
  24. final List<Widget> actions;
  25. final VoidCallback onPressed;
  26. final bool isBack;
  27. @override
  28. Widget build(BuildContext context) {
  29. return AnnotatedRegion<SystemUiOverlayStyle>(
  30. value: SystemUiOverlayStyle.dark,
  31. child: Container(
  32. decoration: BoxDecoration(
  33. color: backgroundColor,
  34. ),
  35. child: SafeArea(
  36. child: Stack(
  37. alignment: Alignment.centerLeft,
  38. children: <Widget>[
  39. Column(
  40. mainAxisAlignment: MainAxisAlignment.center,
  41. children: <Widget>[
  42. Container(
  43. alignment: centerTitle.isEmpty
  44. ? Alignment.centerLeft
  45. : Alignment.center,
  46. width: double.infinity,
  47. child: Text(title.isEmpty ? centerTitle : title,
  48. style: TextStyle(
  49. fontSize: Dimens.font_sp18,
  50. color: Colours.dark_text,
  51. )),
  52. padding: const EdgeInsets.symmetric(horizontal: 48.0),
  53. )
  54. ],
  55. ),
  56. isBack
  57. ? IconButton(
  58. onPressed: () {
  59. FocusScope.of(context).unfocus();
  60. Navigator.maybePop(context);
  61. },
  62. tooltip: 'Back',
  63. padding: const EdgeInsets.all(12.0),
  64. icon: Icon(
  65. Iconfont.fanhui,
  66. size: 17,
  67. ),
  68. )
  69. : Gaps.empty,
  70. GestureDetector(
  71. onTap: () {
  72. // NavigatorUtils.push(context, SearchRouter.searchPage);
  73. // 搜索跳转改为到回调里面触发
  74. onPressed();
  75. },
  76. child: Container(
  77. height: 35,
  78. width: searchWidth == null ? 400 : searchWidth,
  79. margin: const EdgeInsets.only(left: 45.0, right: 15),
  80. decoration: BoxDecoration(
  81. color: Color(0xFFF5F8FA),
  82. borderRadius: BorderRadius.circular(17.5)),
  83. child: Row(
  84. crossAxisAlignment: CrossAxisAlignment.center,
  85. mainAxisAlignment: MainAxisAlignment.start,
  86. children: <Widget>[
  87. SizedBox(
  88. width: 10,
  89. ),
  90. Icon(
  91. Iconfont.sousuo,
  92. size: 16,
  93. color: Color(0xff6A6A76),
  94. ),
  95. SizedBox(
  96. width: 10,
  97. ),
  98. Text(
  99. "搜索你需要的关键词",
  100. style: TextStyle(color: Colours.text_gray_c),
  101. )
  102. ],
  103. ),
  104. )),
  105. Positioned(
  106. right: 0.0,
  107. child: Theme(
  108. data: Theme.of(context).copyWith(
  109. buttonTheme: ButtonThemeData(
  110. padding: const EdgeInsets.symmetric(horizontal: 16.0),
  111. minWidth: 60.0,
  112. )),
  113. child: rightActions()),
  114. ),
  115. ],
  116. ),
  117. ),
  118. ),
  119. );
  120. }
  121. Widget rightActions() {
  122. if (this.actions != null && this.actions.isNotEmpty) {
  123. return Row(
  124. children: this.actions,
  125. );
  126. } else {
  127. return Container();
  128. }
  129. }
  130. @override
  131. Size get preferredSize => Size.fromHeight(48.0);
  132. }
  133. class SearchAppBar2 extends StatefulWidget implements PreferredSizeWidget {
  134. SearchAppBar2({
  135. Key key,
  136. this.backgroundColor,
  137. this.title: "",
  138. this.isBtn: false,
  139. this.centerTitle: "",
  140. this.actions,
  141. this.searchWidth,
  142. this.onPressed,
  143. this.isBack: true,
  144. this.hintText: "搜索您需要的关键词",
  145. }) : super(key: key);
  146. final Color backgroundColor;
  147. final String title;
  148. final double searchWidth;
  149. final String centerTitle;
  150. final List<Widget> actions;
  151. final Function(String) onPressed;
  152. final bool isBack;
  153. final String hintText;
  154. final bool isBtn;
  155. @override
  156. _SearchAppBar2State createState() => _SearchAppBar2State();
  157. @override
  158. Size get preferredSize => Size.fromHeight(48.0);
  159. }
  160. class _SearchAppBar2State extends State<SearchAppBar2> {
  161. int searchBarType = 1;
  162. String searchText = '';
  163. @override
  164. initState() {
  165. super.initState();
  166. }
  167. @override
  168. Widget build(BuildContext context) {
  169. return AnnotatedRegion<SystemUiOverlayStyle>(
  170. value: SystemUiOverlayStyle.light,
  171. child: Container(
  172. // decoration: Decoration(,
  173. // color:widget.backgroundColor,
  174. child: searchBarType == 1
  175. ? SearchAppBar(
  176. backgroundColor: widget.backgroundColor,
  177. actions: widget.actions,
  178. searchWidth: widget.searchWidth,
  179. isBack: widget.isBack,
  180. onPressed: () {
  181. setState(() {
  182. searchBarType = 2;
  183. });
  184. },
  185. )
  186. : search.SearchAppBar(
  187. hintText: widget.hintText,
  188. searchText: searchText,
  189. onPressed: (text) {
  190. searchText = text;
  191. setState(() {});
  192. widget.onPressed(text);
  193. },
  194. ),
  195. ),
  196. );
  197. }
  198. }