Flutter 插件筆記 | 輪播圖 flutter_swiper |

機緣巧合裙顽,我了解到 packages 下的輪播組件 swiper耗绿,記錄一下姑蓝。

?packages 鏈接flutter_swiper
?Github 鏈接best-flutter/flutter_swiper

Get

?在項目目錄中的pubspec.yaml文件中的dependencies里導(dǎo)入flutter_swiper: ^1.1.6。運行flutter packages get储狭。

dependencies:
   # 最新的版本,版本會迭代捣郊,需保持最新的
   flutter_swiper: ^1.1.6

屬性解讀(常用)

Swiper(
  scrollDirection: Axis.horizontal,// 方向 Axis.horizontal  Axis.vertical
  itemCount: 4, // 展示數(shù)量
  autoplay: true,// 自動翻頁
  itemBuilder:(){...},// 布局構(gòu)建
  onTap:(){...}, // 點擊時間
  pagination: SwiperPagination(), // 分頁指示
  viewportFraction: 0.8, // 視窗比例
  layout: SwiperLayout.STACK, // 布局方式 
  itemWidth: MediaQuery.of(context).size.width, // 條目寬度
  itemHeight: MediaQuery.of(context).size.height, // 條目高度
  autoplayDisableOnInteraction: true, // 用戶進(jìn)行操作時停止自動翻頁
  loop: true, // 無線輪播
  indicatorLayout: PageIndicatorLayout.SLIDE, // 指標(biāo)布局 試了半天也沒試出來這是啥
)

使用示例

?在這展示幾個小栗子辽狈,可直接拿去復(fù)制。

?示例一 四張圖片 自動翻頁 左右控制按鈕 頁面指示器(點)


?去掉control:屬性呛牲,圖上的左右控制翻頁按鈕就會消失了刮萌。

Container(
  height: ScreenUtil().setHeight(300), // 高度 插件 flutter_screenutil
  child: Swiper(
    scrollDirection: Axis.horizontal,// 橫向
    itemCount: 4,// 數(shù)量
    autoplay: true, // 自動翻頁
    itemBuilder: (BuildContext context, int index) {
      return Image.network("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=500542857,4026327610&fm=26&gp=0.jpg",
        fit: BoxFit.fill);
    }, // 構(gòu)建
    onTap: (index) {print('點擊了第${index}');},// 點擊事件 onTap
    pagination: SwiperPagination(// 分頁指示器
      alignment: Alignment.bottomCenter,// 位置 Alignment.bottomCenter 底部中間
      margin: const EdgeInsets.fromLTRB(0, 0, 0, 5),// 距離調(diào)整
      builder: DotSwiperPaginationBuilder( // 指示器構(gòu)建
        space: ScreenUtil().setWidth(5),// 點之間的間隔
        size: ScreenUtil().setWidth(10), // 沒選中時的大小
        activeSize: ScreenUtil().setWidth(12),// 選中時的大小
        color: Colors.black54,// 沒選中時的顏色
        activeColor: Colors.white)),// 選中時的顏色
    control: new SwiperControl(color: Colors.pink), // 頁面控制器 左右翻頁按鈕
    scale: 0.95,// 兩張圖片之間的間隔
    ),
 ),

?示例二 四張圖片 自動翻頁 頁面指示器(數(shù)字)

Container(
  height: ScreenUtil().setHeight(300), // 高度
  child: Swiper(
    scrollDirection: Axis.horizontal,// 橫向
    itemCount: imageList.length,// 數(shù)量
    autoplay: true,// 自動翻頁
    itemBuilder: (BuildContext context, int index) {return imageList[index];},// 構(gòu)建
    onTap: (index) {print('點擊了第${index}');},// 點擊事件 onTap
    // 分頁指示器
    pagination: SwiperPagination(
        alignment: Alignment.bottomRight,// 位置 Alignment.bottomCenter 底部中間
        margin: const EdgeInsets.fromLTRB(0, 0, 20, 10),// 距離調(diào)整
        builder: FractionPaginationBuilder( // 指示器構(gòu)建
          color: Colors.white,// 字體顏色
          activeColor: Colors.yellow, // 當(dāng)前的指示字體顏色
          fontSize: ScreenUtil().setSp(30),// 字體大小
          activeFontSize: ScreenUtil().setSp(35),// 當(dāng)前的指示字體大小
        )
    ),
    scale: 0.95,// 兩張圖片之間的間隔
  ),
)

?示例三

Container(
  color: Colors.black,
  padding: const EdgeInsets.only(top: 10),
  height: ScreenUtil().setHeight(380), // 高度
  child: Swiper(
    scrollDirection: Axis.horizontal, // 橫向
    itemCount: 4,// 數(shù)量
    autoplay: true,// 自動翻頁
    itemBuilder: (BuildContext context, int index) {// 構(gòu)建
      return Container(
        margin: const EdgeInsets.only(bottom: 30),
        decoration: BoxDecoration(
            image: DecorationImage(
                image: NetworkImage(
                    'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3932546523,304539533&fm=26&gp=0.jpg'),
                     fit: BoxFit.fill),
                borderRadius: BorderRadius.all(Radius.circular(10))),
      );
    },
    onTap: (index) {print('點擊了第${index}');},// 點擊事件 onTap
    pagination: SwiperPagination( // 分頁指示器
        alignment: Alignment.bottomCenter,// 位置 Alignment.bottomCenter 底部中間
        margin: const EdgeInsets.fromLTRB(0, 0, 0, 5), // 距離調(diào)整
        builder: DotSwiperPaginationBuilder(
          activeColor: Colors.yellow,
          color: Colors.white,
          size: ScreenUtil().setWidth(15),
          activeSize: ScreenUtil().setWidth(25),
          space: ScreenUtil().setWidth(10),
       )),
    viewportFraction: 0.8,// 當(dāng)前視窗展示比例 小于1可見上一個和下一個視窗
    scale: 0.8, // 兩張圖片之間的間隔
  ),
)

?示例四


?代碼中的_imageHttpsList是一個存儲了四個圖片鏈接List

Container(
  color: Colors.black,
  padding: const EdgeInsets.only(top: 10),
  height: ScreenUtil().setHeight(380), // 高度
  child: Swiper(
    scrollDirection: Axis.horizontal, // 橫向
    itemCount: _imageHttpsList.length,// 數(shù)量
    autoplay: false,// 自動翻頁
    itemBuilder: (BuildContext context, int index) {// 構(gòu)建
      return Container(
        margin: const EdgeInsets.only(bottom: 30),
        decoration: BoxDecoration(
            image: DecorationImage(
                image: NetworkImage(_imageHttpsList[index]),fit: BoxFit.fill),
            borderRadius: BorderRadius.all(Radius.circular(10))),
      );
    },
    onTap: (index) {print('點擊了第${index}');},// 點擊事件 onTap
    pagination: SwiperPagination( // 分頁指示器
        alignment: Alignment.bottomCenter,// 位置 Alignment.bottomCenter 底部中間
        margin: const EdgeInsets.fromLTRB(0, 0, 0, 5), // 距離調(diào)整
        builder: FractionPaginationBuilder(
          activeColor: Colors.yellow,
          color: Colors.white,
          fontSize: ScreenUtil().setSp(15),
          activeFontSize: ScreenUtil().setSp(25),
       )),
    viewportFraction: 0.8,// 當(dāng)前視窗展示比例 小于1可見上一個和下一個視窗
    scale: 0.8, // 兩張圖片之間的間隔
    layout: SwiperLayout.TINDER,
    itemWidth: MediaQuery.of(context).size.width,
    itemHeight: MediaQuery.of(context).size.height,
  ),
)

?示例五


?這里面有個很神奇的地方itemWidth: MediaQuery.of(context).size.width - 100,以后某些特殊的寬度可以這樣設(shè)置娘扩。

Container(
  color: Colors.black,
  padding: const EdgeInsets.only(top: 10),
  height: ScreenUtil().setHeight(380), // 高度
  child: Swiper(
    scrollDirection: Axis.horizontal, // 橫向
    itemCount: _imageHttpsList.length,// 數(shù)量
    autoplay: false,// 自動翻頁
    itemBuilder: (BuildContext context, int index) {// 構(gòu)建
      return Container(
        margin: const EdgeInsets.only(bottom: 30),
        decoration: BoxDecoration(
            image: DecorationImage(
                image: NetworkImage(
                    _imageHttpsList[index]),
                fit: BoxFit.fill),
            borderRadius: BorderRadius.all(Radius.circular(10))),
      );
    },
    onTap: (index) {print('點擊了第${index}');},// 點擊事件 onTap
    pagination: SwiperPagination( // 分頁指示器
        alignment: Alignment.bottomCenter,// 位置 Alignment.bottomCenter 底部中間
        margin: const EdgeInsets.fromLTRB(0, 0, 0, 5), // 距離調(diào)整
        builder: FractionPaginationBuilder(
          activeColor: Colors.yellow,
          color: Colors.white,
          fontSize: ScreenUtil().setSp(15),
          activeFontSize: ScreenUtil().setSp(25),
        )),
    viewportFraction: 0.8,// 當(dāng)前視窗展示比例 小于1可見上一個和下一個視窗
    scale: 0.8, // 兩張圖片之間的間隔
    layout: SwiperLayout.STACK,
    itemWidth: MediaQuery.of(context).size.width - 100,
    itemHeight: MediaQuery.of(context).size.height,
  ),
)

?示例六


?這個只是把示例五的scrollDirection改成Axis.vertical着茸。

Container(
  ...
  child: Swiper(
    scrollDirection: Axis.vertical, // 豎向
    ...
  ),
)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市畜侦,隨后出現(xiàn)的幾起案子元扔,更是在濱河造成了極大的恐慌,老刑警劉巖旋膳,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件澎语,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機擅羞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門尸变,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人减俏,你說我怎么就攤上這事召烂。” “怎么了娃承?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵奏夫,是天一觀的道長。 經(jīng)常有香客問我历筝,道長酗昼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任梳猪,我火速辦了婚禮麻削,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘春弥。我一直安慰自己呛哟,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布匿沛。 她就那樣靜靜地躺著扫责,像睡著了一般。 火紅的嫁衣襯著肌膚如雪俺祠。 梳的紋絲不亂的頭發(fā)上公给,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機與錄音蜘渣,去河邊找鬼淌铐。 笑死,一個胖子當(dāng)著我的面吹牛蔫缸,可吹牛的內(nèi)容都是我干的腿准。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼拾碌,長吁一口氣:“原來是場噩夢啊……” “哼吐葱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起校翔,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤弟跑,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后防症,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體孟辑,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡哎甲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了饲嗽。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片炭玫。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖貌虾,靈堂內(nèi)的尸體忽然破棺而出吞加,到底是詐尸還是另有隱情,我是刑警寧澤尽狠,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布衔憨,位于F島的核電站,受9級特大地震影響晚唇,放射性物質(zhì)發(fā)生泄漏巫财。R本人自食惡果不足惜盗似,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一哩陕、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赫舒,春花似錦悍及、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缺猛,卻和暖如春缨叫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背荔燎。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工耻姥, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人有咨。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓琐簇,卻偏偏與公主長得像,于是被迫代替她去往敵國和親座享。 傳聞我的和親對象是個殘疾皇子婉商,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 在過去七年里,作為Hotwire.com的聯(lián)合創(chuàng)始人和房地產(chǎn)資訊網(wǎng)站Zillow的首席執(zhí)行官(注:zillow相當(dāng)...
    米逍遙閱讀 766評論 0 0
  • 陽氣上升渣叛,萬物復(fù)蘇 生生不息丈秩,又是輪回 減去冬藏,復(fù)又春歸 死去活來淳衙,來來回回 阿彌陀佛蘑秽,阿彌陀佛 自然而然挽唉,不已己悲
    雨墨_1eac閱讀 266評論 0 3
  • 家電家具大家覺得階梯電價土豆燉雞東京鐵塔到家太多太多6健康6記憶猶新九陰真經(jīng)有辛苦辛苦也許可以7kkkkkkkkk...
    1f55b75e5847閱讀 212評論 0 0