Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼
1、前言
實(shí)現(xiàn)輪播圖,效果如下:
2、實(shí)現(xiàn)
將采用 banner_view 實(shí)現(xiàn):資源庫(kù)地址
2.1、yaml 引入依賴
在 pubspec.yaml 聲明需要引用的庫(kù),執(zhí)行命令 flutter packages get 進(jìn)行拉取即可使用。
banner_view: "^1.1.2"
2.2、代碼中引入依賴
在資源庫(kù)地址下方,作者提供了 banner_view
的幾種展示方式。
import 'package:flutter/material.dart'; import 'package:banner_view/banner_view.dart'; import 'Pair.dart'; import 'factory/BannerItemFactory.dart'; class BannerViewPage extends StatefulWidget { @override _BannerViewPageState createState() => new _BannerViewPageState(); } class _BannerViewPageState extends State<BannerViewPage> { @override Widget build(BuildContext context) { return new Scaffold( body: new Container( child: new Column( children: <Widget>[ new Container( alignment: Alignment.center, height: 200.0, child: this._bannerView0(), padding: EdgeInsets.only(bottom: 10.0), ) ], ), ), ); } /** * 第一種方式 */ BannerView _bannerView0() { // 盛放圖片的 List List<Pair<String, Color>> param = [ Pair.create('https://p5.ssl.qhimg.com/dm/456_209_/t01f43c5849ef5f521a.jpg', Colors.red[500]), Pair.create('https://p.ssl.qhimg.com/t0171bb61911ebe8899.jpg', Colors.green[500]), Pair.create('https://p.ssl.qhimg.com/t01ee77978d3a95a3ae.jpg', Colors.blue[500]), ]; return new BannerView( BannerItemFactory.banners(param), ); // return new BannerView( // BannerItemFactory.banners(param), // indicatorMargin: 10.0, // indicatorNormal: new Container( // width: 5.0, // height: 5.0, // decoration: new BoxDecoration( // color: Colors.green, // shape: BoxShape.rectangle, // ), // ), // indicatorSelected: new Container( // width: 15.0, // height: 5.0, // decoration: new BoxDecoration( // color: Colors.black, // shape: BoxShape.rectangle, // borderRadius: new BorderRadius.all( // new Radius.circular(5.0), // ), // ), // ), // indicatorBuilder: (context, indicator) { // Widget cc = new Container( // padding: new EdgeInsets.symmetric(horizontal: 20.0,), // height: 44.0, // width: double.infinity, // color: Colors.grey[300], // child: indicator, // ); // return new Opacity( // opacity: 0.5, // child: cc, // ); // }, // ); } /** * 第二種方式 */ BannerView _bannerView() { var pre = 'https://raw.githubusercontent.com/yangxiaoweihn/Assets/master'; List<Pair<String, Color>> param = [ Pair.create('https://raw.githubusercontent.com/yangxiaoweihn/Assets/master/cars/car_0.jpg', Colors.red[100]), Pair.create('https://raw.githubusercontent.com/yangxiaoweihn/Assets/master/cartoons/ct_0.jpg', Colors.green[100]), Pair.create('https://raw.githubusercontent.com/yangxiaoweihn/Assets/master/pets/cat_1.jpg', Colors.blue[100]), Pair.create('https://raw.githubusercontent.com/yangxiaoweihn/Assets/master/scenery/s_1.jpg', Colors.yellow[100]), Pair.create('https://raw.githubusercontent.com/yangxiaoweihn/Assets/master/cartoons/ct_1.jpg', Colors.red[100]), // Pair.create('$pre/cartoons/ct_1.jpg', Colors.red[100]), ]; return new BannerView( BannerItemFactory.banners(param), indicatorMargin: 10.0, indicatorNormal: new Container( width: 5.0, height: 5.0, decoration: new BoxDecoration( color: Colors.green, shape: BoxShape.rectangle, ), ), indicatorSelected: new Container( width: 15.0, height: 5.0, decoration: new BoxDecoration( color: Colors.black, shape: BoxShape.rectangle, borderRadius: new BorderRadius.all( new Radius.circular(5.0), ), ), ), indicatorBuilder: (context, indicator) { Widget cc = new Container( padding: new EdgeInsets.symmetric(horizontal: 20.0,), height: 44.0, width: double.infinity, color: Colors.grey[300], child: indicator, ); return new Opacity( opacity: 0.5, child: cc, ); }, ); } }
總結(jié)
以上所述是小編給大家介紹的Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Android 用 camera2 API 自定義相機(jī)
本文主要介紹了Android 用 camera2 API 自定義相機(jī)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04Android中實(shí)現(xiàn)水平滑動(dòng)(橫向滑動(dòng))ListView示例
這篇文章主要介紹了Android中實(shí)現(xiàn)水平滑動(dòng)(橫向滑動(dòng))ListView示例,本文用自己封裝一個(gè)控件的方法解決了這個(gè)需求,需要的朋友可以參考下2015-06-06Flutter app頁(yè)面路由以及路由攔截的實(shí)現(xiàn)
本篇介紹了介紹了Flutter如何使用路由來實(shí)現(xiàn)頁(yè)面的跳轉(zhuǎn),從而簡(jiǎn)化頁(yè)面之間的耦合,并可以實(shí)現(xiàn)路由攔截。2021-06-06詳解Android使用Gradle統(tǒng)一配置依賴管理
本篇文章主要介紹了詳解Android 使用 Gradle 統(tǒng)一配置依賴管理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android?NDK開發(fā)(C語言--聯(lián)合體與枚舉)
這篇文章主要介紹了Android?NDK開發(fā)C語言聯(lián)合體與枚舉,共用體是一種特殊的數(shù)據(jù)類型,允許您在相同的內(nèi)存位置存儲(chǔ)不同的數(shù)據(jù)類型。您可以定義一個(gè)帶有多成員的共用體,但是任何時(shí)候只能有一個(gè)成員帶有值。下面詳細(xì)介紹該內(nèi)容,需要的朋友可以參考一下2021-12-12Android開發(fā)之Notification通知用法詳解
這篇文章主要介紹了Android開發(fā)之Notification通知用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Notification通知的功能、參數(shù)、定義及使用方法,需要的朋友可以參考下2016-11-11Android自定義view仿微信刷新旋轉(zhuǎn)小風(fēng)車
這篇文章主要介紹了Android自定義view仿微信刷新旋轉(zhuǎn)小風(fēng)車,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12