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

home_goods.dart 4.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. dynamic _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.page=1;
  61. _bloc.loadMore(_provider);
  62. });
  63. super.initState();
  64. }
  65. @override
  66. void didChangeDependencies() {
  67. super.didChangeDependencies();
  68. print('didChangeDependencies');
  69. bool isNeedLoadMore = Provider.of<MainPageNotifier>(context).scrollEnd;
  70. if (isNeedLoadMore && _provider != '') {
  71. print('HomeGoods loadmore...');
  72. _bloc.loadMore(_provider);
  73. }
  74. }
  75. @override
  76. Widget build(BuildContext context) {
  77. return StreamBuilder<List<HomeGoodsModel>>(
  78. stream: _bloc.outData,
  79. builder: (BuildContext context, AsyncSnapshot snapshot) {
  80. // if (snapshot.data == null) {
  81. // return SliverToBoxAdapter(
  82. // child: HomeGoodsSkeleton(),
  83. // );
  84. // }
  85. // Loading.dismiss();
  86. List<HomeGoodsModel> goods = snapshot.data;
  87. int column = int.tryParse(_style.listColumn ?? '1');
  88. column = column <= 0 ? 1 : column;
  89. int count = ((goods?.length ?? 0) / column).ceil();
  90. return SliverList(
  91. delegate: SliverChildBuilderDelegate((context, index) {
  92. if (column == 1) {
  93. return HomeGoodsItemSingle(
  94. goods[index],
  95. _style,
  96. data: widget.data,
  97. );
  98. } else {
  99. // return Container(color: Colors.red, height: 126,margin: EdgeInsets.all(10),);
  100. return Padding(
  101. padding: const EdgeInsets.only(left: 5, right: 5),
  102. child: Row(
  103. children: List.generate(column, (c) {
  104. int i = index * column + c;
  105. return Expanded(
  106. child: i < goods.length
  107. ? HomeGoodsItem(
  108. goods[i],
  109. _style,
  110. data: widget.data,
  111. )
  112. : Container(),
  113. );
  114. }).toList(),
  115. ),
  116. );
  117. }
  118. }, childCount: count),
  119. );
  120. },
  121. );
  122. }
  123. }