Flutter中的轮动图以及抖音上下滑页切换视频功能等等,这些都可以通过 PageView 轻松实现

PageView常见属性:

PageView 的使用

class MyPage extends StatefulWidget {
const MyPage({super.key}); @override
State<MyPage> createState() => _MyPageState();
} class _MyPageState extends State<MyPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
scrollDirection: Axis.vertical, //垂直滑动 默认水平滑动
children: const [
Center(
child: Text("1"),
),
Center(
child: Text("2"),
),
Center(
child: Text("3"),
),
Center(
child: Text("4"),
),
],
));
}
}

PageView.builder

class MyPage extends StatefulWidget {
const MyPage({super.key}); @override
State<MyPage> createState() => _MyPageState();
} class _MyPageState extends State<MyPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView.builder(
itemCount: 5,
scrollDirection: Axis.vertical, // 滑动方向为垂直方向
itemBuilder: (BuildContext context, int index) {
return Center(
child: Text("第$index页"),
);
},
));
}
}

PageView上拉无限加载的实现思路

class PageViewPage extends StatefulWidget {
const PageViewPage({super.key});
@override
State<PageViewPage> createState() => _PageViewPageState();
} class _PageViewPageState extends State<PageViewPage> {
final List<Widget> _list = []; // 创建一个名为_list的空Widget列表 @override
void initState() {
// 在初始化阶段运行
super.initState();
for (var i = 0; i < 3; i++) {
_list.add(MyPage(text: "$i")); // 将一个带有相应文本的MyPage小部件添加到_list中
}
} @override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
scrollDirection: Axis.vertical, // 设置PageView的滚动方向为垂直方向
onPageChanged: (index) {
// 每当页面更改时调用的回调函数 print("-----$index"); // 打印当前页面的索引
print(_list.length); // 打印_list中的部件数
if (index + 1 == _list.length) {
// 如果当前页面是_list的倒数第二个
setState(() {
// 在setState中更改_list
for (var i = 0; i < 3; i++) {
// 添加10个新的MyPage小部件到_list中
_list.add(MyPage(text: "$i"));
}
});
}
},
children: _list, // 将_list中的小部件作为PageView的子部件
));
}
} class MyPage extends StatefulWidget {
final String text;
const MyPage({super.key, required this.text});
@override
State<MyPage> createState() => _MyPageState();
} class _MyPageState extends State<MyPage> {
@override
Widget build(BuildContext context) {
return Center(
child: Text(widget.text,
style: Theme.of(context).textTheme.headline1), // 显示小部件的文本
);
}
}

PageView 实现一个无限轮播的轮播图

import 'package:flutter/material.dart';

class PageViewPage extends StatefulWidget {
const PageViewPage({super.key});
// PageViewPage组件的构造函数
@override
State<PageViewPage> createState() => _PageViewPageState();
// 创建并返回PageViewPage组件对应的状态
}
class _PageViewPageState extends State<PageViewPage> {
List<Widget> pageList = [];
// 定义一个Widget类型的列表pageList
@override
void initState() {
super.initState();
// 重写父类的initState()函数
List listData = [
// 创建一个列表listData包含多个图片URL
{
"imageUrl": 'https://www.itying.com/images/flutter/1.png',
},
{
"imageUrl": 'https://www.itying.com/images/flutter/2.png',
},
{
"imageUrl": 'https://www.itying.com/images/flutter/3.png',
},
];
for (int i = 0; i < listData.length; ++i) {
// 遍历listData
pageList.add(PicturePage(
url: listData[i]["imageUrl"],
));
// 根据遍历结果创建PicturePage组件,并将其添加到pageList列表中
}
}
@override
Widget build(BuildContext context) {
// 重写父类的build()函数
return Scaffold(
body: ListView(
// 在Scaffold的主体部分创建一个ListView组件
children: [Swiper(pageList: pageList)],
// 将Swiper组件添加到ListView的子部件列表中
),
);
}
} //Swiper组件
class Swiper extends StatefulWidget {
final double width;
final double height;
final List<Widget> pageList;
const Swiper(
{super.key,
this.width = double.infinity,
this.height = 200,
required this.pageList});
@override
State<Swiper> createState() => _SwiperState();
} class _SwiperState extends State<Swiper> {
int _currentPageIndex = 0;
@override
void initState() {
super.initState();
} @override
Widget build(BuildContext context) {
return Stack(
children: [
SizedBox(
width: double.infinity,
height: 200,
child: PageView.builder(
onPageChanged: (int index) {
// 当页面改变时调用的回调函数
setState(() {
_currentPageIndex = index % (widget.pageList.length);
});
},
itemCount: 10000, // 打算生成的页面数,这里写得非常大,因此可以实现无限循环滚动
itemBuilder: (context, index) {
return widget.pageList[index % (widget.pageList.length)];
}),
),
Positioned(
//圆点
bottom: 10, //位置
left: 0, //位置
right: 0, //位置
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(widget.pageList.length, (i) {
// 利用List.generate方法生成指定数量的圆点
return Container(
margin: const EdgeInsets.fromLTRB(2, 0, 2, 0),
width: 10,
height: 10,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _currentPageIndex == i ? Colors.blue : Colors.grey),
);
}).toList(),
),
),
],
);
}
}
// PicturePage 图片页面
class PicturePage extends StatefulWidget {
final String url;
final double width;
final double height;
const PicturePage(
{super.key,
required this.url,
this.width = double.infinity,
this.height = 200});
@override
State<PicturePage> createState() => _PicturePageState();
} class _PicturePageState extends State<PicturePage> {
@override
Widget build(BuildContext context) {
print(widget.url);
return SizedBox(
width: widget.width,
height: widget.height,
child: Image.network(widget.url, fit: BoxFit.cover),
);
}
}

