import 'package:baseproject/core/components/tab/custom_tab_widget.dart'; import 'package:baseproject/core/theme/custom_color.dart'; import 'package:flutter/material.dart'; typedef OnTabChanged = Function(int tabIndex); class CustomTab extends StatefulWidget { const CustomTab({ Key? key, required this.items, this.borderBottomColor, this.onTabChanged, this.indicatorColor, this.labelStyle, this.unselectedLabelStyle, this.tabItemPadding, this.tabPadding, this.isScrollable = false, this.isTopIcon = false, this.backgroundColor, this.indicatorPadding, this.tabBarIndicatorSize, this.alignment = Alignment.center, this.initialIndex = 0, this.labelColor, this.unselectedLabelColor, this.tabController, this.widgetBottom, }) : super(key: key); final List items; final Color? borderBottomColor; final Color? backgroundColor; final OnTabChanged? onTabChanged; final Color? indicatorColor; final TextStyle? labelStyle; final TextStyle? unselectedLabelStyle; final EdgeInsets? tabItemPadding; final EdgeInsets? tabPadding; final EdgeInsets? indicatorPadding; final bool isScrollable; final bool isTopIcon; final Alignment alignment; final TabBarIndicatorSize? tabBarIndicatorSize; final int initialIndex; final Color? labelColor; final Color? unselectedLabelColor; final TabController? tabController; final Widget? widgetBottom; @override _CustomTabState createState() => _CustomTabState(); } class _CustomTabState extends State with TickerProviderStateMixin, AutomaticKeepAliveClientMixin { Color get _borderBottomColor => widget.borderBottomColor ?? CustomColor.borderLight; int get initialIndex => widget.initialIndex; late TabController _tabController; List get _items => widget.items; EdgeInsets get _tabItemPadding => widget.tabItemPadding ?? const EdgeInsets.symmetric(vertical: 12); EdgeInsets get _indicatorPadding => widget.indicatorPadding ?? const EdgeInsets.symmetric(horizontal: 12); @override void initState() { super.initState(); _tabController = widget.tabController ?? TabController(length: _items.length, vsync: this, initialIndex: initialIndex); _tabController.addListener(() { if (widget.onTabChanged != null && !_tabController.indexIsChanging) { widget.onTabChanged!(_tabController.index); } }); } @override Widget build(BuildContext context) { super.build(context); return Column( children: [ CustomTabWidget( items: widget.items, borderBottomColor: widget.borderBottomColor, onTabChanged: widget.onTabChanged, indicatorColor: widget.indicatorColor, labelStyle: widget.labelStyle, unselectedLabelStyle: widget.unselectedLabelStyle, tabItemPadding: widget.tabItemPadding, tabPadding: widget.tabPadding, isScrollable: widget.isScrollable, isTopIcon: widget.isTopIcon, backgroundColor: widget.backgroundColor, indicatorPadding: widget.indicatorPadding, tabBarIndicatorSize: widget.tabBarIndicatorSize, alignment: widget.alignment, initialIndex: widget.initialIndex, labelColor: widget.labelColor, unselectedLabelColor: widget.unselectedLabelColor, tabController: _tabController, ), widget.widgetBottom ?? const SizedBox(), Expanded( child: TabBarView( controller: _tabController, children: widget.items.map((AppBarItemModel e) => e.widget ?? const SizedBox()).toList(), )), ], ); } @override bool get wantKeepAlive => true; } // ignore: must_be_immutable class AppBarItemModel { String title; int? itemCount; Color? iconColor; Color? borderColor; Color? iconActiveColor; Widget? widget; int? id; AppBarItemModel( this.title, { this.iconColor, this.borderColor, this.iconActiveColor, this.widget, this.id, this.itemCount, }); }