search_bar.dart 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/scheduler.dart';
  3. import 'package:flutter/services.dart';
  4. import 'load_image.dart';
  5. /// 搜索页的AppBar
  6. class SearchBar extends StatefulWidget implements PreferredSizeWidget{
  7. const SearchBar({
  8. Key key,
  9. this.hintText: "",
  10. this.autofocus:true,
  11. this.onPressed,
  12. }): super(key: key);
  13. final String hintText;
  14. final bool autofocus;
  15. final Function(String) onPressed;
  16. @override
  17. _SearchBarState createState() => _SearchBarState();
  18. @override
  19. Size get preferredSize => Size.fromHeight(48.0);
  20. }
  21. class _SearchBarState extends State<SearchBar> {
  22. SystemUiOverlayStyle overlayStyle = SystemUiOverlayStyle.light;
  23. TextEditingController _controller = TextEditingController();
  24. // Color getColor(){
  25. // return overlayStyle == SystemUiOverlayStyle.light ? Colours.dark_text : Colours.text;
  26. // }
  27. @override
  28. Widget build(BuildContext context) {
  29. // bool isDark = ThemeUtils.isDark(context);
  30. // overlayStyle = isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;
  31. Color iconColor = Color(0xFF999999);
  32. return Material(
  33. // color: ThemeUtils.getBackgroundColor(context),
  34. child: Container(
  35. height: 32.0,
  36. decoration: BoxDecoration(
  37. color: Color(0xFFE8E9E8),
  38. borderRadius: BorderRadius.circular(17.5),
  39. ),
  40. child: TextField(
  41. key: const Key('srarch_text_field'),
  42. autofocus: widget.autofocus,
  43. controller: _controller,
  44. maxLines: 1,
  45. onChanged: widget.onPressed,
  46. decoration: InputDecoration(
  47. contentPadding: const EdgeInsets.only(top: 0.0, left: -8.0, right: -16.0, bottom: 14.0),
  48. border: InputBorder.none,
  49. icon: Padding(
  50. padding: const EdgeInsets.only(top: 8.0, bottom: 8.0, left: 10.0),
  51. child: LoadAssetImage("icon_search", color: iconColor,),
  52. ),
  53. hintText: widget.hintText,
  54. hintStyle: TextStyle(
  55. color: Color(0xFF999999)
  56. ),
  57. suffixIcon: InkWell(
  58. child: Padding(
  59. padding: const EdgeInsets.only(left: 16.0, top: 8.0, bottom: 8.0,right: 10),
  60. child: LoadAssetImage("icon_delete", color: iconColor),
  61. ),
  62. onTap: (){
  63. /// https://github.com/flutter/flutter/issues/35909
  64. SchedulerBinding.instance.addPostFrameCallback((_) {
  65. _controller.text = "";
  66. });
  67. },
  68. ),
  69. ),
  70. ),
  71. ),
  72. );
  73. }
  74. }