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

102 lines
3.0 KiB

  1. import 'package:event_bus/event_bus.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
  6. import 'package:zhiying_base_widget/widgets/home/home_goods/bloc/home_goods_bloc.dart';
  7. import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_creater.dart';
  8. import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_item.dart';
  9. import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_model.dart';
  10. import 'package:zhiying_base_widget/widgets/home/home_goods/skeleton/home_goods_sk.dart';
  11. import 'package:zhiying_comm/util/base_bloc.dart';
  12. class HomeGoods extends StatefulWidget {
  13. final Map<String, dynamic> data;
  14. final EventBus eventBus;
  15. HomeGoods(this.data, {Key key, this.eventBus}) : super(key: key);
  16. @override
  17. _HomeGoodsState createState() => _HomeGoodsState();
  18. }
  19. class _HomeGoodsState extends State<HomeGoods> {
  20. @override
  21. void initState() {
  22. super.initState();
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. return BlocProvider<HomeGoodsBloc>(
  27. bloc: HomeGoodsBloc(),
  28. child: _HomeGoodsContainer(widget.data, widget.eventBus),
  29. );
  30. }
  31. }
  32. class _HomeGoodsContainer extends StatefulWidget {
  33. final Map<String, dynamic> data;
  34. final EventBus eventBus;
  35. _HomeGoodsContainer(this.data, this.eventBus, {Key key}) : super(key: key);
  36. @override
  37. _HomeGoodsContainerState createState() => _HomeGoodsContainerState();
  38. }
  39. class _HomeGoodsContainerState extends State<_HomeGoodsContainer> {
  40. HomeGoodsBloc _bloc;
  41. String _provider = '';
  42. @override
  43. void initState() {
  44. _bloc = BlocProvider.of<HomeGoodsBloc>(context);
  45. widget.eventBus.on<HomeGoodsHeaderEvent>().listen((data) {
  46. _provider = data.provider;
  47. _bloc.loadMore(_provider);
  48. });
  49. super.initState();
  50. }
  51. @override
  52. void didChangeDependencies() {
  53. super.didChangeDependencies();
  54. print('didChangeDependencies');
  55. bool isNeedLoadMore = Provider.of<MainPageNotifier>(context).scrollEnd;
  56. if (isNeedLoadMore && _provider != '') {
  57. print('HomeGoods loadmore...');
  58. _bloc.loadMore(_provider);
  59. }
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. return Container(
  64. width: double.infinity,
  65. child: StreamBuilder<List<HomeGoodsModel>>(
  66. stream: _bloc.outData,
  67. builder: (BuildContext context, AsyncSnapshot snapshot) {
  68. if (snapshot.data == null) {
  69. return HomeGoodsSkeleton();
  70. }
  71. List<HomeGoodsModel> goods = snapshot.data;
  72. return ListView.builder(
  73. shrinkWrap: true,
  74. physics: NeverScrollableScrollPhysics(),
  75. itemCount: goods.length,
  76. itemBuilder: (context, index) {
  77. return HomeGoodsItem(
  78. goods[index],
  79. _bloc.style,
  80. data: widget.data,
  81. );
  82. });
  83. },
  84. ),
  85. );
  86. }
  87. }