search_app_bar.dart 5.4 KB

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