search_app_bar.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. decoration: InputDecoration(
  84. contentPadding: const EdgeInsets.only(
  85. top: -3.0,
  86. left: -8.0,
  87. right: -16.0,
  88. bottom: 14.0),
  89. border: InputBorder.none,
  90. icon: Padding(
  91. padding: const EdgeInsets.only(left: 10.0),
  92. child: Icon(
  93. Iconfont.sousuo,
  94. size: 16,
  95. color: Color(0xff6A6A76),
  96. ),
  97. ),
  98. hintText: widget.hintText,
  99. hintStyle: TextStyle(color: Color(0xFF999999)),
  100. suffixIcon: InkWell(
  101. child: Padding(
  102. padding: const EdgeInsets.only(
  103. left: 16.0, top: 8.0, bottom: 8.0, right: 10),
  104. child: LoadAssetImage("icon_delete",
  105. color: iconColor),
  106. ),
  107. onTap: () {
  108. /// https://github.com/flutter/flutter/issues/35909
  109. SchedulerBinding.instance
  110. .addPostFrameCallback((_) {
  111. _controller.text = "";
  112. });
  113. },
  114. ),
  115. ),
  116. ),
  117. ),
  118. ),
  119. SizedBox(
  120. width: 20,
  121. ),
  122. GestureDetector(
  123. onTap: () {
  124. widget.onPressed(_controller.text);
  125. FocusScope.of(context).requestFocus(FocusNode());
  126. },
  127. child: Text("搜索",
  128. style: TextStyle(
  129. fontSize: 14,
  130. color: Color(0xff343434),
  131. )),
  132. ),
  133. ],
  134. )),
  135. ),
  136. ),
  137. );
  138. }
  139. }