import 'package:event_bus/event_bus.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:zhiying_base_widget/widgets/home_goods/bloc/home_goods_header_bloc.dart'; import 'package:zhiying_base_widget/widgets/home_goods/home_goods_creater.dart'; import 'package:zhiying_base_widget/widgets/home_goods/models/home_goods_header_model.dart'; import 'package:zhiying_base_widget/widgets/home_goods/skeleton/home_goods_header_sk.dart'; import 'package:zhiying_comm/util/base_bloc.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; class HomeGoodsHeaderDelegate extends SliverPersistentHeaderDelegate { final Widget child; HomeGoodsHeaderDelegate(this.child) : super(); @override Widget build( BuildContext context, double shrinkOffset, bool overlapsContent) { return child; } @override double get maxExtent => 64.0; @override double get minExtent => 64.0; @override bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) => false; // 如果内容需要更新,设置为true } class HomeGoodsHeader extends StatelessWidget { final Map model; final EventBus eventBus; const HomeGoodsHeader( this.model, this.eventBus, { Key key, }) : super(key: key); @override Widget build(BuildContext context) { return BlocProvider( bloc: HomeGoodsHeaderBloc(), child: _HomeGoodsHeader(model, eventBus), ); } } class _HomeGoodsHeader extends StatefulWidget { final Map model; final EventBus eventBus; _HomeGoodsHeader(this.model, this.eventBus, {Key key}) : super(key: key); @override _HomeGoodsHeaderState createState() => _HomeGoodsHeaderState(); } class _HomeGoodsHeaderState extends State<_HomeGoodsHeader> { HomeGoodsHeaderBloc _bloc; @override void dispose() { _bloc.dispose(); super.dispose(); } @override void initState() { _bloc = BlocProvider.of(context); _bloc.loadData(widget.model['mod_id']); super.initState(); } @override Widget build(BuildContext context) { return StreamBuilder>( stream: _bloc.outData, builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.data == null) { return HomeGoodsHeaderSkeleton(); } return HomeGoodsHeaderContainer(snapshot.data, widget.eventBus); }); } } class HomeGoodsHeaderContainer extends StatefulWidget { final List headers; final EventBus eventBus; HomeGoodsHeaderContainer(this.headers, this.eventBus, {Key key}) : super(key: key); @override _HomeGoodsHeaderContainerState createState() => _HomeGoodsHeaderContainerState(); } class _HomeGoodsHeaderContainerState extends State with TickerProviderStateMixin { List _widgets = List(); TabController _tabController; int _currentIndex = 0; @override void initState() { _tabController = TabController(length: widget.headers.length, vsync: this); if (widget.headers.first != null) { widget.eventBus.fire(HomeGoodsHeaderEvent(widget.headers.first.modName)); } super.initState(); } @override Widget build(BuildContext context) { _widgets.clear(); for (int index = 0; index < widget.headers.length; index++) { _widgets.add(_HomeGoodsHeaderItem( widget.headers[index], isSelected: index == _currentIndex, )); } return Container( width: double.infinity, height: double.infinity, color: Color(0xfff9f9f9), child: TabBar( indicatorColor: Colors.transparent, controller: _tabController, isScrollable: true, tabs: _widgets, onTap: (index) { widget.eventBus .fire(HomeGoodsHeaderEvent(widget.headers[index].modName)); setState(() { _currentIndex = index; }); }, ), ); } } class _HomeGoodsHeaderItem extends StatelessWidget { final bool isSelected; final HomeGoodsHeaderModel model; const _HomeGoodsHeaderItem(this.model, {Key key, this.isSelected = false}) : super(key: key); @override Widget build(BuildContext context) { return Container( // width: 60, // height: 54, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( model.title, style: TextStyle( color: HexColor.fromHex(model.fontColor), fontSize: 17, fontWeight: FontWeight.bold), ), Container( margin: EdgeInsets.only(top: 4), padding: EdgeInsets.only(left: 6, right: 6, top: 2, bottom: 2), decoration: BoxDecoration( color: isSelected ? HexColor.fromHex(model.fontColor) : Colors.transparent, borderRadius: BorderRadius.circular(18)), child: Text( model.subtitle, style: TextStyle( fontSize: 10, color: isSelected ? Colors.white : Color(0xff999999)), textAlign: TextAlign.center, ), ), ], ), )); } }