carousel_slider.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import 'dart:async';
  2. import 'package:eye_video/framework/uikit/carousel/carousel_controller.dart';
  3. import 'package:eye_video/framework/uikit/carousel/carousel_option.dart';
  4. import 'package:eye_video/framework/uikit/carousel/carousel_state.dart';
  5. import 'package:eye_video/framework/uikit/carousel/utils.dart';
  6. import 'package:flutter/gestures.dart';
  7. import 'package:flutter/material.dart';
  8. class CarouselSlider extends StatefulWidget {
  9. final CarouselOption options;
  10. final List<Widget> items;
  11. final IndexedWidgetBuilder itemBuilder;
  12. final CarouselController _carouselController;
  13. final int itemCount;
  14. CarouselSlider({
  15. required this.items,
  16. required this.options,
  17. carouselController,
  18. Key? key,
  19. }) : itemBuilder = null,
  20. itemCount = items != null ? items.length : 0,
  21. _carouselController = carouselController ?? CarouselController(),
  22. super(key: key);
  23. CarouselSlider.builder({
  24. required this.itemCount,
  25. required this.itemBuilder,
  26. required this.options,
  27. carouselController,
  28. Key? key,
  29. }) : items = null,
  30. _carouselController = carouselController ?? CarouselController(),
  31. super(key: key);
  32. @override
  33. CarouselSliderState createState() => CarouselSliderState(_carouselController);
  34. }
  35. class CarouselSliderState extends State<CarouselSlider>
  36. with TickerProviderStateMixin {
  37. final CarouselController carouselController;
  38. Timer timer;
  39. CarouselOption get options => widget.options ?? CarouselOption();
  40. CarouselState carouselState;
  41. CarouselPageChangedReason mode = CarouselPageChangedReason.controller;
  42. CarouselSliderState(this.carouselController);
  43. void changeMode(CarouselPageChangedReason _mode) {
  44. mode = _mode;
  45. }
  46. @override
  47. void didUpdateWidget(CarouselSlider oldWidget) {
  48. carouselState.options = options;
  49. carouselState.itemCount = widget.itemCount;
  50. super.didUpdateWidget(oldWidget);
  51. }
  52. @override
  53. void initState() {
  54. super.initState();
  55. carouselState =
  56. CarouselState(this.options, clearTimer, resumeTimer, this.changeMode);
  57. carouselState.itemCount = widget.itemCount;
  58. carouselController.state = carouselState;
  59. carouselState.initIndex = widget.options.initIndex;
  60. carouselState.realIndex = options.isEnableLoop
  61. ? carouselState.realIndex + carouselState.initIndex
  62. : carouselState.initIndex;
  63. timer = getTimer();
  64. PageController pageController = PageController(
  65. viewportFraction: options.viewportFraction,
  66. initialPage: carouselState.realIndex,
  67. );
  68. carouselState.pageController = pageController;
  69. }
  70. Timer getTimer() {
  71. if (widget.options.autoPlay) {
  72. return Timer.periodic(widget.options.autoPlayInterval, (_) {
  73. CarouselPageChangedReason previousReason = mode;
  74. changeMode(CarouselPageChangedReason.timed);
  75. int nextPage = carouselState.pageController.page.round() + 1;
  76. int itemCount = widget.itemCount ?? widget.items.length;
  77. if (nextPage >= itemCount && widget.options.isEnableLoop == false) {
  78. if (widget.options.pauseAutoPlayInFiniteScroll) {
  79. clearTimer();
  80. return;
  81. }
  82. nextPage = 0;
  83. }
  84. carouselState.pageController
  85. .animateToPage(nextPage,
  86. duration: widget.options.autoPlayAnimationDuration,
  87. curve: widget.options.autoPlayCurve)
  88. .then((_) => changeMode(previousReason));
  89. });
  90. }
  91. return null;
  92. }
  93. void clearTimer() {
  94. if (widget.options.autoPlay) {
  95. timer?.cancel();
  96. }
  97. }
  98. void resumeTimer() {
  99. if (widget.options.autoPlay) {
  100. timer = getTimer();
  101. }
  102. }
  103. Widget getGestureWrapper(Widget child) {
  104. Widget wrapper;
  105. if (widget.options.height != null) {
  106. wrapper = Container(height: widget.options.height, child: child);
  107. } else {
  108. wrapper =
  109. AspectRatio(aspectRatio: widget.options.aspectRatio, child: child);
  110. }
  111. return RawGestureDetector(
  112. gestures: {
  113. _MultipleGestureRecognizer:
  114. GestureRecognizerFactoryWithHandlers<_MultipleGestureRecognizer>(
  115. () => _MultipleGestureRecognizer(),
  116. (_MultipleGestureRecognizer instance) {
  117. instance.onStart = (_) {
  118. onStart();
  119. };
  120. instance.onDown = (_) {
  121. onPanDown();
  122. };
  123. instance.onEnd = (_) {
  124. onPanUp();
  125. };
  126. instance.onCancel = () {
  127. onPanUp();
  128. };
  129. }),
  130. },
  131. child: NotificationListener(
  132. onNotification: (notification) {
  133. if (widget.options.onScrolled != null &&
  134. notification is ScrollUpdateNotification) {
  135. widget.options.onScrolled(carouselState.pageController.page);
  136. }
  137. return false;
  138. },
  139. child: wrapper,
  140. ),
  141. );
  142. }
  143. Widget getCenterWrapper(Widget child) {
  144. if (widget.options.disableCenter) {
  145. return Container(
  146. child: child,
  147. );
  148. }
  149. return Center(child: child);
  150. }
  151. Widget getEnlargeWrapper(Widget child,
  152. {double width, double height, double scale}) {
  153. if (widget.options.enlargeStrategy == CenterPageEnlargeStrategy.normal) {
  154. return SizedBox(child: child, width: width, height: height);
  155. }
  156. return Transform.scale(
  157. scale: scale,
  158. child: Container(child: child, width: width, height: height));
  159. }
  160. void onStart() {
  161. changeMode(CarouselPageChangedReason.manual);
  162. }
  163. void onPanDown() {
  164. if (widget.options.pauseAutoPlayOnTouch) {
  165. clearTimer();
  166. }
  167. changeMode(CarouselPageChangedReason.manual);
  168. }
  169. void onPanUp() {
  170. if (widget.options.pauseAutoPlayOnTouch) {
  171. resumeTimer();
  172. }
  173. }
  174. @override
  175. void dispose() {
  176. super.dispose();
  177. clearTimer();
  178. }
  179. @override
  180. Widget build(BuildContext context) {
  181. return getGestureWrapper(PageView.builder(
  182. physics: widget.options.scrollPhysics,
  183. scrollDirection: widget.options.scrollDirection,
  184. controller: carouselState.pageController,
  185. reverse: widget.options.isEnableReverse,
  186. itemCount: widget.options.isEnableLoop ? null : widget.itemCount,
  187. key: widget.options.pageViewKey,
  188. onPageChanged: (int index) {
  189. int currentPage = getRealIndex(index + carouselState.initIndex,
  190. carouselState.realIndex, widget.itemCount);
  191. if (widget.options.onPageChanged != null) {
  192. widget.options.onPageChanged(currentPage, mode);
  193. }
  194. },
  195. itemBuilder: (BuildContext context, int idx) {
  196. final int index = getRealIndex(idx + carouselState.initIndex,
  197. carouselState.realIndex, widget.itemCount);
  198. return AnimatedBuilder(
  199. animation: carouselState.pageController,
  200. child: (widget.items != null)
  201. ? (widget.items.length > 0 ? widget.items[index] : Container())
  202. : widget.itemBuilder(context, index),
  203. builder: (BuildContext context, child) {
  204. double distortionValue = 1.0;
  205. if (widget.options != null && widget.options.isEnableLargeCenterPage != null && widget.options.isEnableLargeCenterPage == true) {
  206. double itemOffset;
  207. if (carouselState.pageController.position.minScrollExtent == null || carouselState.pageController.position.maxScrollExtent == null) {
  208. BuildContext storageContext = carouselState.pageController.position.context.storageContext;
  209. final double previousSavedPosition = PageStorage.of(storageContext)?.readState(storageContext) as double;
  210. if (previousSavedPosition != null) {
  211. itemOffset = previousSavedPosition - idx.toDouble();
  212. } else {
  213. itemOffset = carouselState.realIndex.toDouble() - idx.toDouble();
  214. }
  215. } else {
  216. itemOffset = carouselState.pageController.page - idx;
  217. }
  218. final distortionRatio =
  219. (1 - (itemOffset.abs() * 0.3)).clamp(0.0, 1.0);
  220. distortionValue = Curves.easeOut.transform(distortionRatio);
  221. }
  222. final double height = widget.options.height ??
  223. MediaQuery.of(context).size.width *
  224. (1 / widget.options.aspectRatio);
  225. if (widget.options != null && widget.options.scrollDirection == Axis.horizontal) {
  226. return getCenterWrapper(getEnlargeWrapper(child,
  227. height: distortionValue * height, scale: distortionValue));
  228. } else {
  229. return getCenterWrapper(getEnlargeWrapper(child,
  230. width: distortionValue * MediaQuery.of(context).size.width,
  231. scale: distortionValue));
  232. }
  233. },
  234. );
  235. },
  236. ));
  237. }
  238. }
  239. class _MultipleGestureRecognizer extends PanGestureRecognizer {
  240. @override
  241. void rejectGesture(int pointer) {
  242. acceptGesture(pointer);
  243. }
  244. }