FahimHoque

feed_slider_container.dart

Dec 24th, 2021
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.90 KB | None | 0 0
  1. class FeedSliderContainer extends StatefulWidget {
  2.   const FeedSliderContainer({
  3.     Key? key,
  4.   }) : super(key: key);
  5.  
  6.   @override
  7.   _FeedSliderContainerState createState() => _FeedSliderContainerState();
  8. }
  9.  
  10. class _FeedSliderContainerState extends State<FeedSliderContainer>
  11.     with SingleTickerProviderStateMixin {
  12.   /*
  13.     FOR TESTING PURPOSES ONLY, DO NOT USE IN PRODUCTION
  14.   */
  15.   List<String> categories = ['All', 'Women', 'Men', 'Kids', 'Gadgets', 'Shoes'];
  16.  
  17.   late TabController _tabController;
  18.  
  19.   GridContainerData? _products;
  20.   GridContainerData _getProductData() {
  21.     return productGridContainerData;
  22.   }
  23.  
  24.   @override
  25.   void initState() {
  26.     super.initState();
  27.     _products = _getProductData();
  28.     _tabController = TabController(vsync: this, length: categories.length);
  29.   }
  30.  
  31.   @override
  32.   void dispose() {
  33.     _tabController.dispose();
  34.     super.dispose();
  35.   }
  36.  
  37.   @override
  38.   Widget build(BuildContext context) {
  39.     return Container(
  40.       child: Builder(
  41.         builder: (BuildContext context) {
  42.           // final TabController tabController = DefaultTabController.of(context);
  43.           // tabController.addListener(() {
  44.           //   if (!tabController.indexIsChanging) {
  45.           //     int index = tabController.index;
  46.           //     setState(() {});
  47.           //   }
  48.           // });
  49.           _tabController.addListener(() {
  50.             if (!_tabController.indexIsChanging) {
  51.               int index = _tabController.index;
  52.               setState(() {});
  53.             }
  54.           });
  55.           return Column(
  56.             children: <Widget>[
  57.               getTabBar(context, _tabController),
  58.               Expanded(
  59.                 child: Container(
  60.                   child: TabBarView(
  61.                     controller: _tabController,
  62.                     children: [
  63.                       _buildProductFeed(),
  64.                       _buildProductFeed(),
  65.                       _buildProductFeed(),
  66.                       _buildProductFeed(),
  67.                       _buildProductFeed(),
  68.                       _buildProductFeed(),
  69.                     ],
  70.                   ),
  71.                 ),
  72.               ),
  73.             ],
  74.           );
  75.         },
  76.       ),
  77.     );
  78.   }
  79.  
  80.   List<Widget> getTabs(TabController tabController) {
  81.     return categories.map((category) {
  82.       return Tab(
  83.         icon: Container(
  84.           width: MediaQuery.of(context).size.width * 0.3,
  85.           height: MediaQuery.of(context).size.height * 0.06,
  86.           decoration: tabController.index == categories.indexOf(category)
  87.               ? BoxDecoration(
  88.                   color: Colors.white,
  89.                   borderRadius: BorderRadius.circular(10),
  90.                   boxShadow: const [
  91.                     BoxShadow(
  92.                       color: Colors.black12,
  93.                       blurRadius: 10,
  94.                       offset: Offset(3, 7),
  95.                     )
  96.                   ],
  97.                 )
  98.               : const BoxDecoration(),
  99.           child: Row(
  100.             mainAxisAlignment: MainAxisAlignment.center,
  101.             children: <Widget>[
  102.               Center(
  103.                 child: Text(
  104.                   category,
  105.                 ),
  106.               ),
  107.               const SizedBox(width: 5),
  108.             ],
  109.           ),
  110.         ),
  111.       );
  112.     }).toList();
  113.   }
  114.  
  115.   Widget getTabBar(BuildContext context, TabController tabController) {
  116.     return TabBar(
  117.       tabs: getTabs(tabController),
  118.       labelColor: CustomColors.primary,
  119.       unselectedLabelColor: CustomColors.secondary.withOpacity(0.5),
  120.       indicatorColor: Colors.transparent,
  121.       isScrollable: true,
  122.       padding: const EdgeInsets.all(3),
  123.       controller: tabController,
  124.     );
  125.   }
  126.  
  127.   Widget _buildProductFeed() {
  128.     return Container(
  129.       padding: EdgeInsets.all(10),
  130.       child: GridContainer(
  131.         data: _products,
  132.         isScrollable: true,
  133.       ),
  134.     );
  135.   }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment