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(轮动图)的更多相关文章
- 7、Flutter banner_view 轮播图的使用
1.前言 实现轮播图,效果如下: 2.实现 将采用 banner_view 实现:资源库地址 2.1.yaml 引入依赖 在 pubspec.yaml 声明需要引用的库,执行命令 flutter pa ...
- Flutter学习五之网络请求和轮播图的实现
上期讲到了,怎样实现一个下拉刷新和加载更多的列表,数据更新,需要使用到网络请求,在flutter中,怎样实现一个网络请求呢?官方使用的是dart io中的HttpClient发起的请求,但HttpCl ...
- JS —— 轮播图中的缓动函数的封装
轮播图的根本其实就是缓动函数的封装,如果说轮播图是一辆跑动的汽车,那么缓动函数就是它的发动机,今天本文章就带大家由简入繁,封装属于自己的缓动函数~~ 我们从需求的角度开始,首先给出一个简单需求: 1. ...
- 28 Flutter 轮播图 flutter_swiper
中文地址: https://github.com/best-flutter/flutter_swiper/blob/master/README-ZH.md 基本参数 参数 默认值 描述 scrollD ...
- 05-06 Flutter JSON和序列化反序列化、创建模型类转换Json数据、轮播图数据渲染:Flutter创建商品数据模型 、请求Api接口渲染热门商品 推荐商品
Config.dart class Config{ static String domain='http://jd.itying.com/'; } FocusModel.dart class Focu ...
- JavaScript--缓动动画+轮播图
上效果: 实现步骤: 最重要的是运动公式!!! <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- Flutter轮播图
前端开发当中最有意思的就是实现动画特效,Flutter提供的各种动画组件可以方便实现各种动画效果.Flutter中的动画组件主要分为两类: 隐式动画控件:只需设置组件开始值,结束值,执行时间,比如An ...
- jquery-抖动图组轮播动画
JQ匀速抖动图组轮播动画 一.HTML+CSS <!DOCTYPE html> <html lang="en" xmlns="http://www.w3 ...
- 实现下来ScrollView放大轮播图
创建工程,创建一个UIScrollView属性,并遵循其协议: #define kWidth self.view.frame.size.width//屏幕宽 #define kHeight self. ...
- js原生代码实现轮播图案例
一.轮播图是现在网站网页上最常见的效果之一,对于轮播图的功能,要求不同,效果也不同! 我们见过很多通过不同的方式,实现这一效果,但是有很多比较麻烦,而且不容易理解,兼容性也不好. 在这里分享一下,用j ...
随机推荐
- Oracle:字符串的拼接、截取、查找、替换
一.拼接:1.使用"||"来拼接字符串: select '拼接'||'字符串' as Str from dual; 2.使用concat(param1,param2)函数实现: s ...
- CCF PTA编程培训师资认证
考试费用: 双会员500元,任意一方单会员750元,报名考试同时成为CCF专业会员850元,非会员1000元. P/T2补考费用:双会员200元,任意一方单会员300元,非会员400元. T1补考费用 ...
- nacos-mysql.sql
https://github.com/alibaba/nacos/blob/master/distribution/conf/nacos-mysql.sql # Nacos安装 /home/nacos ...
- C语言存储类别
对于C语言中的变量,存储类别可分为4种:auto(自动存储).static(静态存储).register(寄存器存储).extern(外部存储). auto自动存储 函数中的局部变量,如果不专门声明为 ...
- 入门篇-其之六-Java运算符(中)
祝所有程序员,1024节日快乐!!! 一.自增/自减运算符 假设有一个变量intValue的值为10,如果想让这个值加1,有哪些方式? 首先,我们可以使用最原始的方式: int intValue = ...
- SpringBoot如何缓存方法返回值?
目录 Why? HowDo annotation MethodCache MethodCacheAspect controller SpringCache EnableCaching Cacheabl ...
- C# 在流行度指数上将超过Java
2023年10月最新的TIOBE编程语言流行指数表明:C#和Java之间的差距从未如此之小,目前,差异仅为1.2%,如果趋势保持这种状态,C#将在大约2个月内超过Java,TIOBE Software ...
- Nginx-多功能脚本
#!/bin/bash #2020年2月16日 #auto_install_nginx_web.v3 #by fly ################################ #NGX_VER ...
- np.array和np.ndarry 的区别
np.array和np.ndarray都是NumPy中用于创建多维数组的函数. np.ndarray是NumPy中的多维数组类,它是一种可变的数组,可以通过修改数组中的元素来改变其内容.使用np.nd ...
- 工控机中部署Ubuntu 22.04 系统
1.下载Ubuntu系统服务器版本 获取Ubuntu服务器版 | Ubuntu 2.下载启动盘制作工具 UltralSO(试用就可以) 文件 > 打开(Ubuntu.ISO) > 启动 & ...