func_page_view.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import 'dart:async';
  2. import '../../../resource/assets.gen.dart';
  3. import 'package:clean/utils/expand.dart';
  4. import 'package:flutter/Material.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. class InfinitePreviewPageView extends StatefulWidget {
  7. final List<Widget> items;
  8. final double height;
  9. final Duration autoPlayDuration;
  10. final bool autoPlay;
  11. final bool showIndicator;
  12. const InfinitePreviewPageView({
  13. super.key,
  14. required this.items,
  15. this.height = 98,
  16. this.autoPlayDuration = const Duration(seconds: 3),
  17. this.autoPlay = true,
  18. this.showIndicator = true,
  19. });
  20. @override
  21. State<InfinitePreviewPageView> createState() => _InfinitePreviewPageViewState();
  22. }
  23. class _InfinitePreviewPageViewState extends State<InfinitePreviewPageView> {
  24. late final PageController _pageController;
  25. Timer? _timer;
  26. double _currentPage = 0;
  27. bool _isInitialized = false;
  28. // 实际页面索引
  29. int get _actualPage => _currentPage.round() % widget.items.length;
  30. @override
  31. void initState() {
  32. super.initState();
  33. // 设置初始页面
  34. final initialPage = widget.items.length * 1000;
  35. _currentPage = initialPage.toDouble(); // 设置初始currentPage
  36. _pageController = PageController(
  37. viewportFraction: 0.8,
  38. initialPage: initialPage,
  39. );
  40. _pageController.addListener(() {
  41. setState(() {
  42. _currentPage = _pageController.page ?? initialPage.toDouble();
  43. });
  44. });
  45. // 延迟一帧设置初始化完成
  46. WidgetsBinding.instance.addPostFrameCallback((_) {
  47. setState(() {
  48. _isInitialized = true;
  49. });
  50. });
  51. if (widget.autoPlay) {
  52. _startAutoPlay();
  53. }
  54. }
  55. void _startAutoPlay() {
  56. _timer?.cancel();
  57. _timer = Timer.periodic(widget.autoPlayDuration, (timer) {
  58. if (widget.items.isNotEmpty) {
  59. _pageController.nextPage(
  60. duration: const Duration(milliseconds: 300),
  61. curve: Curves.easeOut,
  62. );
  63. }
  64. });
  65. }
  66. @override
  67. void dispose() {
  68. _pageController.dispose();
  69. _timer?.cancel();
  70. super.dispose();
  71. }
  72. // 构建页面指示器
  73. Widget _buildPageIndicator() {
  74. return Row(
  75. mainAxisAlignment: MainAxisAlignment.center,
  76. children: List.generate(
  77. widget.items.length,
  78. (index) {
  79. bool isActive = index == _actualPage;
  80. return Container(
  81. margin: EdgeInsets.symmetric(horizontal: 4.w),
  82. height: 6.w,
  83. width: 6.w,
  84. decoration: BoxDecoration(
  85. color: isActive ? Colors.white : Colors.white.withOpacity(0.22),
  86. borderRadius: BorderRadius.circular(6.r),
  87. ),
  88. );
  89. },
  90. ),
  91. );
  92. }
  93. @override
  94. Widget build(BuildContext context) {
  95. if (widget.items.isEmpty) return const SizedBox();
  96. return Column(
  97. children: [
  98. SizedBox(
  99. height: widget.height,
  100. child: PageView.builder(
  101. controller: _pageController,
  102. itemBuilder: (context, index) {
  103. final itemIndex = index % widget.items.length;
  104. // 如果还没初始化完成,不应用缩放效果
  105. if (!_isInitialized) {
  106. return Padding(
  107. padding: const EdgeInsets.symmetric(horizontal: 8),
  108. child: Container(
  109. decoration: BoxDecoration(
  110. color: const Color(0xFF1E1E1E),
  111. borderRadius: BorderRadius.circular(16),
  112. ),
  113. child: widget.items[itemIndex],
  114. ),
  115. );
  116. }
  117. // 计算缩放和透明度
  118. double diffScale = (_currentPage - index).abs();
  119. double scale = 1 - (diffScale * 0.1).clamp(0.0, 0.3);
  120. double opacity = 1 - (diffScale * 0.3).clamp(0.0, 0.5);
  121. return Transform.scale(
  122. scale: scale,
  123. child: Opacity(
  124. opacity: opacity,
  125. child: Padding(
  126. padding: const EdgeInsets.symmetric(horizontal: 0),
  127. child: Container(
  128. decoration: BoxDecoration(
  129. color: const Color(0xFF1E1E1E),
  130. borderRadius: BorderRadius.circular(23.r),
  131. ),
  132. child: widget.items[itemIndex],
  133. ),
  134. ),
  135. ),
  136. );
  137. },
  138. ),
  139. ),
  140. // 添加页面指示器,仅当showIndicator为true时显示
  141. if (widget.showIndicator)
  142. Padding(
  143. padding: EdgeInsets.only(top: 10.h),
  144. child: _buildPageIndicator(),
  145. ),
  146. ],
  147. );
  148. }
  149. }
  150. // 单个项目组件
  151. class PreviewItem extends StatelessWidget {
  152. final String title;
  153. final Image icon;
  154. const PreviewItem({
  155. super.key,
  156. required this.title,
  157. required this.icon
  158. });
  159. @override
  160. Widget build(BuildContext context) {
  161. return Stack(
  162. alignment: Alignment.topLeft,
  163. children: [
  164. Container(
  165. padding: EdgeInsets.only(left: 16.w, right: 16.w),
  166. decoration: BoxDecoration(
  167. gradient: LinearGradient(
  168. begin: Alignment.centerLeft,
  169. end: Alignment.centerRight,
  170. colors: [
  171. '#36363D'.color,
  172. '#18181F'.color,
  173. ],
  174. ),
  175. borderRadius: BorderRadius.circular(23.r),
  176. ),
  177. child: Row(
  178. children: [
  179. Container(
  180. width: 56.w,
  181. height: 56.w,
  182. child: icon,
  183. ),
  184. SizedBox(width: 10.w),
  185. Expanded(
  186. child: Column(
  187. crossAxisAlignment: CrossAxisAlignment.start,
  188. mainAxisAlignment: MainAxisAlignment.center,
  189. children: [
  190. Text(
  191. title,
  192. style: TextStyle(
  193. color: Colors.white,
  194. fontSize: 15.sp,
  195. fontWeight: FontWeight.w500,
  196. ),
  197. ),
  198. ],
  199. ),
  200. ),
  201. ],
  202. ),
  203. ),
  204. Container(
  205. padding: EdgeInsets.symmetric(vertical: 2.h, horizontal: 15.w),
  206. decoration: BoxDecoration(
  207. color: '#383850'.color,
  208. borderRadius: BorderRadius.only(topLeft: Radius.circular(100), bottomRight: Radius.circular(100)),
  209. ),
  210. child: ShaderMask(
  211. shaderCallback: (Rect bounds) {
  212. return LinearGradient(
  213. begin: Alignment.centerLeft,
  214. end: Alignment.centerRight,
  215. colors: [
  216. '#ffdd55'.color,
  217. '#ffe89d'.color,
  218. ],
  219. ).createShader(bounds);
  220. },
  221. child: Text(
  222. "VIP",
  223. style: TextStyle(
  224. color: Colors.white,
  225. fontSize: 12.sp,
  226. fontWeight: FontWeight.w700
  227. )
  228. ),
  229. ),
  230. )
  231. ],
  232. );
  233. }
  234. }