Flutter PageView(轮动图)的更多相关文章

  1. 7、Flutter banner_view 轮播图的使用

    1.前言 实现轮播图,效果如下: 2.实现 将采用 banner_view 实现:资源库地址 2.1.yaml 引入依赖 在 pubspec.yaml 声明需要引用的库,执行命令 flutter pa ...

  2. Flutter学习五之网络请求和轮播图的实现

    上期讲到了,怎样实现一个下拉刷新和加载更多的列表,数据更新,需要使用到网络请求,在flutter中,怎样实现一个网络请求呢?官方使用的是dart io中的HttpClient发起的请求,但HttpCl ...

  3. JS —— 轮播图中的缓动函数的封装

    轮播图的根本其实就是缓动函数的封装,如果说轮播图是一辆跑动的汽车,那么缓动函数就是它的发动机,今天本文章就带大家由简入繁,封装属于自己的缓动函数~~ 我们从需求的角度开始,首先给出一个简单需求: 1. ...

  4. 28 Flutter 轮播图 flutter_swiper

    中文地址: https://github.com/best-flutter/flutter_swiper/blob/master/README-ZH.md 基本参数 参数 默认值 描述 scrollD ...

  5. 05-06 Flutter JSON和序列化反序列化、创建模型类转换Json数据、轮播图数据渲染:Flutter创建商品数据模型 、请求Api接口渲染热门商品 推荐商品

    Config.dart class Config{ static String domain='http://jd.itying.com/'; } FocusModel.dart class Focu ...

  6. JavaScript--缓动动画+轮播图

    上效果: 实现步骤: 最重要的是运动公式!!! <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  7. Flutter轮播图

    前端开发当中最有意思的就是实现动画特效,Flutter提供的各种动画组件可以方便实现各种动画效果.Flutter中的动画组件主要分为两类: 隐式动画控件:只需设置组件开始值,结束值,执行时间,比如An ...

  8. jquery-抖动图组轮播动画

    JQ匀速抖动图组轮播动画 一.HTML+CSS <!DOCTYPE html> <html lang="en" xmlns="http://www.w3 ...

  9. 实现下来ScrollView放大轮播图

    创建工程,创建一个UIScrollView属性,并遵循其协议: #define kWidth self.view.frame.size.width//屏幕宽 #define kHeight self. ...

  10. js原生代码实现轮播图案例

    一.轮播图是现在网站网页上最常见的效果之一,对于轮播图的功能,要求不同,效果也不同! 我们见过很多通过不同的方式,实现这一效果,但是有很多比较麻烦,而且不容易理解,兼容性也不好. 在这里分享一下,用j ...

随机推荐

  1. PostgreSQL学习笔记-6.基础知识:ALTER、TRUNCATE 、View(视图)、TRANSACTION 事务、LOCK 锁

    ALTER TABLE 命令 在 PostgreSQL 中,ALTER TABLE 命令用于添加,修改,删除一张已经存在表的列. 另外你也可以用 ALTER TABLE 命令添加和删除约束. 语法 用 ...

  2. 通过资源名称得到资源id

    demo地址 主要应用类 package com.example.activitylibrary; import android.app.Activity; import android.os.Bun ...

  3. 俄罗斯版IDM安装与破解以及解决B站视频网站不弹出下载浮窗

    IDM 全称 Internet Download Manager,是一款非常优秀的多线程下载和视频嗅探工具,不仅可以显著提高文件下载速度,配合IDM浏览器扩展插件,还可以嗅探并下载YouTube.知乎 ...

  4. WEBGpu最佳实践之BindGroup

    介绍 在WebGPU中,资源通过GPUBindGroup结构传递给着色器,与支持它的布局定义(GPUBindGroupLayout和GPUPipelineLayout)以及着色器中绑定组的声明一起.这 ...

  5. Android下音视频对讲演示程序(声学回音消除、噪音抑制、语音活动检测、自动增益控制、自适应抖动缓冲)(2023年07月13日更新)

    Android下音视频对讲演示程序 必读说明 简介   本软件根据<道德经>为核心思想而设计,实现了两个设备之间进行音视频对讲,一般可用于楼宇对讲.智能门铃对讲.企业员工对讲.智能对讲机. ...

  6. docker容器管理脚本

    #!/bin/bash #auto install docker and Create VM #by jfedu.net 2017 #Define PATH Varablies IPADDR=`ifc ...

  7. 全局关闭Unity编译的CS警告

    实现方式 Editor和Game的全局CSharp编译配置文件名: Assets/mcs.rsp 添加如下内容可屏蔽对应的警告信息 -nowarn:1234 常用内容 CS0219 未使用的publi ...

  8. 文心一言 VS 讯飞星火 VS chatgpt (132)-- 算法导论11.2 4题

    四.用go语言,说明在散列表内部,如何通过将所有未占用的槽位链接成一个自由链表,来分配和释放元素所占的存储空间.假定一个槽位可以存储一个标志.一个元素加上一个或两个指针.所有的字典和自由链表操作均应具 ...

  9. 让 keil MDK 支持C99

    打开options fot target-> C/C++ 在 Misc Controls 中添加 --c99.

  10. MySQL函数解读

    一.字符串函数 1.1.instr() INSTR(str,substr)一共有两个参数str被查询字符,substr查询字符,查询时下标从1开始记,只查询第一次出现的地方,为查询到显示为0 一般用法 ...