search_bar.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 SearchBar extends StatefulWidget implements PreferredSizeWidget{
  9. const SearchBar({
  10. Key key,
  11. this.hintText: "",
  12. this.autofocus:true,
  13. this.onPressed,
  14. }): super(key: key);
  15. final String hintText;
  16. final bool autofocus;
  17. final Function(String) onPressed;
  18. @override
  19. _SearchBarState createState() => _SearchBarState();
  20. @override
  21. Size get preferredSize => Size.fromHeight(48.0);
  22. }
  23. class _SearchBarState extends State<SearchBar> {
  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 Material(
  35. // color: ThemeUtils.getBackgroundColor(context),
  36. child: Container(
  37. height: 32.0,
  38. decoration: BoxDecoration(
  39. color: Color(0xFFE8E9E8),
  40. borderRadius: BorderRadius.circular(17.5),
  41. ),
  42. child: TextField(
  43. key: const Key('srarch_text_field'),
  44. autofocus: widget.autofocus,
  45. controller: _controller,
  46. maxLines: 1,
  47. onChanged: widget.onPressed,
  48. decoration: InputDecoration(
  49. contentPadding: const EdgeInsets.only(top: 0.0, left: -8.0, right: -16.0, bottom: 14.0),
  50. border: InputBorder.none,
  51. icon: Padding(
  52. padding: const EdgeInsets.only(top: 8.0, bottom: 8.0, left: 10.0),
  53. child: LoadAssetImage("icon_search", color: iconColor,),
  54. ),
  55. hintText: widget.hintText,
  56. hintStyle: TextStyle(
  57. color: Color(0xFF999999)
  58. ),
  59. suffixIcon: InkWell(
  60. child: Padding(
  61. padding: const EdgeInsets.only(left: 16.0, top: 8.0, bottom: 8.0,right: 10),
  62. child: LoadAssetImage("icon_delete", color: iconColor),
  63. ),
  64. onTap: (){
  65. /// https://github.com/flutter/flutter/issues/35909
  66. SchedulerBinding.instance.addPostFrameCallback((_) {
  67. _controller.text = "";
  68. });
  69. },
  70. ),
  71. ),
  72. ),
  73. ),
  74. );
  75. }
  76. }