一、引入第三方依赖

flutter_swiper: ^1.1.6

image.png

Use this package as a library

Depend on it

Run this command:
With Flutter:

$ flutter pub add flutter_swiper

This will add a line like this to your package’s pubspec.yaml (and run an implicit flutter pub get):

dependencies: flutter_swiper: ^1.1.6

Alternatively, your editor might support or flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import ‘package:flutter_swiper/flutter_swiper.dart’;

二、使用组件

创建一个Swiper组件

  1. // ignore_for_file: file_names
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_swiper/flutter_swiper.dart';
  4. class Cate extends StatefulWidget {
  5. const Cate({ Key? key }) : super(key: key);
  6. @override
  7. _CateState createState() => _CateState();
  8. }
  9. class _CateState extends State<Cate> {
  10. List<Map> imgList = [
  11. {
  12. "url": "https://www.itying.com/images/flutter/1.png"
  13. },
  14. {
  15. "url": "https://www.itying.com/images/flutter/2.png"
  16. },
  17. {
  18. "url": "https://www.itying.com/images/flutter/3.png"
  19. },
  20. {
  21. "url": "https://www.itying.com/images/flutter/4.png"
  22. }
  23. ];
  24. @override
  25. Widget build(BuildContext context) {
  26. return Container(
  27. height: 200,
  28. child: Swiper(
  29. itemBuilder: (BuildContext context,int index) => Image.network(imgList[index]["url"],fit: BoxFit.fill),
  30. itemCount: imgList.length,
  31. pagination: const SwiperPagination(),
  32. autoplay: true,
  33. control: SwiperControl( // 指示器
  34. iconNext: Icon(Icons.navigate_next).icon,
  35. iconPrevious: Icon(Icons.navigate_before).icon
  36. ),
  37. )
  38. );
  39. }
  40. }

效果图:

image.png

三、Swiper的常见属性

Constructor (构造函数)

Basic

Parameter Default Description
scrollDirection Axis.horizontal If Axis.horizontal, the scroll view’s children are arranged horizontally in a row instead of vertically in a column.
loop true Set to false to disable continuous loop mode.
index 0 Index number of initial slide.
autoplay false Set to true enable auto play mode.
onIndexChanged void onIndexChanged(int index) Called with the new index when the user swiped or autoplay
onTap void onTap(int index) Called when user tap ui.
duration 300.0 The milliscends of every transaction animation costs
pagination null set new SwiperPagination() to show default pagination
control null set new SwiperControl() to show default control buttons

Pagination(分页器)

The pagination extends from SwiperPlugin,the SwiperPlugin provides extra ui for Swiper.Set new SwiperPagination() to show default pagination.

Parameter Default Description
alignment Alignment.bottomCenter Change this value if you what to put pagination in other place
margin const EdgeInsets.all(10.0) The distance between inner side of the parent container.
builder SwiperPagination.dots There are two default styles SwiperPagination.dots and SwiperPagination.fraction,both can be customized.

Control buttons(左右切换的按钮)

The control also extends from SwiperPlugin,set new SwiperControl() to show default control buttons.

Parameter Default Description
iconPrevious Icons.arrow_back_ios The icon data to display previous control button
iconNext Icons.arrow_forward_ios The icon data to display next.
color Theme.of(context).primaryColor Control button color
size 30.0 Control button size
padding const EdgeInsets.all(5.0) Control button padding