Flutter - PageView(1) 基本用法

2024-01-08 00:50
文章标签 用法 基本 flutter pageview

本文主要是介绍Flutter - PageView(1) 基本用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

如果要实现页面切换和 Tab 布局,我们可以使用 PageView 组件。需要注意,PageView 是一个非常重要的组件,因为在移动端开发中很常用,比如大多数 App 都包含 Tab 换页效果、图片轮动以及抖音上下滑页切换视频功能等等,这些都可以通过 PageView 轻松实现。
在这里插入图片描述
1 PageView 简单使用

PageView(scrollDirection:Axis.horizontal,allowImplicitScrolling: true,children: <Widget>[MyItemContainerState(colors: Colors.yellow,pageName: "yellow",),MyItemContainerState(colors: Colors.red,pageName: "red",),MyItemContainerState(colors: Colors.green,pageName: "green",),MyItemContainerState(colors: Colors.blue,pageName: "blue",),MyItemContainerState(colors: Colors.deepPurple,pageName: "deepPurple",)],)
class MyItemContainerState extends StatefulWidget {const MyItemContainerState({Key? key,required this.colors,required this.pageName}) : super(key: key);final MaterialColor colors ;final String pageName ;State<MyItemContainerState> createState() {// TODO: implement createStatereturn ItemContainerState(colors,pageName);}
}

class MyItemContainerState extends StatefulWidget {const MyItemContainerState({Key? key,required this.colors,required this.pageName}) : super(key: key);final MaterialColor colors ;final String pageName ;State<MyItemContainerState> createState() {// TODO: implement createStatereturn ItemContainerState(colors,pageName);}
}

class ItemContainerState extends State<MyItemContainerState> with AutomaticKeepAliveClientMixin{MaterialColor colors ;String pageName ;ItemContainerState(this.colors,this.pageName);// TODO: implement wantKeepAlivebool get wantKeepAlive => true;Widget build(BuildContext context) {super.build(context);print("创建$pageName");// TODO: implement buildreturn  Container(width: double.infinity,height: double.infinity,color: colors,alignment: Alignment.center,child: Text(pageName,style: TextStyle(fontSize: 50,color: Colors.white),),);}void dispose() {// TODO: implement disposeprint("销毁$pageName");super.dispose();}}

注1 scrollDirection:Axis.horizontal, 控制PageView的滑动方向
Axis.horizontal 是横着滑
Axis.vertical 竖着滑
在这里插入图片描述
注 2 allowImplicitScrolling: true,
为true 时 ,缓存当前页的前一页和后一页
为false 时 ,不缓存,滑出当前页及时销毁当前页

1 下面日志打印的是 allowImplicitScrolling = false 的情况
在这里插入图片描述
2 下面是 allowImplicitScrolling = true 的情况
2.1第一次运行程序,创建了 yellow 和 red 两个界面
在这里插入图片描述
2.2 滑动到第二个界面是 创建了 green 界面 ,并且没有销毁 yellow 界面
在这里插入图片描述
2.3 滑动到第三个green界面时 创建了 blue 界面 且销毁了yellow 界面
在这里插入图片描述

这篇关于Flutter - PageView(1) 基本用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/581798

相关文章

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb

使用Python进行文件读写操作的基本方法

《使用Python进行文件读写操作的基本方法》今天的内容来介绍Python中进行文件读写操作的方法,这在学习Python时是必不可少的技术点,希望可以帮助到正在学习python的小伙伴,以下是Pyth... 目录一、文件读取:二、文件写入:三、文件追加:四、文件读写的二进制模式:五、使用 json 模块读写

oracle中exists和not exists用法举例详解

《oracle中exists和notexists用法举例详解》:本文主要介绍oracle中exists和notexists用法的相关资料,EXISTS用于检测子查询是否返回任何行,而NOTE... 目录基本概念:举例语法pub_name总结 exists (sql 返回结果集为真)not exists (s

Springboot中Jackson用法详解

《Springboot中Jackson用法详解》Springboot自带默认json解析Jackson,可以在不引入其他json解析包情况下,解析json字段,下面我们就来聊聊Springboot中J... 目录前言Jackson用法将对象解析为json字符串将json解析为对象将json文件转换为json

鸿蒙开发搭建flutter适配的开发环境

《鸿蒙开发搭建flutter适配的开发环境》文章详细介绍了在Windows系统上如何创建和运行鸿蒙Flutter项目,包括使用flutterdoctor检测环境、创建项目、编译HAP包以及在真机上运... 目录环境搭建创建运行项目打包项目总结环境搭建1.安装 DevEco Studio NEXT IDE

基本知识点

1、c++的输入加上ios::sync_with_stdio(false);  等价于 c的输入,读取速度会加快(但是在字符串的题里面和容易出现问题) 2、lower_bound()和upper_bound() iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。 iterator upper_bou

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

Flutter 进阶:绘制加载动画

绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。

bytes.split的用法和注意事项

当然,我很乐意详细介绍 bytes.Split 的用法和注意事项。这个函数是 Go 标准库中 bytes 包的一个重要组成部分,用于分割字节切片。 基本用法 bytes.Split 的函数签名如下: func Split(s, sep []byte) [][]byte s 是要分割的字节切片sep 是用作分隔符的字节切片返回值是一个二维字节切片,包含分割后的结果 基本使用示例: pa