|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import 'dart:convert' as convert;
-
- 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/dialog/loading/loading.dart';
- import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
- import 'package:zhiying_base_widget/widgets/home/home_goods/bloc/home_goods_bloc.dart';
- import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_creater.dart';
- import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_item.dart';
- import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_item_single.dart';
- import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_model.dart';
- import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_style_model.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;
- dynamic _provider ;
- HomeGoodsStyleModel _style;
- bool _isFirstLoading = true;
-
- @override
- void initState() {
- _bloc = BlocProvider.of<HomeGoodsBloc>(context);
-
- String d = widget.data['data'];
- dynamic json = convert.jsonDecode(d);
- _style = HomeGoodsStyleModel.fromJson(Map<String, dynamic>.from(json));
-
- widget.eventBus.on<HomeGoodsHeaderEvent>().listen((data) {
- // if (!_isFirstLoading) {
- // // 老板说打开app的时候,第一次加载不要显示loading
- // Loading.show(context);
- // }
- _isFirstLoading = false;
- _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 StreamBuilder<List<HomeGoodsModel>>(
- stream: _bloc.outData,
- builder: (BuildContext context, AsyncSnapshot snapshot) {
- // if (snapshot.data == null) {
- // return SliverToBoxAdapter(
- // child: HomeGoodsSkeleton(),
- // );
- // }
- // Loading.dismiss();
- List<HomeGoodsModel> goods = snapshot.data;
- int column = int.tryParse(_style.listColumn ?? '1');
- column = column <= 0 ? 1 : column;
- int count = ((goods?.length ?? 0) / column).ceil();
- return SliverList(
- delegate: SliverChildBuilderDelegate((context, index) {
- if (column == 1) {
- return HomeGoodsItemSingle(
- goods[index],
- _style,
- data: widget.data,
- );
- } else {
- // return Container(color: Colors.red, height: 126,margin: EdgeInsets.all(10),);
- return Padding(
- padding: const EdgeInsets.only(left: 5, right: 5),
- child: Row(
- children: List.generate(column, (c) {
- int i = index * column + c;
- return Expanded(
- child: i < goods.length
- ? HomeGoodsItem(
- goods[i],
- _style,
- data: widget.data,
- )
- : Container(),
- );
- }).toList(),
- ),
- );
- }
- }, childCount: count),
- );
- },
- );
- }
- }
|