search_bar.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'package:flutter/material.dart';
  2. import 'package:twong/router/index.dart';
  3. import 'package:twong/config/style.dart';
  4. import 'package:twong/utils/image_utils.dart';
  5. import 'package:twong/utils/index.dart';
  6. class SearchBar extends StatefulWidget implements PreferredSizeWidget {
  7. final Widget leading;
  8. final Size _size = DStyle.appBarHeight;
  9. final String search;
  10. final Function doSearch;
  11. final bool autoFocus;
  12. final bool enable;
  13. final Color color;
  14. final double elevation;
  15. final List<Widget> actions;
  16. SearchBar (this.leading, {Key key, this.actions, this.search, this.doSearch, this.autoFocus = false,
  17. this.enable = false, this.color = DColors.Main, this.elevation = 3 }): super(key: key);
  18. @override
  19. State<StatefulWidget> createState() {
  20. return SearchBarState();
  21. }
  22. @override
  23. Size get preferredSize => _size;
  24. }
  25. class SearchBarState extends State<SearchBar> {
  26. List<Widget> actions = new List<Widget>();
  27. onChange (String text) {
  28. if (text.length < 1) {
  29. setState(() {
  30. actions = <Widget>[
  31. getAction("取消", (){ Navigator.pop(context); }),
  32. ];
  33. });
  34. } else {
  35. setState(() {
  36. actions = <Widget>[
  37. getAction("搜索", (){ widget.doSearch(text); }),
  38. ];
  39. });
  40. }
  41. }
  42. Widget getAction(String text, Function onTap) {
  43. return GestureDetector(
  44. onTap: onTap,
  45. child: Container(
  46. width: 60.px,
  47. child: Center(
  48. child: Text(text, style: TextStyle(color: Colors.white, fontWeight: FontWeight.w300), textAlign: TextAlign.center),
  49. ),
  50. )
  51. );
  52. }
  53. List<Widget> getActions() {
  54. if (widget.actions != null) actions = widget.actions;
  55. if(actions.length > 0) return actions;
  56. if(widget.enable) {
  57. return <Widget>[getAction("取消", (){ Navigator.pop(context); })];
  58. } else {
  59. return null;
  60. }
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. return PreferredSize(
  65. preferredSize: widget.preferredSize,
  66. child: AppBar(
  67. titleSpacing: 0.0,
  68. elevation: widget.elevation,
  69. backgroundColor: widget.color,
  70. automaticallyImplyLeading: false,
  71. leading: widget.leading,
  72. title: GestureDetector(
  73. onTap: () {
  74. if (widget.enable) return;
  75. Navigator.pushNamed(context, RouteNames.search);
  76. },
  77. child: Container(
  78. height: 30,
  79. margin: EdgeInsets.only(right: 10.px, left: 10.px),
  80. child: TextField(
  81. onChanged: onChange,
  82. style: TextStyle(color: Color.fromARGB(212, 255, 255, 255), fontSize: 14.px, fontWeight: FontWeight.w300),
  83. enabled: widget.enable,
  84. autofocus: widget.autoFocus,
  85. onSubmitted: widget.doSearch,
  86. textInputAction: TextInputAction.search,
  87. decoration: InputDecoration(
  88. hintText: "美妆 护肤 面膜",
  89. hintStyle: TextStyle(color: Color.fromARGB(212, 255, 255, 255), fontSize: 14.px, fontWeight: FontWeight.w300),
  90. prefixIcon: Icon(IconFonts.search, color: Color.fromARGB(212, 255, 255, 255), size: 15),
  91. contentPadding: const EdgeInsets.symmetric(vertical: 4.0),
  92. border: OutlineInputBorder(
  93. borderSide: BorderSide.none,
  94. borderRadius: BorderRadius.circular(3)
  95. ),
  96. ),
  97. ),
  98. decoration: BoxDecoration(
  99. color: Color.fromARGB(90, 255, 255, 255),
  100. borderRadius: BorderRadius.circular(3),
  101. ),
  102. ),
  103. ),
  104. actions: getActions(),
  105. ),
  106. );
  107. }
  108. }