Flutter轮播广告插件banner_swipper的使用

Flutter轮播广告插件banner_swipper的使用

平台支持

Android iOS MacOS Web Linux Windows

使用方法

以下是一个简单的示例,展示了如何在Flutter应用中使用banner_swipper插件。

import 'package:flutter/material.dart';
import 'package:banner_swipper/src/index.dart';

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          // 第一个轮播广告
          BannerSwipper(
            const [
              'https://cdn1-manfashion.techbang.com/system/excerpt_images/13193/original/9784b0ab3adf7e48cf8119f98e15aa87.jpg?1613716683',
              'https://cdn.sortiraparis.com/images/80/98390/851995-les-gardiens-de-la-galaxie-3.jpg',
              'https://d2eib6r9tuf5y8.cloudfront.net/l/assets/img/article/article-2061-hggqyjvq/keyvisual.jpg',
            ],
            alignment: IndicatorAlignment.end,
            onTap: (index) => debugPrint('DEBUG: 点击了第$index张图片'),
          ),
          // 第二个轮播广告
          BannerSwipper(
            const [
              'asset/images/694711.jpg',
              'asset/images/759351.jpg',
              'asset/images/842583.jpg'
            ],
            sourceType: ImageSourceType.asset,
            indicatorWidth: 15.0,
            indicatorHeight: 2.0,
            indicatorBorderRadius: 1.0,
            indicatorColor: Colors.black54,
            currentIndicatorColor: Colors.blue,
            alignment: IndicatorAlignment.start,
          ),
        ],
      ),
    );
  }
}

属性介绍

以下是BannerSwipper的一些常用属性:

/// 图片资源路径
final List<String> images;

/// 自动轮播间隔
final Duration? scrollDuration;

/// 图片类型,本地或网络, 默认为网络图片
final ImageSourceType sourceType;

/// 轮播图控件宽度,默认与父容器一样
final double width;

/// 轮播图控件高度,默认与父容器一样
final double height;

/// 点击回调
final ValueChanged<int>? onTap;

/// 翻页方式
final Curve curve;

/// 小圆点的宽度
final double indicatorWidth;

/// 指示器的高度
final double indicatorHeight;

/// 指示器的颜色,默认为白色
final Color indicatorColor;

/// 指示器圆角数值
final double indicatorBorderRadius;

/// 指示器 padding
final EdgeInsets? indicatorPadding;

/// 指示器间隔,默认 5.0
final double indicatorSpace;

/// 选中状态下的指示器颜色, 默认为主题颜色
final Color? currentIndicatorColor;

/// 指示器基于父视图的对齐方式,默认为居中对齐
final IndicatorAlignment alignment;

完整示例Demo

以下是完整的示例代码,展示如何在一个Flutter应用中使用banner_swipper插件。

import 'package:flutter/material.dart';
import 'package:banner_swipper/src/index.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // 这是你的应用根组件
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          // 第一个轮播广告
          BannerSwipper(
            const [
              'https://cdn1-manfashion.techbang.com/system/excerpt_images/13193/original/9784b0ab3adf7e48cf8119f98e15aa87.jpg?1613716683',
              'https://cdn.sortiraparis.com/images/80/98390/851995-les-gardiens-de-la-galaxie-3.jpg',
              'https://d2eib6r9tuf5y8.cloudfront.net/l/assets/img/article/article-2061-hggqyjvq/keyvisual.jpg',
            ],
            alignment: IndicatorAlignment.end,
            onTap: (index) => debugPrint('DEBUG: 点击了第$index张图片'),
          ),
          // 第二个轮播广告
          BannerSwipper(
            const [
              'asset/images/694711.jpg',
              'asset/images/759351.jpg',
              'asset/images/842583.jpg'
            ],
            sourceType: ImageSourceType.asset,
            indicatorWidth: 15.0,
            indicatorHeight: 2.0,
            indicatorBorderRadius: 1.0,
            indicatorColor: Colors.black54,
            currentIndicatorColor: Colors.blue,
            alignment: IndicatorAlignment.start,
          ),
        ],
      ),
    );
  }
}

更多关于Flutter轮播广告插件banner_swipper的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter轮播广告插件banner_swipper的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter中使用banner_swiper插件来实现轮播广告效果的代码示例。banner_swiper是一个非常流行的轮播图插件,它提供了丰富的功能和配置选项。

首先,你需要在pubspec.yaml文件中添加banner_swiper依赖:

dependencies:
  flutter:
    sdk: flutter
  banner_swiper: ^2.0.0  # 请注意版本号,这里使用的是2.0.0,实际使用时请检查最新版本

然后,运行flutter pub get来安装依赖。

接下来,在你的Dart文件中,你可以按照以下方式使用BannerSwiper来实现轮播广告效果:

import 'package:flutter/material.dart';
import 'package:banner_swiper/banner_swiper.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Banner Swiper Demo'),
        ),
        body: Center(
          child: BannerSwiper(
            itemCount: 3,  // 轮播图的数量
            itemBuilder: (context, index) {
              // 这里返回每个轮播项的内容
              return Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: NetworkImage(
                      'https://via.placeholder.com/600x300?text=Ad+$index',
                    ),
                    fit: BoxFit.cover,
                  ),
                ),
              );
            },
            autoplay: true,  // 是否自动播放
            autoplayDelay: 2000,  // 自动播放的延迟时间(毫秒)
            autoplayDisableOnInteraction: true,  // 用户交互后是否停止自动播放
            indicatorColor: Colors.white,  // 指示器的颜色
            indicatorActiveColor: Colors.blue,  // 激活状态的指示器颜色
            pagination: Pagination.dots,  // 分页指示器的样式
            dotSize: 6.0,  // 点的大小
            dotSpacing: 12.0,  // 点的间距
            leftMargin: 10.0,  // 左侧的外边距
            rightMargin: 10.0,  // 右侧的外边距
            itemWidth: double.infinity,  // 轮播项的宽度
            itemHeight: 300.0,  // 轮播项的高度
            layout: SwiperLayout.DEFAULT,  // 布局方式
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,并在其中使用了BannerSwiper组件来实现轮播广告效果。我们配置了轮播图的数量、每个轮播项的内容、自动播放的相关参数以及分页指示器的样式等。

请注意,NetworkImage用于从网络加载图片,你可以根据自己的需求替换为本地图片或其他图片加载方式。

这个示例展示了banner_swiper插件的基本用法,你可以根据实际需求进一步定制和扩展功能。

回到顶部