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

131 lines
4.2 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/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_base_widget/widgets/home/home_goods/skeleton/home_goods_sk.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. _provider = data.provider;
  54. _bloc.loadMore(_provider);
  55. });
  56. super.initState();
  57. }
  58. @override
  59. void didChangeDependencies() {
  60. super.didChangeDependencies();
  61. print('didChangeDependencies');
  62. bool isNeedLoadMore = Provider.of<MainPageNotifier>(context).scrollEnd;
  63. if (isNeedLoadMore && _provider != '') {
  64. print('HomeGoods loadmore...');
  65. _bloc.loadMore(_provider);
  66. }
  67. }
  68. @override
  69. Widget build(BuildContext context) {
  70. return Container(
  71. width: double.infinity,
  72. child: StreamBuilder<List<HomeGoodsModel>>(
  73. stream: _bloc.outData,
  74. builder: (BuildContext context, AsyncSnapshot snapshot) {
  75. if (snapshot.data == null) {
  76. return HomeGoodsSkeleton();
  77. }
  78. List<HomeGoodsModel> goods = snapshot.data;
  79. int column = int.tryParse(_style.listColumn);
  80. column = column <= 0 ? 1: column;
  81. int count = (goods.length / column).ceil();
  82. return ListView.builder(
  83. shrinkWrap: true,
  84. physics: NeverScrollableScrollPhysics(),
  85. itemCount: count,
  86. itemBuilder: (context, index) {
  87. if (column == 1) {
  88. return HomeGoodsItemSingle(
  89. goods[index],
  90. _style,
  91. data: widget.data,
  92. );
  93. } else {
  94. // return Container(color: Colors.red, height: 126,margin: EdgeInsets.all(10),);
  95. return Padding(
  96. padding: const EdgeInsets.only(left: 5, right: 5),
  97. child: Row(children: List.generate(column, (c) {
  98. int i = index * column + c;
  99. return Expanded
  100. (child: i < goods.length ? HomeGoodsItem(
  101. goods[i],
  102. _style,
  103. data: widget.data,
  104. ): Container(),);
  105. }).toList(),),
  106. );
  107. }
  108. });
  109. },
  110. ),
  111. );
  112. }
  113. }