Advertisement
harmonyV

CarouselWidget

Jul 11th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.84 KB | None | 0 0
  1. class CarouselWidget extends StatefulWidget {
  2.   const CarouselWidget({super.key});
  3.  
  4.   @override
  5.   State<CarouselWidget> createState() => _CarouselWidgetState();
  6. }
  7.  
  8. class _CarouselWidgetState extends State<CarouselWidget> {
  9.   CarouselController carouselController = CarouselController();
  10.  
  11.   @override
  12.   Widget build(BuildContext context) {
  13.     Size size = MediaQuery.sizeOf(context);
  14.     double height = size.height;
  15.     double width = size.width;
  16.  
  17.     HabitCategoryData habitCategoryData =
  18.         Provider.of<HabitCategoryProvider>(context).habitCategoryData;
  19.  
  20.     List<HabitCategoryData> habitCategoryDataList =
  21.         habitCategoryDataMap.values.toList();
  22.  
  23.     int newIndex = habitCategoryDataList.indexOf(habitCategoryData);
  24.  
  25.     return Column(
  26.       children: [
  27.         RepaintBoundary(
  28.           child: SizedBox(
  29.             height: height * 0.24,
  30.             width: width,
  31.             child: Stack(
  32.               alignment: Alignment.center,
  33.               children: [
  34.                 Positioned.fill(
  35.                   child: CarouselSlider.builder(
  36.                     carouselController: carouselController,
  37.                     itemCount: habitCategoryDataMap.length,
  38.                     itemBuilder: (context, index, realIndex) {
  39.                       return CategoryCard(
  40.                           habitCategoryData: habitCategoryDataList[index]);
  41.                     },
  42.                     options: CarouselOptions(
  43.                       initialPage: newIndex,
  44.                       enlargeCenterPage: true,
  45.                       autoPlay: false,
  46.                       viewportFraction: 0.6,
  47.                       onPageChanged: (index, reason) {
  48.                         HabitCategoryData newHabitCategoryData =
  49.                             habitCategoryDataList[index];
  50.  
  51.                         Provider.of<HabitCategoryProvider>(context,
  52.                                 listen: false)
  53.                             .setHabitVariables(newHabitCategoryData);
  54.                         Provider.of<HabitCategoryProvider>(context,
  55.                                 listen: false)
  56.                             .clearSelectedItemsList();
  57.                       },
  58.                     ),
  59.                   ),
  60.                 ),
  61.                 Positioned(
  62.                   left: 12,
  63.                   //bottom: height * 0.25 / 2,
  64.                   child: Container(
  65.                     alignment: Alignment.center,
  66.                     height: (height * 0.24) * (1 / 4),
  67.                     decoration: BoxDecoration(
  68.                       color: Colors.white.withOpacity(0.7),
  69.                       borderRadius: BorderRadius.circular(12),
  70.                     ),
  71.                     child: IconButton(
  72.                       onPressed: carouselController.previousPage,
  73.                       icon: const Icon(Icons.arrow_back_ios_rounded),
  74.                     ),
  75.                   ),
  76.                 ),
  77.                 Positioned(
  78.                   right: 12,
  79.                   //bottom: height * 0.25 / 2,
  80.                   child: Container(
  81.                     alignment: Alignment.center,
  82.                     height: (height * 0.24) * (1 / 4),
  83.                     decoration: BoxDecoration(
  84.                       color: Colors.white.withOpacity(0.7),
  85.                       borderRadius: BorderRadius.circular(12),
  86.                     ),
  87.                     child: IconButton(
  88.                       onPressed: carouselController.nextPage,
  89.                       icon: const Icon(Icons.arrow_forward_ios_rounded),
  90.                     ),
  91.                   ),
  92.                 ),
  93.               ],
  94.             ),
  95.           ),
  96.         ),
  97.         const SizedBox(
  98.           height: 8,
  99.         ),
  100.         Text(
  101.           habitCategoryData.title,
  102.           style: Theme.of(context).textTheme.bodyLarge,
  103.         ),
  104.       ],
  105.     );
  106.   }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement