|
- import 'package:flutter/material.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
- import 'package:flutter_bloc/flutter_bloc.dart';
-
- import 'bloc/search_bloc.dart';
- import 'bloc/search_repository.dart';
-
- ///
- /// 搜索页
- ///
- class SearchPage extends StatelessWidget {
- final Map<String, dynamic> data;
-
- SearchPage(this.data, {Key key}) : super(key: key);
-
- @override
- Widget build(BuildContext context) {
- return BlocProvider<SearchBloc>(
- create: (_) => SearchBloc(repository: SearchRepository())..add(SearchInitEvent(model: data)),
- child: SearchPageContianer(),
- );
- }
- }
-
- class SearchPageContianer extends StatefulWidget {
- @override
- _SearchPageContianerState createState() => _SearchPageContianerState();
- }
-
- class _SearchPageContianerState extends State<SearchPageContianer> {
- /// tab轮播
- TabController _tabController;
- ScrollController _controller = ScrollController();
-
- @override
- void initState() {
- _tabController = TabController(length:0, vsync: ScrollableState());
- super.initState();
- }
-
- @override
- void dispose() {
- _controller?.dispose();
- _tabController?.dispose();
- super.dispose();
- }
-
- @override
- Widget build(BuildContext context) {
- return MediaQuery.removePadding(
- removeTop: true,
- context: context,
- child: Container(
- width: double.infinity,
- child: BlocConsumer<SearchBloc, SearchState>(
- 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<Map<String, dynamic>> datas) {
- return Scaffold(
- backgroundColor: Colors.white,
- body: CustomScrollView(
- controller: _controller,
- slivers: _createContent(context, datas ?? []),
- ),
- );
- }
-
- List<Widget> _createContent(BuildContext context, List<Map<String, dynamic>> datas) {
- List<Widget> 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<String, dynamic>.from(datas[i]));
- print('item.modName ${item.modName}');
- list.addAll(WidgetFactory.create(
- item.modName,
- isSliver: true,
- model: datas[i],
- ));
- }
-
- list.add(SliverFillRemaining(
- child: Text('etstssss et'),
- ));
-
- return list;
- }
- }
|