|
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import 'package:pull_to_refresh/pull_to_refresh.dart';
- import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/intellect_create.dart';
- import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/intellect_search_goods_dialog.dart';
- import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/intellect_search_no_goods_dialog.dart';
- import 'package:zhiying_base_widget/pages/main_page/main_page_bloc.dart';
- import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart';
- import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
- import 'package:zhiying_base_widget/utils/contants.dart';
- import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart';
- import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.dart';
- import 'package:zhiying_comm/util/base_bloc.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
-
- class MainPage extends StatefulWidget {
- final Map<String, dynamic> data;
-
- MainPage(
- this.data, {
- Key key,
- }) : super(key: key);
-
- @override
- _MainPageState createState() => _MainPageState();
- }
-
- class _MainPageState extends State<MainPage> {
- @override
- Widget build(BuildContext context) {
- return MultiProvider(
- providers: [
- ChangeNotifierProvider.value(value: MainPageNotifier()),
- ChangeNotifierProvider.value(value: MainPageBgNotifier()),
- ],
- child: BlocProvider<MainPageBloc>(
- bloc: MainPageBloc(),
- child: _MainPageContainer(widget.data),
- ),
- );
- }
- }
-
- class _MainPageContainer extends StatefulWidget {
- final Map<String, dynamic> data;
-
- _MainPageContainer(this.data, {Key key}) : super(key: key);
-
- @override
- _MainPageContainerState createState() => _MainPageContainerState();
- }
-
- class _MainPageContainerState extends State<_MainPageContainer>
- with WidgetsBindingObserver {
- bool _isEnded = false;
- ScrollController _controller = ScrollController();
- MainPageBloc _bloc;
- RefreshController _refreshController =
- RefreshController(initialRefresh: false);
-
- Widget _floatWidget;
-
- void _onLoading() async {
- // await Future.delayed(Duration(milliseconds: 1000));
- // if (mounted) setState(() {});
- // _refreshController.loadComplete();
- }
-
- @override
- void didChangeAppLifecycleState(AppLifecycleState state) async {
- ///智能粘贴板
- // IntellectCreate.checkAndCreate(state, context);
- super.didChangeAppLifecycleState(state);
- }
-
- @override
- void dispose() {
- _controller.dispose();
- WidgetsBinding.instance.removeObserver(this);
- super.dispose();
- }
-
- @override
- void initState() {
- _bloc = BlocProvider.of<MainPageBloc>(context);
- if (widget.data.containsKey('skip_identifier')) {
- _bloc.loadData(widget.data['skip_identifier']);
- }
-
- _controller.addListener(() {
- // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
- if (_controller.offset >= _controller.position.maxScrollExtent &&
- !_isEnded) {
- // 滑动到底部
- _isEnded = true;
- Provider.of<MainPageNotifier>(context, listen: false).loadMore();
- } else if (_controller.offset < _controller.position.maxScrollExtent &&
- _isEnded) {
- _isEnded = false;
- Provider.of<MainPageNotifier>(context, listen: false).reset();
- }
- });
- ///监听app生命周期变化
- // WidgetsBinding.instance.addObserver(this);
- IntellectCreate.checkAndCreateFirst(context);
- super.initState();
- }
-
- @override
- Widget build(BuildContext context) {
- return StreamBuilder<List<Map<String, dynamic>>>(
- stream: _bloc.outData,
- builder: (BuildContext context, AsyncSnapshot snapshot) {
- List widgets = _createContent(context, snapshot.data ?? []);
- return Scaffold(
- backgroundColor: Color(0xfff9f9f9),
- floatingActionButton: _floatWidget,
- floatingActionButtonLocation:
- FloatingActionButtonLocation.centerDocked,
- body: MediaQuery.removePadding(
- removeTop: true,
- context: context,
- child: SmartRefresher(
- enablePullDown: true,
- enablePullUp: false,
- header: WaterDropHeader(),
- controller: _refreshController,
- onLoading: _onLoading,
- child: Container(
- width: double.infinity,
- child: Stack(
- fit: StackFit.passthrough,
- children: <Widget>[
- MineHeaderBgWidget(
- controller: _controller,
- ),
- CustomScrollView(
- controller: _controller,
- slivers: widgets,
- )
- ],
- ),
- ),
- ),
- ),
- );
- });
- }
-
- List<Widget> _createContent(
- BuildContext context, List<Map<String, dynamic>> datas) {
- List<Widget> list = List();
- for (int i = 0; i < datas.length; i++) {
- WidgetModel item =
- WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
-
- print('item.modName ${item.modName}');
- if (item.modName == 'index_taobao_auth_tip') {
- _floatWidget = HomeAuth(datas[i]);
- continue;
- }
- list.addAll(WidgetFactory.create(
- item.modName,
- isSliver: true,
- model: datas[i],
- ));
- }
- if (list.length <= 0) {
- list.add(SliverToBoxAdapter(
- child: Container(
- height: 200,
- child: Center(
- child: Text('暂时无数据哦~'),
- ),
- ),
- ));
- }
- return list;
- }
- }
|