基础组件库
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.
 
 
 
 
 

139 lines
4.4 KiB

  1. import 'dart:convert' as convert;
  2. import 'package:event_bus/event_bus.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:provider/provider.dart';
  6. import 'package:zhiying_base_widget/dialog/loading/loading.dart';
  7. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
  8. import 'package:zhiying_base_widget/widgets/home/home_goods/bloc/home_goods_bloc.dart';
  9. import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_creater.dart';
  10. import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_item.dart';
  11. import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_item_single.dart';
  12. import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_model.dart';
  13. import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_style_model.dart';
  14. import 'package:zhiying_comm/util/base_bloc.dart';
  15. class HomeGoods extends StatefulWidget {
  16. final Map<String, dynamic> data;
  17. final EventBus eventBus;
  18. HomeGoods(this.data, {Key key, this.eventBus}) : super(key: key);
  19. @override
  20. _HomeGoodsState createState() => _HomeGoodsState();
  21. }
  22. class _HomeGoodsState extends State<HomeGoods> {
  23. @override
  24. void initState() {
  25. super.initState();
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return BlocProvider<HomeGoodsBloc>(
  30. bloc: HomeGoodsBloc(),
  31. child: _HomeGoodsContainer(widget.data, widget.eventBus),
  32. );
  33. }
  34. }
  35. class _HomeGoodsContainer extends StatefulWidget {
  36. final Map<String, dynamic> data;
  37. final EventBus eventBus;
  38. _HomeGoodsContainer(this.data, this.eventBus, {Key key}) : super(key: key);
  39. @override
  40. _HomeGoodsContainerState createState() => _HomeGoodsContainerState();
  41. }
  42. class _HomeGoodsContainerState extends State<_HomeGoodsContainer> {
  43. HomeGoodsBloc _bloc;
  44. String _provider = '';
  45. HomeGoodsStyleModel _style;
  46. bool _isFirstLoading = true;
  47. @override
  48. void initState() {
  49. _bloc = BlocProvider.of<HomeGoodsBloc>(context);
  50. String d = widget.data['data'];
  51. dynamic json = convert.jsonDecode(d);
  52. _style = HomeGoodsStyleModel.fromJson(Map<String, dynamic>.from(json));
  53. widget.eventBus.on<HomeGoodsHeaderEvent>().listen((data) {
  54. if (!_isFirstLoading) {
  55. // 老板说打开app的时候,第一次加载不要显示loading
  56. Loading.show(context);
  57. }
  58. _isFirstLoading = false;
  59. _provider = data.provider;
  60. _bloc.loadMore(_provider);
  61. });
  62. super.initState();
  63. }
  64. @override
  65. void didChangeDependencies() {
  66. super.didChangeDependencies();
  67. print('didChangeDependencies');
  68. bool isNeedLoadMore = Provider.of<MainPageNotifier>(context).scrollEnd;
  69. if (isNeedLoadMore && _provider != '') {
  70. print('HomeGoods loadmore...');
  71. _bloc.loadMore(_provider);
  72. }
  73. }
  74. @override
  75. Widget build(BuildContext context) {
  76. return StreamBuilder<List<HomeGoodsModel>>(
  77. stream: _bloc.outData,
  78. builder: (BuildContext context, AsyncSnapshot snapshot) {
  79. // if (snapshot.data == null) {
  80. // return SliverToBoxAdapter(
  81. // child: HomeGoodsSkeleton(),
  82. // );
  83. // }
  84. Loading.dismiss();
  85. List<HomeGoodsModel> goods = snapshot.data;
  86. int column = int.tryParse(_style.listColumn);
  87. column = column <= 0 ? 1 : column;
  88. int count = ((goods?.length ?? 0) / column).ceil();
  89. return SliverList(
  90. delegate: SliverChildBuilderDelegate((context, index) {
  91. if (column == 1) {
  92. return HomeGoodsItemSingle(
  93. goods[index],
  94. _style,
  95. data: widget.data,
  96. );
  97. } else {
  98. // return Container(color: Colors.red, height: 126,margin: EdgeInsets.all(10),);
  99. return Padding(
  100. padding: const EdgeInsets.only(left: 5, right: 5),
  101. child: Row(
  102. children: List.generate(column, (c) {
  103. int i = index * column + c;
  104. return Expanded(
  105. child: i < goods.length
  106. ? HomeGoodsItem(
  107. goods[i],
  108. _style,
  109. data: widget.data,
  110. )
  111. : Container(),
  112. );
  113. }).toList(),
  114. ),
  115. );
  116. }
  117. }, childCount: count),
  118. );
  119. },
  120. );
  121. }
  122. }