欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Flutter banner_view 轮播图的使用及实现代码

时间:2021-04-17 09:55:20|栏目:Android代码|点击:

1、前言

实现轮播图,效果如下:

2、实现

将采用 banner_view 实现:资源库地址

2.1、yaml 引入依赖

在 pubspec.yaml 声明需要引用的库,执行命令 flutter packages get 进行拉取即可使用。

banner_view: "^1.1.2"

2.2、代码中引入依赖

 在资源库地址下方,作者提供了 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,
  );
  },
 );
 }
}

总结

上一篇:XListView实现多条目网络数据刷新加载 网络加载图片

栏    目:Android代码

下一篇:Android提高之Android手机与BLE终端通信

本文标题:Flutter banner_view 轮播图的使用及实现代码

本文地址:http://www.codeinn.net/misctech/103353.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有