|
| 1 | +import 'dart:ui'; |
| 2 | + |
| 3 | +import 'package:flutter/gestures.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:phosphor_flutter/phosphor_flutter.dart'; |
| 6 | + |
| 7 | +final class CarouselScrollBehavior extends ScrollBehavior { |
| 8 | + final double itemExtent; |
| 9 | + const CarouselScrollBehavior({required this.itemExtent}); |
| 10 | + // Override behavior methods and getters like dragDevices |
| 11 | + @override |
| 12 | + Set<PointerDeviceKind> get dragDevices => { |
| 13 | + PointerDeviceKind.touch, |
| 14 | + PointerDeviceKind.mouse, |
| 15 | + // etc. |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +class VulpineCarouselView extends StatefulWidget { |
| 20 | + final List<Widget> children; |
| 21 | + final double itemExtent; |
| 22 | + final double shrinkExtent; |
| 23 | + |
| 24 | + const VulpineCarouselView({ |
| 25 | + super.key, |
| 26 | + required this.children, |
| 27 | + this.itemExtent = 330, |
| 28 | + this.shrinkExtent = 200, |
| 29 | + }); |
| 30 | + |
| 31 | + @override |
| 32 | + State<VulpineCarouselView> createState() => _VulpineCarouselViewState(); |
| 33 | +} |
| 34 | + |
| 35 | +class _VulpineCarouselViewState extends State<VulpineCarouselView> { |
| 36 | + final CarouselController _carouselController = CarouselController(); |
| 37 | + double _nextOffset = 0; |
| 38 | + double _maxOffset = 0; |
| 39 | + |
| 40 | + @override |
| 41 | + void initState() { |
| 42 | + super.initState(); |
| 43 | + _updateMaxPosition(); |
| 44 | + } |
| 45 | + |
| 46 | + @override |
| 47 | + void dispose() { |
| 48 | + super.dispose(); |
| 49 | + _carouselController.dispose(); |
| 50 | + } |
| 51 | + |
| 52 | + void _updateMaxPosition() { |
| 53 | + _maxOffset = |
| 54 | + widget.itemExtent + |
| 55 | + (widget.shrinkExtent * (widget.children.length - 1)); |
| 56 | + } |
| 57 | + |
| 58 | + void _animateToPrevious() => |
| 59 | + _animateTo(_carouselController.offset - widget.itemExtent); |
| 60 | + void _animateToNext() => |
| 61 | + _animateTo(_carouselController.offset + widget.itemExtent); |
| 62 | + |
| 63 | + void animateToNext(int index) { |
| 64 | + double to = 0; |
| 65 | + if (index > 1) { |
| 66 | + to = widget.itemExtent + (widget.shrinkExtent * (index - 1)); |
| 67 | + } else if (index == 1) { |
| 68 | + to = widget.itemExtent; |
| 69 | + } |
| 70 | + _animateTo(to); |
| 71 | + } |
| 72 | + |
| 73 | + void _animateTo(double to) { |
| 74 | + _carouselController.animateTo( |
| 75 | + to, |
| 76 | + duration: const Duration(milliseconds: 300), |
| 77 | + curve: Curves.easeInOut, |
| 78 | + ); |
| 79 | + setState(() { |
| 80 | + _nextOffset = to; |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + Widget build(BuildContext context) { |
| 86 | + return ConstrainedBox( |
| 87 | + constraints: const BoxConstraints(maxHeight: 200), |
| 88 | + child: Stack( |
| 89 | + children: [ |
| 90 | + ScrollConfiguration( |
| 91 | + behavior: CarouselScrollBehavior(itemExtent: widget.itemExtent), |
| 92 | + child: CarouselView( |
| 93 | + itemSnapping: false, |
| 94 | + itemExtent: widget.itemExtent, |
| 95 | + shrinkExtent: widget.shrinkExtent, |
| 96 | + controller: _carouselController, |
| 97 | + children: widget.children, |
| 98 | + ), |
| 99 | + ), |
| 100 | + Align( |
| 101 | + alignment: Alignment.centerLeft, |
| 102 | + child: AnimatedScale( |
| 103 | + alignment: Alignment.centerLeft, |
| 104 | + scale: _nextOffset > 0 ? 1 : 0, |
| 105 | + duration: const Duration(milliseconds: 300), |
| 106 | + child: Padding( |
| 107 | + padding: const EdgeInsets.all(8.0), |
| 108 | + child: IconButton( |
| 109 | + icon: const Icon(PhosphorIconsLight.arrowLeft), |
| 110 | + onPressed: _animateToPrevious, |
| 111 | + ), |
| 112 | + ), |
| 113 | + ), |
| 114 | + ), |
| 115 | + Align( |
| 116 | + alignment: Alignment.centerRight, |
| 117 | + child: AnimatedScale( |
| 118 | + alignment: Alignment.centerRight, |
| 119 | + scale: _nextOffset < _maxOffset ? 1 : 0, |
| 120 | + duration: const Duration(milliseconds: 300), |
| 121 | + child: Padding( |
| 122 | + padding: const EdgeInsets.all(8.0), |
| 123 | + child: IconButton( |
| 124 | + icon: const Icon(PhosphorIconsLight.arrowRight), |
| 125 | + onPressed: _animateToNext, |
| 126 | + ), |
| 127 | + ), |
| 128 | + ), |
| 129 | + ), |
| 130 | + ], |
| 131 | + ), |
| 132 | + ); |
| 133 | + } |
| 134 | +} |
0 commit comments