sliver_bar.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import 'package:flutter/material.dart';
  2. import 'package:twong/utils/index.dart';
  3. class SliverBar extends StatefulWidget {
  4. final List<Widget> menus;
  5. final Function(int, {bool up}) onChange;
  6. SliverBar({ Key key, this.menus, this.onChange } ): super(key: key);
  7. @override
  8. State<StatefulWidget> createState() {
  9. return _SliverBarState();
  10. }
  11. }
  12. class _SliverBarState extends State<SliverBar> {
  13. TextStyle _style = TextStyle(fontSize: 14, color: Colors.grey,);
  14. TextStyle _selectStyle = TextStyle(fontSize: 14, color: Colors.black,);
  15. int _curIdx = 0;
  16. bool _isSort = true;
  17. changeType (int type) {
  18. if(_curIdx == type) {
  19. if(type == 0) return;
  20. setState(() {
  21. _isSort = !_isSort;
  22. });
  23. } else {
  24. _isSort = true;
  25. }
  26. setState(() {
  27. _curIdx = type;
  28. });
  29. if(widget.onChange != null) {
  30. widget.onChange(_curIdx, up: _isSort);
  31. }
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return SliverAppBar(
  36. pinned: true,
  37. centerTitle: true,
  38. backgroundColor: Colors.white,
  39. shape: Border(
  40. bottom: BorderSide(color: Colors.grey, width: 0.3.px)
  41. ),
  42. shadowColor: Colors.black,
  43. automaticallyImplyLeading: false,
  44. title: Row(
  45. mainAxisAlignment: MainAxisAlignment.center,
  46. children: <Widget>[
  47. Expanded(child: GestureDetector(
  48. onTap: () => changeType(0),
  49. child: Text("默认", textAlign: TextAlign.center,
  50. style: _curIdx == 0 ? _selectStyle : _style,)),
  51. ),
  52. Expanded(child: GestureDetector(
  53. onTap: () => changeType(1),
  54. child: Row(
  55. mainAxisAlignment: MainAxisAlignment.center,
  56. children: [
  57. Text("销量", textAlign: TextAlign.center,
  58. style: _curIdx == 1 ? _selectStyle : _style,),
  59. _curIdx == 1
  60. ? Icon(_isSort ? Icons.expand_more : Icons.expand_less,
  61. color: Colors.black)
  62. : Icon(Icons.unfold_more, color: Colors.grey)
  63. ])
  64. )),
  65. Expanded(child: GestureDetector(
  66. onTap: () => changeType(2),
  67. child: Row(
  68. mainAxisAlignment: MainAxisAlignment.center,
  69. children: [
  70. Text("价格", textAlign: TextAlign.center,
  71. style: _curIdx == 2 ? _selectStyle : _style,),
  72. _curIdx == 2
  73. ? Icon(_isSort ? Icons.expand_less : Icons.expand_more,
  74. color: Colors.black)
  75. : Icon(Icons.unfold_more, color: Colors.grey)
  76. ]),
  77. )),
  78. ]),
  79. bottom: PreferredSize(
  80. child: Container(),
  81. preferredSize: Size.fromHeight(- 8.px),
  82. ),
  83. );
  84. }
  85. }