flutter开发实战-ListWheelScrollView与自定义TimePicker时间选择器

flutter开发实战-ListWheelScrollView与自定义TimePicker

最近在使用时间选择器的时候,需要自定义一个TimePicker效果,当然这里就使用了ListWheelScrollView。ListWheelScrollView与ListView类似,但ListWheelScrollView渲染效果类似滚筒效果。
在这里插入图片描述

一、ListWheelScrollView

基本用法

  ListWheelScrollView({super.key,this.controller,this.physics,this.diameterRatio = RenderListWheelViewport.defaultDiameterRatio,this.perspective = RenderListWheelViewport.defaultPerspective,this.offAxisFraction = 0.0,this.useMagnifier = false,this.magnification = 1.0,this.overAndUnderCenterOpacity = 1.0,required this.itemExtent,this.squeeze = 1.0,this.onSelectedItemChanged,this.renderChildrenOutsideViewport = false,this.clipBehavior = Clip.hardEdge,this.restorationId,this.scrollBehavior,required List<Widget> children,})

ListWheelScrollView的一些属性,如children是子控件

  • children是子控件,
  • itemExtent是每个item的高度
  • magnification是圆筒直径和主轴渲染窗口的尺寸比,默认值是2
  • perspective是圆柱投影试图,为0表示从无限远处看,1表示从无限近处看。默认值0.003
  • offAxisFraction表示圆筒水平偏移中心的程度
  • magnification与useMagnifier放大镜,分辨设置放大镜与放大倍率。
  • squeeze表示圆筒上子控件数量与在同等大小的平面上的子控件的数量之比。

看下ListWheelScrollView基本用法

Container(height: 250,child: ListWheelScrollView(itemExtent: 50,children: [Container(color: Colors.red,),Container(color: Colors.orangeAccent,),Container(color: Colors.yellow,),Container(color: Colors.green,),Container(color: Colors.teal,),Container(color: Colors.blue,),Container(color: Colors.purple,),],),)

效果图如下
在这里插入图片描述
如果将diameterRatio调整为1的

效果图如下

在这里插入图片描述

其他属性的效果可以逐个尝试一下。

如果使用的数据比较多时候,可以使用userDelegate方式, 使用ListWheelChildBuilderDelegate来指定builder与childCount.

 Container(height: 350,child: ListWheelScrollView.useDelegate(itemExtent: 50,diameterRatio: 2,childDelegate:ListWheelChildBuilderDelegate(builder: (context, index) {return Container(color: Colors.lightBlue,alignment: Alignment.center,child: Text("$index",style: TextStyle(color: Colors.white, shadows: [Shadow(color: Colors.black,offset: Offset(.5, .5),blurRadius: 2)])),);}, childCount: 100),),);

效果图如下

在这里插入图片描述
当然还有一个ListWheelChildLoopingListDelegate可以表现出来循环滚动的效果

final List dataList = ["第1行","第2行","第3行","第4行","第5行","第6行","第7行","第8行","第9行","第10行",];Widget buildChildItem(String text) {return Container(color: Colors.lightBlue,alignment: Alignment.center,child: Text("$text",style: TextStyle(color: Colors.white, shadows: [Shadow(color: Colors.black,offset: Offset(.5, .5),blurRadius: 2)])),);}void testListWheelScrollViewDelegate(BuildContext context) {showModalBottomSheet(context: context,isScrollControlled: true,builder: (ctx) {return Container(height: 350,child: ListWheelScrollView.useDelegate(itemExtent: 50,diameterRatio: 2,childDelegate: ListWheelChildLoopingListDelegate(children: dataList.map((e) => buildChildItem(e)).toList())),);},);}

效果图如下

在这里插入图片描述

二、自定义TimePicker

自定义TimePicker使用ListWheelScrollView
自定义TimePicker有小时和分钟,左边显示小时,右边显示分钟。点击确定确认选择的时间,时间格式为10:20
onSelectedItemChanged来确认选择的item
在这里插入图片描述
完整代码如下


