search_app_bar.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/scheduler.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:liftmanager/res/iconfont.dart';
  5. import 'package:liftmanager/res/resources.dart';
  6. import 'package:liftmanager/utils/theme_utils.dart';
  7. import 'load_image.dart';
  8. /// 搜索页的AppBar
  9. class SearchAppBar extends StatefulWidget implements PreferredSizeWidget {
  10. const SearchAppBar({
  11. Key key,
  12. this.hintText: "",
  13. this.searchText,
  14. this.onPressed,
  15. this.autoFocus = true,
  16. }) : super(key: key);
  17. final String hintText;
  18. final searchText;
  19. final Function(String) onPressed;
  20. final bool autoFocus;
  21. @override
  22. _SearchAppBarState createState() => _SearchAppBarState();
  23. @override
  24. Size get preferredSize => Size.fromHeight(48.0);
  25. }
  26. class _SearchAppBarState extends State<SearchAppBar> {
  27. SystemUiOverlayStyle overlayStyle = SystemUiOverlayStyle.light;
  28. TextEditingController _controller = TextEditingController();
  29. Color getColor() {
  30. return overlayStyle == SystemUiOverlayStyle.light
  31. ? Colours.dark_text
  32. : Colours.text;
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. double width = MediaQuery.of(context).size.width;
  37. bool isDark = ThemeUtils.isDark(context);
  38. overlayStyle =
  39. isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;
  40. Color iconColor = Color(0xFF999999);
  41. _controller.text = widget.searchText ?? '';
  42. return AnnotatedRegion<SystemUiOverlayStyle>(
  43. value: overlayStyle,
  44. child: Material(
  45. color: ThemeUtils.getBackgroundColor(context),
  46. child: SafeArea(
  47. child: Container(
  48. padding: EdgeInsets.symmetric(horizontal: 10),
  49. child: Row(
  50. // mainAxisAlignment: MainAxisAlignment.spaceAround,
  51. crossAxisAlignment: CrossAxisAlignment.center,
  52. children: <Widget>[
  53. if (Navigator.canPop(context)) ...[
  54. InkWell(
  55. onTap: () {
  56. FocusScope.of(context).unfocus();
  57. Navigator.maybePop(context);
  58. },
  59. child: Container(
  60. child: Icon(
  61. Iconfont.fanhui,
  62. size: 17,
  63. ),
  64. ),
  65. ),
  66. SizedBox(
  67. width: 20,
  68. ),
  69. ],
  70. Expanded(
  71. child: Container(
  72. height: 30.0,
  73. decoration: BoxDecoration(
  74. color: ThemeUtils.getDialogTextFieldColor(context),
  75. borderRadius: BorderRadius.circular(17.5),
  76. ),
  77. child: TextField(
  78. key: const Key('srarch_text_field'),
  79. autofocus: widget.autoFocus,
  80. controller: _controller,
  81. maxLines: 1,
  82. style: TextStyle(fontSize: 14),
  83. // textInputAction:'',
  84. onSubmitted: (value) {
  85. widget.onPressed(_controller.text);
  86. FocusScope.of(context).requestFocus(FocusNode());
  87. },
  88. decoration: InputDecoration(
  89. contentPadding: const EdgeInsets.only(
  90. top: -3.0,
  91. left: -8.0,
  92. right: -16.0,
  93. bottom: 14.0),
  94. border: InputBorder.none,
  95. icon: Padding(
  96. padding: const EdgeInsets.only(left: 10.0),
  97. child: Icon(
  98. Iconfont.sousuo,
  99. size: 16,
  100. color: Color(0xff6A6A76),
  101. ),
  102. ),
  103. hintText: widget.hintText,
  104. hintStyle: TextStyle(color: Color(0xFF999999)),
  105. suffixIcon: InkWell(
  106. child: Padding(
  107. padding: const EdgeInsets.only(
  108. left: 16.0, top: 8.0, bottom: 8.0, right: 10),
  109. child: LoadAssetImage("icon_delete",
  110. color: iconColor),
  111. ),
  112. onTap: () {
  113. /// https://github.com/flutter/flutter/issues/35909
  114. SchedulerBinding.instance
  115. .addPostFrameCallback((_) {
  116. _controller.text = "";
  117. });
  118. },
  119. ),
  120. ),
  121. ),
  122. ),
  123. ),
  124. SizedBox(
  125. width: 20,
  126. ),
  127. GestureDetector(
  128. onTap: () {
  129. widget.onPressed(_controller.text);
  130. FocusScope.of(context).requestFocus(FocusNode());
  131. },
  132. child: Text("搜索",
  133. style: TextStyle(
  134. fontSize: 14,
  135. color: Color(0xff343434),
  136. )),
  137. ),
  138. ],
  139. )),
  140. ),
  141. ),
  142. );
  143. }
  144. }