Flutter实现轮播图功能

一、在pubspec.yaml中添加:

dart 复制代码
dependencies:
  # 轮播图
  card_swiper: ^3.0.1

card_swiper: ^3.0.1,要获取最新版本:https://pub-web.flutter-io.cn/packages/card_swiper/versions,这个里面有文档可以看,如下图:

打开Versions看看文档 ,如下图:


二、在项目下的assets\images上添加图标,然后在pubspec.yaml上配置:

三、完整代码实现:

dart 复制代码
import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';

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

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: [
            SizedBox(
              height: 200,
              child: CustomSwiper(),
            ),
          ],
        ),
      ),
    );
  }
}

//custom swiper
class CustomSwiper extends StatefulWidget {
  @override
  _CustomSwiperState createState() => _CustomSwiperState();
}

class _CustomSwiperState extends State<CustomSwiper> {
  List banner = [
    {"imagePath": 'assets/images/banner1.png'},
    {"imagePath": 'assets/images/banner2.png'},
    {"imagePath": 'assets/images/banner3.png'},
  ];

  @override
  Widget build(BuildContext context) {
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.asset(
          banner[index]['imagePath'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: banner.length,
      autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      // control: const SwiperControl(),//< >
    );
  }
}

运行程序效果:

相关推荐
leluckys2 小时前
flutter 专题十二 Flutter Fair逻辑动态化架构设计与实现
flutter
sunly_2 小时前
Flutter:AnimatedIcon图标动画,自定义Icon通过延时Interval,实现交错式动画
flutter
嘟嘟叽2 小时前
初学 flutter 问题记录
flutter
__WanG2 小时前
Flutter将应用打包发布到App Store
前端·flutter·ios
leluckys2 小时前
flutter 专题十七 Flutter Flar动画实战
前端·flutter
sunly_3 小时前
Flutter:AnimatedBuilder自定义显示动画
flutter
leluckys4 小时前
flutter 专题十一 Fair原理篇Fair逻辑动态化架构设计与实现
flutter
恋猫de小郭6 小时前
IntelliJ IDEA 2024.3 K2 模式已发布稳定版,Android Studio Meerkat 预览也正式支持
android·android studio
Jinkey19 小时前
FlutterBasic - GetBuilder、Obx、GetX<Controller>、GetxController 有啥区别
android·flutter·ios
Summer不秃1 天前
Flutter之使用mqtt进行连接和信息传输的使用案例
前端·flutter