class CustomTimePicker extends StatefulWidget {const CustomTimePicker({super.key,this.width,this.height,});final double? width;final double? height;@overrideState<CustomTimePicker> createState() => _CustomTimePickerState();
}class _CustomTimePickerState extends State<CustomTimePicker> {List<String> hourData = [];List<String> minuteData = [];String selectedHour = "";String selectedminute = "";@overridevoid initState() {// TODO: implement initStatesuper.initState();for (int i = 0; i < 24; i++) {String hour = i.toString();if (i < 10) {hour = "0" + i.toString();}hourData.add(hour);}for (int i = 0; i < 60; i++) {String minute = i.toString();if (i < 10) {minute = "0" + i.toString();}minuteData.add(minute);}}@overridevoid dispose() {// TODO: implement disposesuper.dispose();}Widget buildItem(String text) {return Text(text,textAlign: TextAlign.center,softWrap: true,style: TextStyle(fontSize: 20,fontWeight: FontWeight.w500,fontStyle: FontStyle.normal,color: Color(0xFF333333),decoration: TextDecoration.none,),);}@overrideWidget build(BuildContext context) {return Container(width: widget.width,height: widget.height,color: Colors.white,child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [Container(width: widget.width,height: 50,child: Row(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [TextButton(onPressed: () {Navigator.of(context).pop();},child: Container(height: 36,width: 100,color: Colors.transparent,alignment: Alignment.center,child: Text('取消',style: TextStyle(fontSize: 15, color: Colors.black87),),),),Expanded(child: Text('${selectedHour}:${selectedminute}',textAlign: TextAlign.center,style: TextStyle(fontSize: 16, color: Colors.black87),),),TextButton(onPressed: () {Navigator.of(context).pop();},child: Container(height: 36,width: 100,alignment: Alignment.center,child: Text('确定',style: TextStyle(fontSize: 15, color: Colors.blue),),),),],),),Expanded(child: Row(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [Container(width: 150,height: widget.height,child: Scrollable(axisDirection: AxisDirection.down,physics: BouncingScrollPhysics(),dragStartBehavior: DragStartBehavior.start,viewportBuilder: (ctx, position) =>ListWheelScrollView.useDelegate(itemExtent: 44,squeeze: 1,diameterRatio: 3,useMagnifier: true,overAndUnderCenterOpacity: 0.8,magnification: 1.1,onSelectedItemChanged: (index) {String hour = hourData[index];print("hour:${hour}");setState(() {selectedHour = hour;});},childDelegate: ListWheelChildLoopingListDelegate(children:hourData.map((e) => buildItem(e)).toList()),),),),Container(width: 150,height: widget.height,child: Scrollable(axisDirection: AxisDirection.down,physics: BouncingScrollPhysics(),dragStartBehavior: DragStartBehavior.start,viewportBuilder: (ctx, position) =>ListWheelScrollView.useDelegate(itemExtent: 44,squeeze: 1,diameterRatio: 3,useMagnifier: true,overAndUnderCenterOpacity: 0.8,magnification: 1.1,onSelectedItemChanged: (index) {String minute = minuteData[index];print("minute:${minute}");setState(() {selectedminute = minute;});},childDelegate: ListWheelChildLoopingListDelegate(children:minuteData.map((e) => buildItem(e)).toList()),),),),],),),],));}
}

效果图如下

在这里插入图片描述

三、小结

flutter开发实战-ListWheelScrollView与自定义TimePicker

学习记录,每天不停进步。

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

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

相关文章

【vue3】【vant】 移动端中国传统文化和民间传说案例

更多项目点击&#x1f446;&#x1f446;&#x1f446;完整项目成品专栏 【vue3】【vant】 移动端中国传统文化和民间传说案例 获取源码方式项目说明&#xff1a;其中功能包括项目包含&#xff1a;项目运行环境运行截图和视频 获取源码方式 加Q群&#xff1a;632562109项目说…

Vip-智能预估+大数据标签+人群全选=用户分群!

Mobpush用户分群功能升级&#xff0c;创建推送入口vip用户可进入自有选择标签创建“用户分群”&#xff0c;相比于免费标签&#xff0c;“用户标签”维度更丰富。在应用基础属性上&#xff0c;增加“品牌”、“网络状态”、“运营商”&#xff0c;众所周知&#xff0c;不同厂商…

MJ可以SD就不行么?Stable Diffusion 轻松复刻12生肖水果拼盘,我被AI水果拼盘惊艳到了!

有人用MJ可以轻松生成AI水果拼盘&#xff0c;今天老徐就带大家试试用Stable Diffusion**如何轻松复刻实现。 提示词模版&#xff1a; fruit shapes for chinese new year a wonderful example of edible art, in the style of hyperrealistic wildlife portraits, 1 piece ma…

为什么ISO 45001职业健康安全管理体系是企业发展的基石

ISO 45001源自OHSAS 18001职业健康和安全管理体系&#xff0c;是全球第一个国际职业健康和安全管理标准。ISO&#xff08;国际标准化组织&#xff09;于2018年发布了这一标准&#xff0c;旨在帮助各类组织为员工提供一个更安全、更健康的工作环境。与OHSAS 18001相比&#xff0…

2024年跨境电商关键数据统计:市场规模将达到1.976万亿美元

预计2024年跨境电商消费市场规模将达到1.976万亿美元&#xff0c;占全球网上销售总额的31.2%。这一数据无疑展示了跨境电商市场的巨大潜力和迅猛增长趋势。 全球跨境电商的现状与未来 现状 2023年&#xff0c;全球跨境电商市场规模预计达到1.56万亿美元&#xff0c;占全球电子…

【网络安全学习】漏洞利用:BurpSuite的使用-02-常用的标签

下面介绍一下BurpSuite各个标签的用法&#x1f447; 1️⃣ Dashboard标签 Dashboard&#xff0c;顾名思义就是BurpSuite的仪表盘&#xff0c;可以通过Dashboard进行漏洞扫描&#xff0c;不过该功能需要升级到pro版本&#xff0c;也就是得交钱&#x1f62d;。 2️⃣ Target标签…

借助AI写代码,使用通义灵码智能编写Java和Vue3项目,在Idea和vscode里用AI写代码

在人工智能技术越来越成熟的当下&#xff0c;好多人说AI会取代程序员&#xff0c;这句话石头哥不知可否。但是有一点可以肯定&#xff0c;会熟练使用Ai&#xff0c;驾驭Ai的程序员肯定不会被时代所淘汰。所以今天石头哥就来教大家如何借助Ai来提升自己的代码编写效率。 一&…

用ChatMoney写歌,一分钟一首,音乐人将被AI取代?

本文由 ChatMoney团队出品 随着科技的不断进步&#xff0c;音乐是人类文明的一部分&#xff0c;它在社会、文化、艺术和娱乐领域发挥着重要作用。随着AI技术的发展&#xff0c;AI技术的应用正在以惊人的速度改变音乐创作、演奏、传播和消费的方式&#xff0c;有人欢呼&#xff…

根文件系统

根文件系统 1 介绍1.1 根文件系统介绍1.2 根文件系统目录1.3 常见的根文件系统 2 Buildroot 根文件系统的构建2.1 介绍2.2 依赖文件2.3 交叉编译工具2.4 构建2.4.1 配置 Target options2.4.2 配置 Toolchain2.4.3 配置 System configuration2.4.4 配置 Filesystem images2.4.5 …

超详细的Stable Diffusion WebUI 安装!

前言 安装方式&#xff1a; 使用发行包在带有 NVidia-GPU 的 Windows 10/11 上安装 sd.webui.zip从v1.0.0-pre下载并解压其内容。 跑步update.bat。 跑步run.bat。 Windows 上自动安装 安装Python 3.10.6&#xff08;较新版本的Python不支持torch&#xff09;&#xff0…

go 学习 之 HTTP微服务示例

1. 背景 学习ing 2. 创建文件&#xff1a;server.go go package mainimport ("github.com/gogf/gf/contrib/registry/file/v2""github.com/gogf/gf/v2/frame/g""github.com/gogf/gf/v2/net/ghttp""github.com/gogf/gf/v2/net/gsvc"&…

2.4G无线通信芯片数据手册解读:Ci24R1南京中科微

今天&#xff0c;我非常荣幸地向您介绍这款引领行业潮流的2.4G射频芯片&#xff1a;Ci24R1。这款芯片&#xff0c;不仅是我们技术的结晶&#xff0c;更是未来无线通信的璀璨明星。 首先&#xff0c;让我们来谈谈Ci24R1的“速度”。2.4G射频芯片&#xff0c;凭借其卓越的数据传输…

“论云上自动化运维及其应用”写作框架,软考高级,系统架构设计师

论文真题 云上自动化运维是传统IT运维和DevOps的延伸&#xff0c;通过云原生架构实现运维的再进化。云上自动化运维可以有效帮助企业降低IT运维成本&#xff0c;提升系统的灵活度&#xff0c;以及系统的交付速度&#xff0c;增强系统的可靠性&#xff0c;构建更加安全、可信、…

虹科技术丨Linux环境再升级:PLIN驱动程序正式发布

来源&#xff1a;虹科技术丨Linux环境再升级&#xff1a;PLIN驱动程序正式发布 原文链接&#xff1a;https://mp.weixin.qq.com/s/N4zmkYXTPr7xm-h2s7QiLw 欢迎关注虹科&#xff0c;为您提供最新资讯&#xff01; #PLIN #LIN #LIN接口 导读 Linux驱动程序领域再添新成员&am…

湖北大学2024年成人高考函授报名专升本汉语言文学专业介绍

湖北大学&#xff0c;这所历史底蕴深厚的学府&#xff0c;自创办以来&#xff0c;始终致力于为社会各界人士提供高质量的成人高等继续教育。而今&#xff0c;为了满足广大成年人对于知识更新的渴求&#xff0c;学校特别开放了专升本汉语言文学专业的报名通道&#xff0c;为那些…

SCI二区复现|体育场观众优化算法(SSO)原理及实现

目录 1.背景2.算法原理2.1算法思想2.2算法过程 3.结果展示4.参考文献5.代码获取 1.背景 2024年&#xff0c;M Nemati受到体育场观众的行为对比赛中球员行为的影响启发&#xff0c;提出了体育场观众优化算法&#xff08;Stadium Spectators Optimizer, SSO&#xff09;。 2.算法…

unity使用XR插件开发SteamVR项目,异常问题解决方法

一、unity使用XR插件开发SteamVR项目&#xff0c;运行后相机高度异常问题解决方法如下操作 &#xff08;一&#xff09;、开发环境 1、Unity 2021.3.15f 2、XR Interaction Toolkit Version 2.5.2 &#xff08;com.unity.xr.interaction.toolkit&#xff09; 3、OpenXR Pl…

Nature子刊 | 基于遥感和U-Net绘制6亿棵树木,并发现过去十年印度农田树木严重减少

题目:Severe decline in large farmland trees in India over the past decade 期刊:Nature Sustainability 论文:https://www.nature.com/articles/s41893-024-01356-0 结果数据: https://rs-cph.projects.earthengine.app/view/tree https://zenodo.org/records/10978…

Arduino - Keypad 键盘

Arduino - Keypad Arduino - Keypad The keypad is widely used in many devices such as door lock, ATM, calculator… 键盘广泛应用于门锁、ATM、计算器等多种设备中。 In this tutorial, we will learn: 在本教程中&#xff0c;我们将学习&#xff1a; How to use key…

VUE大屏的开发过程(纯前端)

写在前面&#xff0c;博主是个在北京打拼的码农&#xff0c;工作多年做过各类项目&#xff0c;最近心血来潮在这儿写点东西&#xff0c;欢迎大家多多指教。 对于文章中出现的任何错误请大家批评指出&#xff0c;一定及时修改。有任何想要讨论和学习的问题可联系我&#xff1a;1…