|
- import 'package:event_bus/event_bus.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
- import 'package:zhiying_base_widget/widgets/home_goods/bloc/home_goods_bloc.dart';
- import 'package:zhiying_base_widget/widgets/home_goods/home_goods_creater.dart';
- import 'package:zhiying_base_widget/widgets/home_goods/home_goods_item.dart';
- import 'package:zhiying_base_widget/widgets/home_goods/models/home_goods_model.dart';
- import 'package:zhiying_base_widget/widgets/home_goods/skeleton/home_goods_sk.dart';
- import 'package:zhiying_comm/util/base_bloc.dart';
-
- class HomeGoods extends StatefulWidget {
- final Map<String, dynamic> data;
- final EventBus eventBus;
-
- HomeGoods(this.data, {Key key, this.eventBus}) : super(key: key);
-
- @override
- _HomeGoodsState createState() => _HomeGoodsState();
- }
-
- class _HomeGoodsState extends State<HomeGoods> {
- @override
- void initState() {
- super.initState();
- }
-
- @override
- Widget build(BuildContext context) {
- return BlocProvider<HomeGoodsBloc>(
- bloc: HomeGoodsBloc(),
- child: _HomeGoodsContainer(widget.data, widget.eventBus),
- );
- }
- }
-
- class _HomeGoodsContainer extends StatefulWidget {
- final Map<String, dynamic> data;
- final EventBus eventBus;
-
- _HomeGoodsContainer(this.data, this.eventBus, {Key key}) : super(key: key);
-
- @override
- _HomeGoodsContainerState createState() => _HomeGoodsContainerState();
- }
-
- class _HomeGoodsContainerState extends State<_HomeGoodsContainer> {
- HomeGoodsBloc _bloc;
- String _provider = '';
-
- @override
- void initState() {
- _bloc = BlocProvider.of<HomeGoodsBloc>(context);
- widget.eventBus.on<HomeGoodsHeaderEvent>().listen((data) {
- _provider = data.provider;
- _bloc.loadMore(_provider);
- });
-
- super.initState();
- }
-
- @override
- void didChangeDependencies() {
- super.didChangeDependencies();
- print('didChangeDependencies');
- bool isNeedLoadMore = Provider.of<MainPageNotifier>(context).scrollEnd;
- if (isNeedLoadMore && _provider != '') {
- print('HomeGoods loadmore...');
- _bloc.loadMore(_provider);
- }
- }
-
- @override
- Widget build(BuildContext context) {
- return Container(
- width: double.infinity,
- child: StreamBuilder<List<HomeGoodsModel>>(
- stream: _bloc.outData,
- builder: (BuildContext context, AsyncSnapshot snapshot) {
- if (snapshot.data == null) {
- return HomeGoodsSkeleton();
- }
-
- List<HomeGoodsModel> goods = snapshot.data;
- return ListView.builder(
- shrinkWrap: true,
- physics: NeverScrollableScrollPhysics(),
- itemCount: goods.length,
- itemBuilder: (context, index) {
- return HomeGoodsItem(
- goods[index],
- _bloc.style,
- data: widget.data,
- shop: _bloc.storeName,
- );
- });
- },
- ),
- );
- }
- }
|