import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'bloc/search_bloc.dart'; import 'bloc/search_repository.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; class SearchPage2 extends StatelessWidget { final Map data; const SearchPage2(this.data); @override Widget build(BuildContext context) { return BlocProvider( create: (_) => SearchBloc(repository: SearchRepository())..add(SearchInitEvent(model: data)), child: SearchPage2Container(), ); } } class SearchPage2Container extends StatefulWidget { @override _SearchPage2ContainerState createState() => _SearchPage2ContainerState(); } class _SearchPage2ContainerState extends State { TabController _controller; @override void initState() { _controller = TabController(length: tabTitle.length, vsync: ScrollableState()); super.initState(); } @override void dispose() { _controller?.dispose(); super.dispose(); } @override Widget build(BuildContext context) { 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 Scaffold( backgroundColor: Colors.white, body: NestedScrollView( headerSliverBuilder: (context, bool){ return _createHeadWidget(context, datas); }, body: ListView( children: [ Text('sss') ], ), ), ); } /// 头部视图 List _createHeadWidget(BuildContext context, List> datas){ 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 < 2; 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], )); } list.add(_getTabbar()); return list; } var tabTitle = [ '页面1', '页面2', '页面3', ]; Widget _getTabbar(){ /// 悬停TabBar return SliverPersistentHeader( delegate: new SliverTabBarDelegate( TabBar( controller: _controller, tabs: tabTitle.map((f) => Tab(text: f)).toList(), indicatorColor: Colors.red, unselectedLabelColor: Colors.black, labelColor: Colors.red, ), ), pinned: true, ); } } class SliverTabBarDelegate extends SliverPersistentHeaderDelegate { final TabBar widget; const SliverTabBarDelegate(this.widget) : assert(widget != null); @override Widget build( BuildContext context, double shrinkOffset, bool overlapsContent) { return Container( color:Colors.white,child: this.widget); } @override bool shouldRebuild(SliverTabBarDelegate oldDelegate) { return false; } @override double get maxExtent => widget.preferredSize.height; @override double get minExtent => widget.preferredSize.height; }