flutter_swiper

Flutter 中最好的滑动器,具有多种布局,无限循环。兼容 Android 和 iOS。

:sparkles::sparkles: 新功能:PageTransformer

终于,我们有了类似Android的PageTransformer,只需为Swiper设置一个transformer
它会返回一个已转换的widget。目前仅支持DEFAULT布局。
感谢@FlutterRocks,你做得太棒了?。

我们现在正在使用这个项目transformer_page_view

:sparkles::sparkles: 新功能:布局

展示

Horizontal

Vertical

Custom Pagination

Custom Pagination

Phone

Example

查看更多

安装

添加


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 设置为false可禁用连续循环模式。
index 0 初始幻灯片的索引号。
自动播放 设置为true以启用自动播放模式。
onIndexChanged void onIndexChanged(int index) 当用户滑动或自动播放时,会调用此函数并传入新的索引
onTap void onTap(int index) 当用户点击UI时调用。
duration 300.0 每次事务动画花费的毫秒数
分页 设置new SwiperPagination()以显示默认分页
控制 设置new SwiperControl()以显示默认控制按钮

分页

分页继承自SwiperPluginSwiperPluginSwiper提供了额外的UI。设置new SwiperPagination()以显示默认分页。

参数 默认值 描述
alignment Alignment.bottomCenter 如果要将分页放在其他位置,请更改此值
margin const EdgeInsets.all(10.0) 父容器内侧之间的距离。
builder SwiperPagination.dots 有两种默认样式SwiperPagination.dotsSwiperPagination.fraction,都可以自定义。

如果你想自定义自己的分页,可以这样做:

new Swiper(
    ...,
    pagination:new SwiperCustomPagination(
        builder:(BuildContext context, SwiperPluginConfig config){
            return new YourOwnPaginatipon();
        }
    )
);

控制按钮

控制也继承自SwiperPlugin,设置new SwiperControl()以显示默认控制按钮。

参数 默认值 描述
iconPrevious Icons.arrow_back_ios 用于显示上一页控制按钮的图标数据
iconNext Icons.arrow_forward_ios 用于显示下一页的图标数据。
color Theme.of(context).primaryColor 控制按钮颜色
size 30.0 控制按钮大小
padding const EdgeInsets.all(5.0) 控制按钮内边距

控制器

Controller用于控制Swiperindex,启动或停止自动播放。您可以通过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,则在用户滑动时禁用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)
  ])

代码

Example

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))
),

GitHub

https://github.com/best-flutter/flutter_swiper