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

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