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

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