|
| 1 | +import 'dart:developer'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | + |
| 5 | +class CarousalSlider extends StatefulWidget { |
| 6 | + const CarousalSlider({ |
| 7 | + Key? key, |
| 8 | + required this.childrens, |
| 9 | + required this.builder, |
| 10 | + required this.carouselOptions, |
| 11 | + }) : super(key: key); |
| 12 | + |
| 13 | + final List<Widget> childrens; |
| 14 | + final CarouselOptions carouselOptions; |
| 15 | + final Widget Function( |
| 16 | + BuildContext context, |
| 17 | + int index, |
| 18 | + Widget child, |
| 19 | + double primaryAnimation, |
| 20 | + double secondaryAnimation, |
| 21 | + ) builder; |
| 22 | + |
| 23 | + @override |
| 24 | + State<CarousalSlider> createState() => _CarousalSliderState(); |
| 25 | +} |
| 26 | + |
| 27 | +class _CarousalSliderState extends State<CarousalSlider> { |
| 28 | + late final PageController pageController; |
| 29 | + |
| 30 | + @override |
| 31 | + void initState() { |
| 32 | + // TODO: implement initState |
| 33 | + super.initState(); |
| 34 | + pageController = PageController( |
| 35 | + viewportFraction: widget.carouselOptions.viewportFraction ?? 0.8, |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + @override |
| 40 | + void dispose() { |
| 41 | + // TODO: implement dispose |
| 42 | + pageController.dispose(); |
| 43 | + super.dispose(); |
| 44 | + } |
| 45 | + |
| 46 | + @override |
| 47 | + Widget build(BuildContext context) { |
| 48 | + return SizedBox( |
| 49 | + height: widget.carouselOptions.height, |
| 50 | + child: PageView.builder( |
| 51 | + controller: pageController, |
| 52 | + itemCount: widget.childrens.length, |
| 53 | + padEnds: widget.carouselOptions.padEnds, |
| 54 | + onPageChanged: (int value) { |
| 55 | + log(value.toString()); |
| 56 | + }, |
| 57 | + itemBuilder: (BuildContext context, int index) { |
| 58 | + return AnimatedBuilder( |
| 59 | + animation: pageController, |
| 60 | + child: widget.childrens[index], |
| 61 | + builder: (BuildContext context, Widget? child) { |
| 62 | + final ScrollPosition position = pageController.position; |
| 63 | + late final double value; |
| 64 | + if (position.hasPixels && position.hasContentDimensions) { |
| 65 | + final double? page = pageController.page; |
| 66 | + if (page != null) { |
| 67 | + value = page - index; |
| 68 | + } |
| 69 | + } else { |
| 70 | + final BuildContext storageContext = |
| 71 | + pageController.position.context.storageContext; |
| 72 | + final double? previousSavedPosition = |
| 73 | + PageStorage.of(storageContext)?.readState(storageContext) |
| 74 | + as double?; |
| 75 | + if (previousSavedPosition != null) { |
| 76 | + value = previousSavedPosition - index.toDouble(); |
| 77 | + } else { |
| 78 | + value = pageController.initialPage - index.toDouble(); |
| 79 | + } |
| 80 | + } |
| 81 | + final double distortionRatio = |
| 82 | + (1 - (value.abs() * 0.2)).clamp(0.0, 1.0); |
| 83 | + |
| 84 | + final double distortionValue = |
| 85 | + Curves.linear.transform(distortionRatio.abs()); |
| 86 | + |
| 87 | + return widget.builder( |
| 88 | + context, |
| 89 | + index, |
| 90 | + child!, |
| 91 | + value, |
| 92 | + distortionValue, |
| 93 | + ); |
| 94 | + }, |
| 95 | + ); |
| 96 | + }, |
| 97 | + ), |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +class CarouselOptions { |
| 103 | + CarouselOptions({ |
| 104 | + required this.height, |
| 105 | + this.viewportFraction, |
| 106 | + this.padEnds = true, |
| 107 | + }); |
| 108 | + |
| 109 | + final double height; |
| 110 | + final double? viewportFraction; |
| 111 | + final bool padEnds; |
| 112 | +} |
0 commit comments