基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

196 lines
5.3 KiB

  1. import 'package:event_bus/event_bus.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:zhiying_base_widget/widgets/home_goods/bloc/home_goods_header_bloc.dart';
  5. import 'package:zhiying_base_widget/widgets/home_goods/home_goods_creater.dart';
  6. import 'package:zhiying_base_widget/widgets/home_goods/models/home_goods_header_model.dart';
  7. import 'package:zhiying_base_widget/widgets/home_goods/skeleton/home_goods_header_sk.dart';
  8. import 'package:zhiying_comm/util/base_bloc.dart';
  9. import 'package:zhiying_comm/zhiying_comm.dart';
  10. class HomeGoodsHeaderDelegate extends SliverPersistentHeaderDelegate {
  11. final Widget child;
  12. HomeGoodsHeaderDelegate(this.child) : super();
  13. @override
  14. Widget build(
  15. BuildContext context, double shrinkOffset, bool overlapsContent) {
  16. return child;
  17. }
  18. @override
  19. double get maxExtent => 64.0;
  20. @override
  21. double get minExtent => 64.0;
  22. @override
  23. bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) =>
  24. false; // 如果内容需要更新,设置为true
  25. }
  26. class HomeGoodsHeader extends StatelessWidget {
  27. final Map<String, dynamic> model;
  28. final EventBus eventBus;
  29. const HomeGoodsHeader(
  30. this.model,
  31. this.eventBus, {
  32. Key key,
  33. }) : super(key: key);
  34. @override
  35. Widget build(BuildContext context) {
  36. return BlocProvider<HomeGoodsHeaderBloc>(
  37. bloc: HomeGoodsHeaderBloc(),
  38. child: _HomeGoodsHeader(model, eventBus),
  39. );
  40. }
  41. }
  42. class _HomeGoodsHeader extends StatefulWidget {
  43. final Map<String, dynamic> model;
  44. final EventBus eventBus;
  45. _HomeGoodsHeader(this.model, this.eventBus, {Key key}) : super(key: key);
  46. @override
  47. _HomeGoodsHeaderState createState() => _HomeGoodsHeaderState();
  48. }
  49. class _HomeGoodsHeaderState extends State<_HomeGoodsHeader> {
  50. HomeGoodsHeaderBloc _bloc;
  51. @override
  52. void dispose() {
  53. _bloc.dispose();
  54. super.dispose();
  55. }
  56. @override
  57. void initState() {
  58. _bloc = BlocProvider.of<HomeGoodsHeaderBloc>(context);
  59. _bloc.loadData(widget.model['mod_id']);
  60. super.initState();
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. return StreamBuilder<List<HomeGoodsHeaderModel>>(
  65. stream: _bloc.outData,
  66. builder: (BuildContext context, AsyncSnapshot snapshot) {
  67. if (snapshot.data == null) {
  68. return HomeGoodsHeaderSkeleton();
  69. }
  70. return HomeGoodsHeaderContainer(snapshot.data, widget.eventBus);
  71. });
  72. }
  73. }
  74. class HomeGoodsHeaderContainer extends StatefulWidget {
  75. final List<HomeGoodsHeaderModel> headers;
  76. final EventBus eventBus;
  77. HomeGoodsHeaderContainer(this.headers, this.eventBus, {Key key})
  78. : super(key: key);
  79. @override
  80. _HomeGoodsHeaderContainerState createState() =>
  81. _HomeGoodsHeaderContainerState();
  82. }
  83. class _HomeGoodsHeaderContainerState extends State<HomeGoodsHeaderContainer>
  84. with TickerProviderStateMixin {
  85. List<Widget> _widgets = List();
  86. TabController _tabController;
  87. int _currentIndex = 0;
  88. @override
  89. void initState() {
  90. _tabController = TabController(length: widget.headers.length, vsync: this);
  91. if (widget.headers.first != null) {
  92. widget.eventBus.fire(HomeGoodsHeaderEvent(widget.headers.first.modName));
  93. }
  94. super.initState();
  95. }
  96. @override
  97. Widget build(BuildContext context) {
  98. _widgets.clear();
  99. for (int index = 0; index < widget.headers.length; index++) {
  100. _widgets.add(_HomeGoodsHeaderItem(
  101. widget.headers[index],
  102. isSelected: index == _currentIndex,
  103. ));
  104. }
  105. return Container(
  106. width: double.infinity,
  107. height: double.infinity,
  108. color: Color(0xfff9f9f9),
  109. child: TabBar(
  110. indicatorColor: Colors.transparent,
  111. controller: _tabController,
  112. isScrollable: true,
  113. tabs: _widgets,
  114. onTap: (index) {
  115. widget.eventBus
  116. .fire(HomeGoodsHeaderEvent(widget.headers[index].modName));
  117. setState(() {
  118. _currentIndex = index;
  119. });
  120. },
  121. ),
  122. );
  123. }
  124. }
  125. class _HomeGoodsHeaderItem extends StatelessWidget {
  126. final bool isSelected;
  127. final HomeGoodsHeaderModel model;
  128. const _HomeGoodsHeaderItem(this.model, {Key key, this.isSelected = false})
  129. : super(key: key);
  130. @override
  131. Widget build(BuildContext context) {
  132. return Container(
  133. // width: 60,
  134. // height: 54,
  135. child: Center(
  136. child: Column(
  137. mainAxisAlignment: MainAxisAlignment.center,
  138. children: <Widget>[
  139. Text(
  140. model.title,
  141. style: TextStyle(
  142. color: HexColor.fromHex(model.fontColor),
  143. fontSize: 17,
  144. fontWeight: FontWeight.bold),
  145. ),
  146. Container(
  147. margin: EdgeInsets.only(top: 4),
  148. padding: EdgeInsets.only(left: 6, right: 6, top: 2, bottom: 2),
  149. decoration: BoxDecoration(
  150. color: isSelected
  151. ? HexColor.fromHex(model.fontColor)
  152. : Colors.transparent,
  153. borderRadius: BorderRadius.circular(18)),
  154. child: Text(
  155. model.subtitle,
  156. style: TextStyle(
  157. fontSize: 10,
  158. color: isSelected ? Colors.white : Color(0xff999999)),
  159. textAlign: TextAlign.center,
  160. ),
  161. ),
  162. ],
  163. ),
  164. ));
  165. }
  166. }