英文原文正文为项目原始 README(英文),本站后续会翻译为中文,当前仅剔除图片与无关章节并统一排版。
flutter_swiper
Flutter 最佳的轮播库,提供多种布局和无限循环。兼容 Android 和 iOS。
:sparkles::sparkles: 新功能:分页布局。
我们现在使用这个项目 flutter_page_indicator 。
:sparkles::sparkles: 新功能:页面转换器
最终,我们像 Android 一样有了 PageTransformer,只需向 Swiper 设置一个 transformer,它会返回一个已转换的组件。目前仅支持 DEFAULT 布局。感谢 @FlutterRocks,你做得很棒 👏。
我们现在使用这个项目 transformer_page_view 。
:sparkles::sparkles: 新功能:布局
展示
路线图
查看:ROADMAP.md
更新日志
查看:CHANGELOG.md
入门指南
安装
添加
flutter_swiper : ^lastest_version
到你的 pubspec.yaml,并运行
flutter packages get
在你的项目根目录。
基本用法
使用命令创建一个新项目
flutter create myapp
像这样编辑 lib/main.dart:
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Swiper(
itemBuilder: (BuildContext context,int index){
return new Image.network("http://via.placeholder.com/350x150",fit: BoxFit.fill,);
},
itemCount: 3,
pagination: new SwiperPagination(),
control: new SwiperControl(),
),
);
}
}
构造函数
基础
| 参数 | 默认值 | 描述 |
|---|---|---|
| scrollDirection | Axis.horizontal | 如果是 Axis.horizontal,滚动视图的子项将水平排列在一行中,而不是垂直排列在一列中。 |
| loop | true | 设置为 false 可禁用循环模式。 |
| index | 0 | 初始幻灯片的索引编号。 |
| autoplay | false | 设置为 true 启用自动播放模式。 |
| onIndexChanged | void onIndexChanged(int index) | 当用户滑动或自动播放时,会调用并传入新的索引 |
| onTap | void onTap(int index) | 当用户点击 UI 时调用。 |
| duration | 300.0 | 每次动画过渡的毫秒数 |
| pagination | null | 设置 new SwiperPagination() 以显示默认分页 |
| control | null | 设置 new SwiperControl() 以显示默认控制按钮 |
分页器
分页器继承自 SwiperPlugin,SwiperPlugin 为 Swiper 提供额外的 UI。设置 new SwiperPagination() 可以显示默认分页器。
| 参数 | 默认值 | 描述 |
|---|---|---|
| 对齐方式 | Alignment.bottomCenter | 如果你想将分页放在其他位置,请更改此值 |
| 边距 | const EdgeInsets.all(10.0) | 父容器内部侧边的距离 |
| 构建器 | SwiperPagination.dots | 有两种默认样式 SwiperPagination.dots 和 SwiperPagination.fraction,两者都可以自定义 |
如果你想自定义自己的分页,你可以这样做:
new Swiper(
...,
pagination:new SwiperCustomPagination(
builder:(BuildContext context, SwiperPluginConfig config){
return new YourOwnPaginatipon();
}
)
);
控制按钮
该控件也继承自 SwiperPlugin,设置 new SwiperControl() 以显示默认控制按钮。
| 参数 | 默认值 | 描述 |
|---|---|---|
| iconPrevious | Icons.arrow_back_ios | 显示 previous 控件按钮的图标数据 |
| iconNext | Icons.arrow_forward_ios | 显示 next 的图标数据。 |
| color | Theme.of(context).primaryColor | 控制按钮颜色 |
| size | 30.0 | 控制按钮大小 |
| padding | const EdgeInsets.all(5.0) | 控制按钮内边距 |
控制器
Controller 用于控制 Swiper 的 index,开始或停止自动播放。你可以通过 new SwiperController() 创建控制器,并保存实例以便后续使用。
| 方法 | 描述 |
|---|---|
| void move(int index, {bool animation: true}) | 移动到指定的 index,是否带动画 |
| void next({bool animation: true}) | 移动到下一个 |
| void previous({bool animation: true}) | 移动到上一个 |
| void startAutoplay() | 开始自动播放 |
| void stopAutoplay() | 停止自动播放 |
自动播放
| 参数 | 默认值 | 描述 |
|---|---|---|
| autoplayDelay | 3000 | 自动播放延迟毫秒数。 |
| autoplayDisableOnInteraction | true | 如果设置为 true,当用户滑动时会禁用 autoplay。 |
布局构建
new Swiper(
itemBuilder: (BuildContext context, int index) {
return new Image.network(
"http://via.placeholder.com/288x188",
fit: BoxFit.fill,
);
},
itemCount: 10,
viewportFraction: 0.8,
scale: 0.9,
)
new Swiper(
itemBuilder: (BuildContext context, int index) {
return new Image.network(
"http://via.placeholder.com/288x188",
fit: BoxFit.fill,
);
},
itemCount: 10,
itemWidth: 300.0,
layout: SwiperLayout.STACK,
)
new Swiper(
itemBuilder: (BuildContext context, int index) {
return new Image.network(
"http://via.placeholder.com/288x188",
fit: BoxFit.fill,
);
},
itemCount: 10,
itemWidth: 300.0,
itemHeight: 400.0,
layout: SwiperLayout.TINDER,
)
创建自己的自定义动画非常容易:
new Swiper(
layout: SwiperLayout.CUSTOM,
customLayoutOption: new CustomLayoutOption(
startIndex: -1,
stateCount: 3
).addRotate([
-45.0/180,
0.0,
45.0/180
]).addTranslate([
new Offset(-370.0, -40.0),
new Offset(0.0, 0.0),
new Offset(370.0, -40.0)
]),
itemWidth: 300.0,
itemHeight: 200.0,
itemBuilder: (context, index) {
return new Container(
color: Colors.grey,
child: new Center(
child: new Text("$index"),
),
);
},
itemCount: 10)
CustomLayoutOption 旨在描述动画。指定 Swiper 中每个项目的状态非常容易。
new CustomLayoutOption(
startIndex: -1, /// Which index is the first item of array below
stateCount: 3 /// array length
).addRotate([ // rotation of every item
-45.0/180,
0.0,
45.0/180
]).addTranslate([ /// offset of every item
new Offset(-370.0, -40.0),
new Offset(0.0, 0.0),
new Offset(370.0, -40.0)
])
代码
new ConstrainedBox(
child: new Swiper(
outer:false,
itemBuilder: (c, i) {
return new Wrap(
runSpacing: 6.0,
children: [0,1,2,3,4,5,6,7,8,9].map((i){
return new SizedBox(
width: MediaQuery.of(context).size.width/5,
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new SizedBox(
child: new Container(
child: new Image.network("https://fuss10.elemecdn.com/c/db/d20d49e5029281b9b73db1c5ec6f9jpeg.jpeg%3FimageMogr/format/webp/thumbnail/!90x90r/gravity/Center/crop/90x90"),
),
height: MediaQuery.of(context).size.width * 0.12,
width: MediaQuery.of(context).size.width * 0.12,
),
new Padding(padding: new EdgeInsets.only(top:6.0),child: new Text("$i"),)
],
),
);
}).toList(),
);
},
pagination: new SwiperPagination(
margin: new EdgeInsets.all(5.0)
),
itemCount: 10,
),
constraints:new BoxConstraints.loose(new Size(screenWidth, 170.0))
),
您可以在此处找到所有自定义选项:
https://github.com/jzoom/flutter_swiper/blob/master/example/lib/src/ExampleCustom.dart
- 本文标题:flutter_swiper - 适用于 Flutter 的最佳滑动组件
- 本文链接:https://cn121.com/mobile/best-flutter-flutter-swiper.html
- 原项目:best-flutter/flutter_swiper 版权归原作者 best-flutter 及贡献者所有
- 收录信息:本站于 2026-10-21 收录本项目,本页所列协议与仓库指标均为收录当时的状态;该日期之后原项目的版本更新与协议变更,本页不作同步。
- 开源协议:收录时本项目采用 MIT(查看 LICENSE 原文),本站转载其原始文档(未改动文字,仅剔除图片与无关章节);使用、修改、分发请以该仓库 LICENSE 原文为准。
- 站点出处:本文首发于 OneTwoOne,收录自 GitHub 开源项目 best-flutter/flutter_swiper。
- 内容说明:本页正文为原项目 README 原文(英文),本站后续会翻译为中文(当前尚未译出,仅将二级标题译为中文,便于按栏目定位;标题原文可在下方原仓库中查看),仅剔除了图片与赞助等无关章节、并把相对链接改为绝对地址;页首简介为机器翻译自仓库描述。
- 引用声明:商业转载、第三方聚合或 AI 检索训练引用时,请务必保留以上来源出处、本文永久链接,以及原项目的版权声明与许可信息。
- 下架通道:若原项目此后变更或收紧了许可协议、或作者/权利人认为本站的收录方式(译文、排版适配、简介翻译等)超出其授权范围,请通过 xyd3302001@163.com 发送下架通知,并附上项目地址与本页链接。本站核实后将第一时间删除本页内容,或改为不复制原文的目录性收录;署名更正等其他要求可一并提出。