初学Flutter:swiper实现

news2024/11/26 23:48:41

效果展示:

flutter swiper

1、安装 card_swiper 

2、引入card_swiper

import 'package:card_swiper/card_swiper.dart';

3、使用

这里我主要是对官网例子进行实践,主要是5种常用的swiper

1、普遍的swiper

//custom swiper
class CustomSwiper extends StatefulWidget {
  const CustomSwiper({super.key, required this.banner});
  final List banner;

  @override
  _CustomSwiperState createState() => _CustomSwiperState();
}

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

2、layouts swiper

//layouts swiper
class MyLayoutSwiper extends StatefulWidget {
  const MyLayoutSwiper({super.key, required this.banner});
  final List banner;

  @override
  _MyLayoutSwiperState createState() => _MyLayoutSwiperState();
}

class _MyLayoutSwiperState extends State<MyLayoutSwiper> {
  @override
  Widget build(BuildContext context) {
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.network(
          widget.banner[index]['banner_url'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: widget.banner.length,
      // autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      viewportFraction: 0.8,
      scale: 0.9,
    );
  }
}

3、STACK Layout swiper

// STACK Layout swiper
class StackLayoutSwiper extends StatefulWidget {
  const StackLayoutSwiper({super.key, required this.banner});
  final List banner;

  @override
  _StackLayoutSwiperState createState() => _StackLayoutSwiperState();
}

class _StackLayoutSwiperState extends State<StackLayoutSwiper> {
  @override
  Widget build(BuildContext context) {
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.network(
          widget.banner[index]['banner_url'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: widget.banner.length,
      // autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      viewportFraction: 0.8,
      scale: 0.9,
      itemWidth: 300.0,
      layout: SwiperLayout.STACK,
    );
  }
}

4、TINDER Layout Swiper

// TINDER Layout Swiper
class TinderLayoutSwiper extends StatefulWidget {
  const TinderLayoutSwiper({super.key, required this.banner});
  final List banner;

  @override
  _TinderLayoutSwiperState createState() => _TinderLayoutSwiperState();
}

class _TinderLayoutSwiperState extends State<TinderLayoutSwiper> {
  @override
  Widget build(BuildContext context) {
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.network(
          widget.banner[index]['banner_url'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: widget.banner.length,
      // autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      viewportFraction: 0.8,
      scale: 0.9,
      itemWidth: 300.0,
      itemHeight: 400.0,
      layout: SwiperLayout.TINDER,
    );
  }
}

5、CUSTOM LAYOUT Swiper

// TINDER Layout Swiper
class TinderLayoutSwiper extends StatefulWidget {
  const TinderLayoutSwiper({super.key, required this.banner});
  final List banner;

  @override
  _TinderLayoutSwiperState createState() => _TinderLayoutSwiperState();
}

class _TinderLayoutSwiperState extends State<TinderLayoutSwiper> {
  @override
  Widget build(BuildContext context) {
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.network(
          widget.banner[index]['banner_url'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: widget.banner.length,
      // autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      viewportFraction: 0.8,
      scale: 0.9,
      itemWidth: 300.0,
      itemHeight: 400.0,
      layout: SwiperLayout.TINDER,
    );
  }
}


// CUSTOM LAYOUT Swiper
class CustomLayoutSwiper extends StatefulWidget {
  const CustomLayoutSwiper({super.key, required this.banner});
  final List banner;

  @override
  _CustomLayoutSwiperState createState()=>_CustomLayoutSwiperState();

}
class _CustomLayoutSwiperState extends State<CustomLayoutSwiper>{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.network(
          widget.banner[index]['banner_url'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: widget.banner.length,
      // autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      viewportFraction: 0.8,
      scale: 0.9,
      itemWidth: 300.0,
      itemHeight: 400.0,
      layout: SwiperLayout.CUSTOM,
       customLayoutOption: CustomLayoutOption(
    startIndex: -1,
    stateCount: 3
  )..addRotate([
    -45.0/180,
    0.0,
    45.0/180
  ])..addTranslate([
    Offset(-370.0, -40.0),
    Offset(0.0, 0.0),
    Offset(370.0, -40.0)
  ]),
    );
  }
  
}

main.dart全部代码

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple)),
        home: const Home());
  }
}

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  List banner = [
    {
      "banner_url":
          "https://ts1.cn.mm.bing.net/th/id/R-C.8e091d0e6a2b9bcb27738cd96e22f699?rik=M4LFAVmmb38W6A&riu=http%3a%2f%2fi3.img.969g.com%2fdown%2fimgx2013%2f01%2f21%2f206_170432_901de.jpg&ehk=564uTBfxvugUBybGO2%2bTpMUNgJEa6rUfSrc1sonLso8%3d&risl=&pid=ImgRaw&r=0"
    },
    {
      "banner_url":
          "https://ts1.cn.mm.bing.net/th/id/R-C.abd4829c9387ec1bfd1a276a5c1da122?rik=dl55Voqxy4wINQ&riu=http%3a%2f%2fi1.073img.com%2f140306%2f4343693_144142_1.jpg&ehk=qvhMjT0iGQT5DhH8MTkAzJqUpjHJRScTQTT5hj%2forLM%3d&risl=&pid=ImgRaw&r=0"
    },
    {
      "banner_url":
          "https://ts1.cn.mm.bing.net/th/id/R-C.ddfbf9ed55354323035f947515fe0233?rik=0KFdFk3RHUzBdg&riu=http%3a%2f%2fi1.img.969g.com%2fdown%2fimgx2013%2f01%2f05%2f206_164916_4fab0.jpg&ehk=Vqb%2f5c3%2fW5n39t7bAae6YpUlfG4ibEbf9lpETY5Xz4c%3d&risl=&pid=ImgRaw&r=0"
    }
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('flutter轮播图学习'), centerTitle: true),
        // body: Container(child: Text('pp')),
        body: SingleChildScrollView(
          child: Column(children: [
            const Text('Custom swiper',
                style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
            SizedBox(
                height: 200,
                child: CustomSwiper(
                  banner: banner,
                )),
            const Text('Build in layouts ',
                style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
            SizedBox(
                height: 200,
                child: MyLayoutSwiper(
                  banner: banner,
                )),
            const Text('Build in STACK layouts ',
                style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
            SizedBox(
                height: 200,
                child: StackLayoutSwiper(
                  banner: banner,
                )),
            const Text('Build in  TINDER layouts ',
                style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
            SizedBox(
                height: 200,
                child: TinderLayoutSwiper(
                  banner: banner,
                )),

                const Text('Build in  CUSTOM layouts ',
                style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold)),
            SizedBox(
                height: 200,
                child: CustomLayoutSwiper(
                  banner: banner,
                )),
          ]),
        ));
  }
}

//custom swiper
class CustomSwiper extends StatefulWidget {
  const CustomSwiper({super.key, required this.banner});
  final List banner;

  @override
  _CustomSwiperState createState() => _CustomSwiperState();
}

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

//layouts swiper
class MyLayoutSwiper extends StatefulWidget {
  const MyLayoutSwiper({super.key, required this.banner});
  final List banner;

  @override
  _MyLayoutSwiperState createState() => _MyLayoutSwiperState();
}

class _MyLayoutSwiperState extends State<MyLayoutSwiper> {
  @override
  Widget build(BuildContext context) {
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.network(
          widget.banner[index]['banner_url'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: widget.banner.length,
      // autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      viewportFraction: 0.8,
      scale: 0.9,
    );
  }
}

// STACK Layout swiper
class StackLayoutSwiper extends StatefulWidget {
  const StackLayoutSwiper({super.key, required this.banner});
  final List banner;

  @override
  _StackLayoutSwiperState createState() => _StackLayoutSwiperState();
}

class _StackLayoutSwiperState extends State<StackLayoutSwiper> {
  @override
  Widget build(BuildContext context) {
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.network(
          widget.banner[index]['banner_url'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: widget.banner.length,
      // autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      viewportFraction: 0.8,
      scale: 0.9,
      itemWidth: 300.0,
      layout: SwiperLayout.STACK,
    );
  }
}

// TINDER Layout Swiper
class TinderLayoutSwiper extends StatefulWidget {
  const TinderLayoutSwiper({super.key, required this.banner});
  final List banner;

  @override
  _TinderLayoutSwiperState createState() => _TinderLayoutSwiperState();
}

class _TinderLayoutSwiperState extends State<TinderLayoutSwiper> {
  @override
  Widget build(BuildContext context) {
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.network(
          widget.banner[index]['banner_url'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: widget.banner.length,
      // autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      viewportFraction: 0.8,
      scale: 0.9,
      itemWidth: 300.0,
      itemHeight: 400.0,
      layout: SwiperLayout.TINDER,
    );
  }
}


// CUSTOM LAYOUT Swiper
class CustomLayoutSwiper extends StatefulWidget {
  const CustomLayoutSwiper({super.key, required this.banner});
  final List banner;

