import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:zhiying_base_widget/pages/search_page/notifier/search_tag_notifier.dart'; import 'package:zhiying_base_widget/pages/search_think_page/bloc/search_think_bloc.dart'; import 'package:zhiying_base_widget/pages/search_think_page/bloc/search_think_repository.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:provider/provider.dart'; import 'bloc/search_bloc.dart'; import 'bloc/search_repository.dart'; /// /// 搜索页 /// class SearchPage extends StatelessWidget { final Map data; SearchPage(this.data, {Key key}) : super(key: key); @override Widget build(BuildContext context) { return MultiProvider( providers: [ChangeNotifierProvider.value(value: SearchTagNotifier())], child: MultiProvider( providers: [ /// 搜索页面的bloc BlocProvider( create: (_) => SearchBloc(repository: SearchRepository())..add(SearchInitEvent(model: data)), ), /// 输入框联想的bloc BlocProvider( create: (_) => SearchThinkBloc(SearchThinkRepository()), ), ], child: SearchPageContianer( data: data, ), ), // child: BlocProvider( // create: (_) => SearchBloc(repository: SearchRepository())..add(SearchInitEvent(model: data)), // child: SearchPageContianer(), // ), ); } } class SearchPageContianer extends StatefulWidget { final Map data; const SearchPageContianer({Key key, this.data}) : super(key: key); @override _SearchPageContianerState createState() => _SearchPageContianerState(); } class _SearchPageContianerState extends State { /// tab轮播 TabController _tabController; @override void initState() { _tabController = TabController(length: 6, vsync: ScrollableState()); super.initState(); } @override void dispose() { _tabController?.dispose(); super.dispose(); } @override Widget build(BuildContext context) { //设置搜索bar应该选择哪个type的item Logger.log("数据2: " + json.encode(widget.data)); Logger.log("类型: " + widget.data['default_pvd']); if(!EmptyUtil.isEmpty(widget.data['default_pvd'])){ Provider.of(context, listen: false).setType(widget.data['default_pvd']); } return MediaQuery.removePadding( removeTop: true, context: context, child: Container( width: double.infinity, child: BlocConsumer( listener: (BuildContext context, SearchState state) { if (state is SearchErrorState) { print('数据加载出错'); } }, buildWhen: (previous, current) { /// 数据加载出错不进行build if (current is SearchErrorState) { return false; } /// 搜索成功,跳转结果页面 if (current is SearchSubmitSuccessState) { return false; } /// 搜索失败,不进行build if (current is SearchSubmitErrorState) { return false; } return true; }, builder: (context, state) { print('currente state = $state'); if (state is SearchLoadedState) { return _getMainWidget(state?.model); } /// 骨架屏幕 return _getMainWidget(null); }, ), ), ); } /// 主视图 Widget _getMainWidget(List> datas) { return AnnotatedRegion( value: SystemUiOverlayStyle.dark, child: Scaffold( backgroundColor: Colors.white, body: Listener( onPointerDown: (down) => RouterUtil.hideKeyboard(context), child: Column( children: _createContentWidget(datas), ), ), ), ); } List _createContentWidget(List> datas) { List list = []; int length = datas?.length ?? 0; if (length <= 0) { /// 骨架屏幕? list.add(Container( height: 200, width: double.infinity, color: Colors.white, // child: Center( // child: Text('暂时无数据哦~'), // ), )); } else { for (int i = 0; i < datas.length; i++) { WidgetModel item = WidgetModel.fromJson(Map.from(datas[i])); print('item.modName ${item.modName}'); list.addAll(WidgetFactory.create( item.modName, isSliver: false, model: datas[i], )); } } return list; } // List _createContent(BuildContext context, List> datas) { // List list = List(); // // int length = datas?.length ?? 0; // // if (length <= 0) { // list.add(SliverToBoxAdapter( // child: Container( // height: 200, // child: Center( // child: Text('暂时无数据哦~'), // ), // ), // )); // return list; // } // // for (int i = 0; i < 3; i++) { // WidgetModel item = WidgetModel.fromJson(Map.from(datas[i])); // print('item.modName ${item.modName}'); // list.addAll(WidgetFactory.create( // item.modName, // isSliver: true, // model: datas[i], // )); // } // // return list; // } }