Skip to content
On this page

Swipe 轮播

代码演示

基本用法

dart
Swipe(children: [
  buildCarouselBox(0),
  buildCarouselBox(1),
  buildCarouselBox(2),
]),

自动轮播

dart
Swipe(
  count: 3,
  autoplay: const Duration(seconds: 3),
  builder: (index) => buildCarouselBox(index),
),

无限滑动

dart
Swipe(loop: true, children: [
  buildCarouselBox(0),
  buildCarouselBox(1),
  buildCarouselBox(2),
]),

监听 onChange 事件

dart
Swipe(
  loop: true,
  onChange: (index) => ToastStatic.show(
    context,
    message: "onChange: $index",
  ),
  children: [
    buildCarouselBox(0),
    buildCarouselBox(1),
    buildCarouselBox(2),
  ],
),

自定义滑块大小

dart
Swipe(padEnds: false, viewportFraction: 0.8, children: [
  buildCarouselBox(0),
  buildCarouselBox(1),
  buildCarouselBox(2),
]),

自定义指示器

dart
Swipe(
  loop: true,
  indicator: (e) {
    return Positioned(
      right: 10,
      bottom: 10,
      child: Container(
        padding: const EdgeInsets.all(10),
        color: TailColors.gray_400.withAlpha(0xAA),
        child: TailTypo()
            .text_color(TailColors.gray_100)
            .Text("${e.active}/${e.total}"),
      ),
    );
  },
  children: [
    buildCarouselBox(0),
    buildCarouselBox(1),
    buildCarouselBox(2),
  ],
),

API

Props类型描述
autoplayDuration自动播放间隔时间,默认 0 不自动播放
durationDuration轮播速度,默认半秒
initialIndexint初始位置
heightdouble | double Function(BoxConstraints con)高度,或以宽度取高度;与 aspectRatio 互斥
aspectRatiodouble宽高比,与 height 互斥
indicatorWidget | Widget Function({active, total})当前位置指示器
gesturebool允许手势切换
loopbool循环切换
countint滑块数量,配合 builder 使用
viewportFractiondouble滑块大小
onChangeFunction(int index)切换时触发
childrenList<Widget>滑块列表,与 builder 互斥
builderFunction(int index)构建滑块,与 children 互斥
padEndsbool紧凑滑块,配合 viewportFraction 使用