search_app_bar.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/scheduler.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:liftmanager/res/resources.dart';
  5. import 'package:liftmanager/utils/theme_utils.dart';
  6. import 'load_image.dart';
  7. /// 搜索页的AppBar
  8. class SearchAppBar extends StatefulWidget implements PreferredSizeWidget{
  9. const SearchAppBar({
  10. Key key,
  11. this.hintText: "",
  12. this.backImg: "assets/images/icon_back.png",
  13. this.onPressed,
  14. }): super(key: key);
  15. final String backImg;
  16. final String hintText;
  17. final Function(String) onPressed;
  18. @override
  19. _SearchAppBarState createState() => _SearchAppBarState();
  20. @override
  21. Size get preferredSize => Size.fromHeight(48.0);
  22. }
  23. class _SearchAppBarState extends State<SearchAppBar> {
  24. SystemUiOverlayStyle overlayStyle = SystemUiOverlayStyle.light;
  25. TextEditingController _controller = TextEditingController();
  26. Color getColor(){
  27. return overlayStyle == SystemUiOverlayStyle.light ? Colours.dark_text : Colours.text;
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. bool isDark = ThemeUtils.isDark(context);
  32. overlayStyle = isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;
  33. Color iconColor = Color(0xFF999999);
  34. return AnnotatedRegion<SystemUiOverlayStyle>(
  35. value: overlayStyle,
  36. child: Material(
  37. color: ThemeUtils.getBackgroundColor(context),
  38. child: SafeArea(
  39. child: Container(
  40. child: Row(
  41. children: <Widget>[
  42. SizedBox(
  43. width: 48.0,
  44. height: 48.0,
  45. child: InkWell(
  46. onTap: (){
  47. FocusScope.of(context).unfocus();
  48. Navigator.maybePop(context);
  49. },
  50. borderRadius: BorderRadius.circular(24.0),
  51. child: Padding(
  52. key: const Key('search_back'),
  53. padding: const EdgeInsets.all(12.0),
  54. child: Image.asset(
  55. widget.backImg,
  56. color: getColor(),
  57. ),
  58. ),
  59. ),
  60. ),
  61. Expanded(
  62. child: Container(
  63. height: 32.0,
  64. decoration: BoxDecoration(
  65. color: ThemeUtils.getDialogTextFieldColor(context),
  66. borderRadius: BorderRadius.circular(17.5),
  67. ),
  68. child: TextField(
  69. key: const Key('srarch_text_field'),
  70. autofocus: true,
  71. controller: _controller,
  72. maxLines: 1,
  73. decoration: InputDecoration(
  74. contentPadding: const EdgeInsets.only(top: 0.0, left: -8.0, right: -16.0, bottom: 14.0),
  75. border: InputBorder.none,
  76. icon: Padding(
  77. padding: const EdgeInsets.only(top: 8.0, bottom: 8.0, left: 10.0),
  78. child: LoadAssetImage("icon_search", color: iconColor,),
  79. ),
  80. hintText: widget.hintText,
  81. hintStyle: TextStyle(
  82. color: Color(0xFF999999)
  83. ),
  84. suffixIcon: InkWell(
  85. child: Padding(
  86. padding: const EdgeInsets.only(left: 16.0, top: 8.0, bottom: 8.0,right: 10),
  87. child: LoadAssetImage("icon_delete", color: iconColor),
  88. ),
  89. onTap: (){
  90. /// https://github.com/flutter/flutter/issues/35909
  91. SchedulerBinding.instance.addPostFrameCallback((_) {
  92. _controller.text = "";
  93. });
  94. },
  95. ),
  96. ),
  97. ),
  98. ),
  99. ),
  100. Gaps.hGap8,
  101. Theme(
  102. data: Theme.of(context).copyWith(
  103. buttonTheme: ButtonThemeData(
  104. padding: const EdgeInsets.symmetric(horizontal: 8.0),
  105. height: 32.0,
  106. minWidth: 44.0,
  107. materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, // 距顶部距离为0
  108. shape: RoundedRectangleBorder(
  109. borderRadius: BorderRadius.circular(4.0),
  110. )
  111. ),
  112. ),
  113. child: FlatButton(
  114. textColor: isDark ? Colours.dark_button_text : Colors.white,
  115. color: isDark ? Colours.dark_app_main : Colours.app_main,
  116. onPressed:(){
  117. widget.onPressed(_controller.text);
  118. FocusScope.of(context)
  119. .requestFocus(FocusNode());
  120. },
  121. child: Text("搜索", style: TextStyle(fontSize: Dimens.font_sp14)),
  122. ),
  123. ),
  124. Gaps.hGap16,
  125. ],
  126. )
  127. ),
  128. ),
  129. ),
  130. );
  131. }
  132. }