  @override
  _CustomLayoutSwiperState createState()=>_CustomLayoutSwiperState();

}
class _CustomLayoutSwiperState extends State<CustomLayoutSwiper>{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Swiper(
      itemBuilder: (BuildContext context, int index) {
        return Image.network(
          widget.banner[index]['banner_url'],
          fit: BoxFit.fill,
        );
      },
      onTap: (index) {
        print(index);
      },
      itemCount: widget.banner.length,
      // autoplay: true,
      pagination: const SwiperPagination(
          builder: DotSwiperPaginationBuilder(
              color: Color(0xFFFFFFFF), activeColor: Color(0xFFFF4646))),
      viewportFraction: 0.8,
      scale: 0.9,
      itemWidth: 300.0,
      itemHeight: 400.0,
      layout: SwiperLayout.CUSTOM,
       customLayoutOption: CustomLayoutOption(
    startIndex: -1,
    stateCount: 3
  )..addRotate([
    -45.0/180,
    0.0,
    45.0/180
  ])..addTranslate([
    Offset(-370.0, -40.0),
    Offset(0.0, 0.0),
    Offset(370.0, -40.0)
  ]),
    );
  }
  
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1170504.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

下载安装各种版本的Vscode以及解决VScode官网下载慢的问题

下载指定版本 在Vscode官网 Vscode官网更新子页 这里的左侧栏点击其中一个会跳转到某个版本&#xff0c;或者在官网子页 https://code.visualstudio.com/updates的后面跟上需要的版本号即可完成目标版本下载页面的跳转 选择Linux里的ARM包不会自动下载而是跳转到另一个页面 …

vue-admin-template 安装遇到的问题

vue-element-admin 是一个后台前端解决方案&#xff0c;它基于 vue 和 element-ui实现。 参考文档&#xff1a; 官网&#xff1a; https://panjiachen.github.io/vue-element-admin-site/zh/guide/#%E5%8A%9F%E8%83%BD遇到的问题&#xff1a; npm ERR! Error while executing…

移动硬盘怎么加密?移动硬盘加密怎么设置?

在工作中&#xff0c;我们经常需要使用移动硬盘来保存重要数据&#xff0c;但是这样却不能保护重要数据的安全。所以&#xff0c;我们可以使用加密来保护移动硬盘。那么&#xff0c;移动硬盘要怎么加密呢&#xff1f; U盘超级加密3000 U盘超级加密3000是一款专业的移动储存设备…

Java类加载机制(类加载器,双亲委派模型,热部署示例)

Java类加载机制 类加载器类加载器的执行流程类加载器的种类加载器之间的关系ClassLoader 的主要方法Class.forName()与ClassLoader.loadClass()区别 双亲委派模型双亲委派 类加载流程优缺点 热部署简单示例 类加载器 类加载器的执行流程 类加载器的种类 AppClassLoader 应用类…

利用maven的dependency插件分析工程的依赖

dependency:analyze https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html 分析项目的依赖&#xff0c;确定哪些&#xff1a;用了并且声明了、用了但没有声明、没有使用但声明了。 dependency:analyze可以单独使用&#xff0c;所以它总是会执行test-…

【算法练习Day38】零钱兑换完全平方数

​&#x1f4dd;个人主页&#xff1a;Sherry的成长之路 &#x1f3e0;学习社区&#xff1a;Sherry的成长之路&#xff08;个人社区&#xff09; &#x1f4d6;专栏链接&#xff1a;练题 &#x1f3af;长路漫漫浩浩&#xff0c;万事皆有期待 文章目录 零钱兑换完全平方数总结&am…

软件测试:单元测试、集成测试、系统测试详解

实际的测试工作当中&#xff0c;我们会从不同的角度对软件测试的活动进行分类&#xff0c;题主说的“单元测试&#xff0c;集成测试&#xff0c;系统测试”&#xff0c;是按照开发阶段进行测试活动的划分。这种划分完整的分类&#xff0c;其实是分为四种“单元测试&#xff0c;…

TablePlus for Mac 数据库管理工具功能介绍

TablePlus是一款功能强大的数据库管理工具&#xff0c;专为Mac操作系统设计。它支持多种主流数据库&#xff0c;包括MySQL&#xff0c;PostgreSQL&#xff0c;SQLite&#xff0c;Microsoft SQL Server&#xff0c;Amazon Redshift等等。无论您是开发人员、数据库管理员还是数据…

【2024最新】Android Debug Bridge【下载安装】零基础到大神【附下载链接】

一、ADB简介 1、什么是adb ADB 全称为 Android Debug Bridge&#xff0c;起到调试桥的作用&#xff0c;是一个客户端-服务器端程序。其中客户端是用来操作的电脑&#xff0c;服务端是 Android 设备。 ADB 也是 Android SDK 中的一个工具&#xff0c;可以直接操作管理 Androi…

多个PDF发票合并实现一张A4纸打印2张电子/数电发票功能

python教程79--A4纸增值税电子发票合并打印_python 打印 发票设置_颐街的博客-CSDN博客文章浏览阅读7.9k次。接上篇https://blog.csdn.net/itmsn/article/details/121902974?spm1001.2014.3001.5501一张A4纸上下2张增值税电子发票实现办法。使用环境&#xff1a;python3.8、ma…

【C++基础知识学习笔记】精华版(复习专用)

常用语法 函数重载(Overload) 规则: 函数名相同 参数个数不同、参数类型不同、参数顺序不同 注意: 返回值类型与函数重载无关 调用函数时,实参的隐式类型转换可能会产生二义性 默认参数 C++ 允许函数设置默认参数,在调用时可以根据情况省略实参。规则如下: 默认参数只能…

chinese_llama_aplaca训练和代码分析

训练细节 ymcui/Chinese-LLaMA-Alpaca Wiki GitHub中文LLaMA&Alpaca大语言模型本地CPU/GPU训练部署 (Chinese LLaMA & Alpaca LLMs) - 训练细节 ymcui/Chinese-LLaMA-Alpaca Wikihttps://github.com/ymcui/Chinese-LLaMA-Alpaca/wiki/%E8%AE%AD%E7%BB%83%E7%BB%86%E…

selenium自动化测试入门 —— 获取元素对象!

一、元素定位简介 八种属性定位页面元素&#xff1a; By.ID By.XPATH By.LINK_TEXT By.PARTIAL_LINK_TEXT By.NAME By.TAG_NAME By.CLASS_NAME By.CSS_SELECTOR webdriver元素定位方法&#xff1a; driver.find_element(By.XXX,元素属性) # 定位单个元素 driver.find_elemen…

按键精灵中常用的命令

1. 声明变量&#xff1a; Dim 2. 注释语句 (1). 单行注释&#xff1a;这是一行注释&#xff0c;使用一个单引号进行注释&#xff1b; (2). 单行注释&#xff1a;// 这是一行注释&#xff0c;使用一对反斜杠进行注释; (3). 多行注释&#xff1a;/*这是多行注释&#xff0c;中…

canal+es+kibana+springboot

1、环境准备 服务器&#xff1a;Centos7 Jdk版本&#xff1a;1.8 Mysql版本&#xff1a;5.7.44 Canal版本&#xff1a;1.17 Es版本&#xff1a;7.12.1 kibana版本&#xff1a;7.12.1 软件包下载地址&#xff1a;链接&#xff1a;https://pan.baidu.com/s/1jRpCJP0-hr9aI…

基于野狗算法的无人机航迹规划-附代码

基于野狗算法的无人机航迹规划 文章目录 基于野狗算法的无人机航迹规划1.野狗搜索算法2.无人机飞行环境建模3.无人机航迹规划建模4.实验结果4.1地图创建4.2 航迹规划 5.参考文献6.Matlab代码 摘要&#xff1a;本文主要介绍利用野狗算法来优化无人机航迹规划。 1.野狗搜索算法 …

前端面试题整理(一)

前言&#xff1a; 这篇博客是记录自己在看面试过程中还未完全掌握的前端知识点&#xff0c;也是一些前端面试需要掌握的知识点&#xff08;总结的并不全面&#xff0c;可以参考&#xff0c;具体情况以自己实际为准&#xff09;,并且这篇博客正在持续更新中… 附言&#xff1a…

2023.11.4-Envoy使用案例-oss

2023.11.4-Envoy使用案例 目录 本节实战 实战名称&#x1f6a9; 实战&#xff1a;前端代理-2023.11.2(测试成功)&#x1f6a9; 实战&#xff1a;流量镜像-2023.11.4(测试成功)&#x1f6a9; 实战&#xff1a;故障注入过滤器-2023.11.4(测试成功)&#x1f6a9; 实战&#xff1a…

【Invea Therapeutics】申请7500万美元纳斯达克IPO上市

来源&#xff1a;猛兽财经 作者&#xff1a;猛兽财经 猛兽财经获悉&#xff0c;美国生物制药公司【Invea Therapeutics】近期已向美国证券交易委员会&#xff08;SEC&#xff09;提交招股书&#xff0c;申请在纳斯达克IPO上市&#xff0c;股票代码为(INAI) &#xff0c;Invea …

基于龙格-库塔算法的无人机航迹规划-附代码

基于龙格-库塔算法的无人机航迹规划 文章目录 基于龙格-库塔算法的无人机航迹规划1.龙格-库塔搜索算法2.无人机飞行环境建模3.无人机航迹规划建模4.实验结果4.1地图创建4.2 航迹规划 5.参考文献6.Matlab代码 摘要&#xff1a;本文主要介绍利用龙格-库塔算法来优化无人机航迹规划…