• 初学Flutter:swiper实现


    效果展示:

    flutter swiper

    1、安装 card_swiper 

    2、引入card_swiper

    import 'package:card_swiper/card_swiper.dart';
    

    3、使用

    这里我主要是对官网例子进行实践,主要是5种常用的swiper

    1、普遍的swiper

    1. //custom swiper
    2. class CustomSwiper extends StatefulWidget {
    3. const CustomSwiper({super.key, required this.banner});
    4. final List banner;
    5. @override
    6. _CustomSwiperState createState() => _CustomSwiperState();
    7. }
    8. class _CustomSwiperState extends State<CustomSwiper> {
    9. @override
    10. Widget build(BuildContext context) {
    11. return Swiper(
    12. itemBuilder: (BuildContext context, int index) {
    13. return Image.network(
    14. widget.banner[index]['banner_url'],
    15. fit: BoxFit.fill,
    16. );
    17. },
    18. onTap: (index) {
    19. print(index);
    20. },
    21. itemCount: widget.banner.length,
    22. // autoplay: true,
    23. pagination: const SwiperPagination(
    24. builder: DotSwiperPaginationBuilder(
    25. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    26. control: SwiperControl(),
    27. );
    28. }
    29. }

    2、layouts swiper

    1. //layouts swiper
    2. class MyLayoutSwiper extends StatefulWidget {
    3. const MyLayoutSwiper({super.key, required this.banner});
    4. final List banner;
    5. @override
    6. _MyLayoutSwiperState createState() => _MyLayoutSwiperState();
    7. }
    8. class _MyLayoutSwiperState extends State<MyLayoutSwiper> {
    9. @override
    10. Widget build(BuildContext context) {
    11. return Swiper(
    12. itemBuilder: (BuildContext context, int index) {
    13. return Image.network(
    14. widget.banner[index]['banner_url'],
    15. fit: BoxFit.fill,
    16. );
    17. },
    18. onTap: (index) {
    19. print(index);
    20. },
    21. itemCount: widget.banner.length,
    22. // autoplay: true,
    23. pagination: const SwiperPagination(
    24. builder: DotSwiperPaginationBuilder(
    25. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    26. viewportFraction: 0.8,
    27. scale: 0.9,
    28. );
    29. }
    30. }

    3、STACK Layout swiper

    1. // STACK Layout swiper
    2. class StackLayoutSwiper extends StatefulWidget {
    3. const StackLayoutSwiper({super.key, required this.banner});
    4. final List banner;
    5. @override
    6. _StackLayoutSwiperState createState() => _StackLayoutSwiperState();
    7. }
    8. class _StackLayoutSwiperState extends State<StackLayoutSwiper> {
    9. @override
    10. Widget build(BuildContext context) {
    11. return Swiper(
    12. itemBuilder: (BuildContext context, int index) {
    13. return Image.network(
    14. widget.banner[index]['banner_url'],
    15. fit: BoxFit.fill,
    16. );
    17. },
    18. onTap: (index) {
    19. print(index);
    20. },
    21. itemCount: widget.banner.length,
    22. // autoplay: true,
    23. pagination: const SwiperPagination(
    24. builder: DotSwiperPaginationBuilder(
    25. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    26. viewportFraction: 0.8,
    27. scale: 0.9,
    28. itemWidth: 300.0,
    29. layout: SwiperLayout.STACK,
    30. );
    31. }
    32. }

    4、TINDER Layout Swiper

    1. // TINDER Layout Swiper
    2. class TinderLayoutSwiper extends StatefulWidget {
    3. const TinderLayoutSwiper({super.key, required this.banner});
    4. final List banner;
    5. @override
    6. _TinderLayoutSwiperState createState() => _TinderLayoutSwiperState();
    7. }
    8. class _TinderLayoutSwiperState extends State<TinderLayoutSwiper> {
    9. @override
    10. Widget build(BuildContext context) {
    11. return Swiper(
    12. itemBuilder: (BuildContext context, int index) {
    13. return Image.network(
    14. widget.banner[index]['banner_url'],
    15. fit: BoxFit.fill,
    16. );
    17. },
    18. onTap: (index) {
    19. print(index);
    20. },
    21. itemCount: widget.banner.length,
    22. // autoplay: true,
    23. pagination: const SwiperPagination(
    24. builder: DotSwiperPaginationBuilder(
    25. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    26. viewportFraction: 0.8,
    27. scale: 0.9,
    28. itemWidth: 300.0,
    29. itemHeight: 400.0,
    30. layout: SwiperLayout.TINDER,
    31. );
    32. }
    33. }

    5、CUSTOM LAYOUT Swiper

    1. // TINDER Layout Swiper
    2. class TinderLayoutSwiper extends StatefulWidget {
    3. const TinderLayoutSwiper({super.key, required this.banner});
    4. final List banner;
    5. @override
    6. _TinderLayoutSwiperState createState() => _TinderLayoutSwiperState();
    7. }
    8. class _TinderLayoutSwiperState extends State<TinderLayoutSwiper> {
    9. @override
    10. Widget build(BuildContext context) {
    11. return Swiper(
    12. itemBuilder: (BuildContext context, int index) {
    13. return Image.network(
    14. widget.banner[index]['banner_url'],
    15. fit: BoxFit.fill,
    16. );
    17. },
    18. onTap: (index) {
    19. print(index);
    20. },
    21. itemCount: widget.banner.length,
    22. // autoplay: true,
    23. pagination: const SwiperPagination(
    24. builder: DotSwiperPaginationBuilder(
    25. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    26. viewportFraction: 0.8,
    27. scale: 0.9,
    28. itemWidth: 300.0,
    29. itemHeight: 400.0,
    30. layout: SwiperLayout.TINDER,
    31. );
    32. }
    33. }
    34. // CUSTOM LAYOUT Swiper
    35. class CustomLayoutSwiper extends StatefulWidget {
    36. const CustomLayoutSwiper({super.key, required this.banner});
    37. final List banner;
    38. @override
    39. _CustomLayoutSwiperState createState()=>_CustomLayoutSwiperState();
    40. }
    41. class _CustomLayoutSwiperState extends State<CustomLayoutSwiper>{
    42. @override
    43. Widget build(BuildContext context) {
    44. // TODO: implement build
    45. return Swiper(
    46. itemBuilder: (BuildContext context, int index) {
    47. return Image.network(
    48. widget.banner[index]['banner_url'],
    49. fit: BoxFit.fill,
    50. );
    51. },
    52. onTap: (index) {
    53. print(index);
    54. },
    55. itemCount: widget.banner.length,
    56. // autoplay: true,
    57. pagination: const SwiperPagination(
    58. builder: DotSwiperPaginationBuilder(
    59. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    60. viewportFraction: 0.8,
    61. scale: 0.9,
    62. itemWidth: 300.0,
    63. itemHeight: 400.0,
    64. layout: SwiperLayout.CUSTOM,
    65. customLayoutOption: CustomLayoutOption(
    66. startIndex: -1,
    67. stateCount: 3
    68. )..addRotate([
    69. -45.0/180,
    70. 0.0,
    71. 45.0/180
    72. ])..addTranslate([
    73. Offset(-370.0, -40.0),
    74. Offset(0.0, 0.0),
    75. Offset(370.0, -40.0)
    76. ]),
    77. );
    78. }
    79. }

    main.dart全部代码

    1. import 'package:flutter/material.dart';
    2. import 'package:card_swiper/card_swiper.dart';
    3. void main() {
    4. runApp(const MyApp());
    5. }
    6. class MyApp extends StatelessWidget {
    7. const MyApp({super.key});
    8. @override
    9. Widget build(BuildContext context) {
    10. return MaterialApp(
    11. theme: ThemeData(
    12. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple)),
    13. home: const Home());
    14. }
    15. }
    16. class Home extends StatefulWidget {
    17. const Home({super.key});
    18. @override
    19. _HomeState createState() => _HomeState();
    20. }
    21. class _HomeState extends State<Home> {
    22. List banner = [
    23. {
    24. "banner_url":
    25. "https://ts1.cn.mm.bing.net/th/id/R-C.8e091d0e6a2b9bcb27738cd96e22f699?rik=M4LFAVmmb38W6A&riu=http%3a%2f%2fi3.img.969g.com%2fdown%2fimgx2013%2f01%2f21%2f206_170432_901de.jpg&ehk=564uTBfxvugUBybGO2%2bTpMUNgJEa6rUfSrc1sonLso8%3d&risl=&pid=ImgRaw&r=0"
    26. },
    27. {
    28. "banner_url":
    29. "https://ts1.cn.mm.bing.net/th/id/R-C.abd4829c9387ec1bfd1a276a5c1da122?rik=dl55Voqxy4wINQ&riu=http%3a%2f%2fi1.073img.com%2f140306%2f4343693_144142_1.jpg&ehk=qvhMjT0iGQT5DhH8MTkAzJqUpjHJRScTQTT5hj%2forLM%3d&risl=&pid=ImgRaw&r=0"
    30. },
    31. {
    32. "banner_url":
    33. "https://ts1.cn.mm.bing.net/th/id/R-C.ddfbf9ed55354323035f947515fe0233?rik=0KFdFk3RHUzBdg&riu=http%3a%2f%2fi1.img.969g.com%2fdown%2fimgx2013%2f01%2f05%2f206_164916_4fab0.jpg&ehk=Vqb%2f5c3%2fW5n39t7bAae6YpUlfG4ibEbf9lpETY5Xz4c%3d&risl=&pid=ImgRaw&r=0"
    34. }
    35. ];
    36. @override
    37. Widget build(BuildContext context) {
    38. return Scaffold(
    39. appBar: AppBar(title: const Text('flutter轮播图学习'), centerTitle: true),
    40. // body: Container(child: Text('pp')),
    41. body: SingleChildScrollView(
    42. child: Column(children: [
    43. const Text('Custom swiper',
    44. style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
    45. SizedBox(
    46. height: 200,
    47. child: CustomSwiper(
    48. banner: banner,
    49. )),
    50. const Text('Build in layouts ',
    51. style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
    52. SizedBox(
    53. height: 200,
    54. child: MyLayoutSwiper(
    55. banner: banner,
    56. )),
    57. const Text('Build in STACK layouts ',
    58. style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
    59. SizedBox(
    60. height: 200,
    61. child: StackLayoutSwiper(
    62. banner: banner,
    63. )),
    64. const Text('Build in TINDER layouts ',
    65. style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
    66. SizedBox(
    67. height: 200,
    68. child: TinderLayoutSwiper(
    69. banner: banner,
    70. )),
    71. const Text('Build in CUSTOM layouts ',
    72. style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
    73. SizedBox(
    74. height: 200,
    75. child: CustomLayoutSwiper(
    76. banner: banner,
    77. )),
    78. ]),
    79. ));
    80. }
    81. }
    82. //custom swiper
    83. class CustomSwiper extends StatefulWidget {
    84. const CustomSwiper({super.key, required this.banner});
    85. final List banner;
    86. @override
    87. _CustomSwiperState createState() => _CustomSwiperState();
    88. }
    89. class _CustomSwiperState extends State<CustomSwiper> {
    90. @override
    91. Widget build(BuildContext context) {
    92. return Swiper(
    93. itemBuilder: (BuildContext context, int index) {
    94. return Image.network(
    95. widget.banner[index]['banner_url'],
    96. fit: BoxFit.fill,
    97. );
    98. },
    99. onTap: (index) {
    100. print(index);
    101. },
    102. itemCount: widget.banner.length,
    103. // autoplay: true,
    104. pagination: const SwiperPagination(
    105. builder: DotSwiperPaginationBuilder(
    106. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    107. control: SwiperControl(),
    108. );
    109. }
    110. }
    111. //layouts swiper
    112. class MyLayoutSwiper extends StatefulWidget {
    113. const MyLayoutSwiper({super.key, required this.banner});
    114. final List banner;
    115. @override
    116. _MyLayoutSwiperState createState() => _MyLayoutSwiperState();
    117. }
    118. class _MyLayoutSwiperState extends State<MyLayoutSwiper> {
    119. @override
    120. Widget build(BuildContext context) {
    121. return Swiper(
    122. itemBuilder: (BuildContext context, int index) {
    123. return Image.network(
    124. widget.banner[index]['banner_url'],
    125. fit: BoxFit.fill,
    126. );
    127. },
    128. onTap: (index) {
    129. print(index);
    130. },
    131. itemCount: widget.banner.length,
    132. // autoplay: true,
    133. pagination: const SwiperPagination(
    134. builder: DotSwiperPaginationBuilder(
    135. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    136. viewportFraction: 0.8,
    137. scale: 0.9,
    138. );
    139. }
    140. }
    141. // STACK Layout swiper
    142. class StackLayoutSwiper extends StatefulWidget {
    143. const StackLayoutSwiper({super.key, required this.banner});
    144. final List banner;
    145. @override
    146. _StackLayoutSwiperState createState() => _StackLayoutSwiperState();
    147. }
    148. class _StackLayoutSwiperState extends State<StackLayoutSwiper> {
    149. @override
    150. Widget build(BuildContext context) {
    151. return Swiper(
    152. itemBuilder: (BuildContext context, int index) {
    153. return Image.network(
    154. widget.banner[index]['banner_url'],
    155. fit: BoxFit.fill,
    156. );
    157. },
    158. onTap: (index) {
    159. print(index);
    160. },
    161. itemCount: widget.banner.length,
    162. // autoplay: true,
    163. pagination: const SwiperPagination(
    164. builder: DotSwiperPaginationBuilder(
    165. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    166. viewportFraction: 0.8,
    167. scale: 0.9,
    168. itemWidth: 300.0,
    169. layout: SwiperLayout.STACK,
    170. );
    171. }
    172. }
    173. // TINDER Layout Swiper
    174. class TinderLayoutSwiper extends StatefulWidget {
    175. const TinderLayoutSwiper({super.key, required this.banner});
    176. final List banner;
    177. @override
    178. _TinderLayoutSwiperState createState() => _TinderLayoutSwiperState();
    179. }
    180. class _TinderLayoutSwiperState extends State<TinderLayoutSwiper> {
    181. @override
    182. Widget build(BuildContext context) {
    183. return Swiper(
    184. itemBuilder: (BuildContext context, int index) {
    185. return Image.network(
    186. widget.banner[index]['banner_url'],
    187. fit: BoxFit.fill,
    188. );
    189. },
    190. onTap: (index) {
    191. print(index);
    192. },
    193. itemCount: widget.banner.length,
    194. // autoplay: true,
    195. pagination: const SwiperPagination(
    196. builder: DotSwiperPaginationBuilder(
    197. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    198. viewportFraction: 0.8,
    199. scale: 0.9,
    200. itemWidth: 300.0,
    201. itemHeight: 400.0,
    202. layout: SwiperLayout.TINDER,
    203. );
    204. }
    205. }
    206. // CUSTOM LAYOUT Swiper
    207. class CustomLayoutSwiper extends StatefulWidget {
    208. const CustomLayoutSwiper({super.key, required this.banner});
    209. final List banner;
    210. @override
    211. _CustomLayoutSwiperState createState()=>_CustomLayoutSwiperState();
    212. }
    213. class _CustomLayoutSwiperState extends State<CustomLayoutSwiper>{
    214. @override
    215. Widget build(BuildContext context) {
    216. // TODO: implement build
    217. return Swiper(
    218. itemBuilder: (BuildContext context, int index) {
    219. return Image.network(
    220. widget.banner[index]['banner_url'],
    221. fit: BoxFit.fill,
    222. );
    223. },
    224. onTap: (index) {
    225. print(index);
    226. },
    227. itemCount: widget.banner.length,
    228. // autoplay: true,
    229. pagination: const SwiperPagination(
    230. builder: DotSwiperPaginationBuilder(
    231. color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
    232. viewportFraction: 0.8,
    233. scale: 0.9,
    234. itemWidth: 300.0,
    235. itemHeight: 400.0,
    236. layout: SwiperLayout.CUSTOM,
    237. customLayoutOption: CustomLayoutOption(
    238. startIndex: -1,
    239. stateCount: 3
    240. )..addRotate([
    241. -45.0/180,
    242. 0.0,
    243. 45.0/180
    244. ])..addTranslate([
    245. Offset(-370.0, -40.0),
    246. Offset(0.0, 0.0),
    247. Offset(370.0, -40.0)
    248. ]),
    249. );
    250. }
    251. }

  • 相关阅读:
    【学习笔记】《Python深度学习》第五章:深度学习用于计算机视觉
    Hive进阶函数之【分割函数(split)、行转列函数(explode)、lateral view】
    java lambda之方法句柄&invokedynamic指令
    平衡树 Treap & Splay [学习笔记]
    基于vue和nodejs的项目知识信息分享平台
    网络安全CTF比赛有哪些事?——《CTF那些事儿》告诉你
    重制版 day 08字典和元组
    Python重要语法回顾-2-流程控制(顺序、选择、循环)、函数、包、异常、文件操作、类、时间
    Eclipse 内容辅助
    具备自纠正和逐维学习能力的粒子群算法-附代码
  • 原文地址:https://blog.csdn.net/Hhjian524/article/details/134218456