当前位置: 首页 > news >正文

Flutter开发 SingleChildScrollView、ScrollController

SingleChildScrollView

滑动组件

有多张图片,左右滑动
请添加图片描述

class MyState extends State {bool flag = false;Widget build(BuildContext context) {double width = MediaQuery.of(context).size.width;double height = MediaQuery.of(context).size.height / 3;SingleChildScrollView singleChildScrollView = SingleChildScrollView(scrollDirection: Axis.horizontal,child: Row(children: [Image.network("https://pic.rmb.bdstatic.com/bjh/other/d1913ba5338a6ae4571a3d8e4ce469bf.png?for=bg",width: width,height: height,fit: BoxFit.cover,),Image.network("https://pic.rmb.bdstatic.com/bjh/3eacb728ac/240814/0c5c4e7a79d45ef72c1f934e9ed7edc6.jpeg",width: width,height: height,fit: BoxFit.cover,),Image.network("https://pic.rmb.bdstatic.com/bjh/other/d1913ba5338a6ae4571a3d8e4ce469bf.png?for=bg",width: width,height: height,fit: BoxFit.cover,),Image.network("https://pic.rmb.bdstatic.com/bjh/other/d1913ba5338a6ae4571a3d8e4ce469bf.png?for=bg",width: width,height: height,fit: BoxFit.cover,),],),);return Scaffold(appBar: AppBar(title: Text("SwitchListTile"), centerTitle: true),body: singleChildScrollView,);}
}

ScrollController

控制滑动

属性说明
initialScrollOffset设置滚动视图的初始位置
keepScrollOffset设置是否保存滚动位置
offset返回当前滚动位置的偏移量
jumpTo设置滚动到指定位置
animateTo设置带动画滚动到指定位置

在上面的例子中,添加按钮,用来滑动图片,只设置一个按钮。

请添加图片描述

class MyState extends State {bool flag = false;Widget build(BuildContext context) {double width = MediaQuery.of(context).size.width;double height = MediaQuery.of(context).size.height / 3;ScrollController scrollController = ScrollController();SingleChildScrollView singleChildScrollView = SingleChildScrollView(controller: scrollController,scrollDirection: Axis.horizontal,child: Row(children: [Image.network("https://pic.rmb.bdstatic.com/bjh/other/d1913ba5338a6ae4571a3d8e4ce469bf.png?for=bg",width: width,height: height,fit: BoxFit.cover,),Image.network("https://pic.rmb.bdstatic.com/bjh/3eacb728ac/240814/0c5c4e7a79d45ef72c1f934e9ed7edc6.jpeg",width: width,height: height,fit: BoxFit.cover,),Image.network("https://pic.rmb.bdstatic.com/bjh/other/d1913ba5338a6ae4571a3d8e4ce469bf.png?for=bg",width: width,height: height,fit: BoxFit.cover,),Image.network("https://pic.rmb.bdstatic.com/bjh/other/d1913ba5338a6ae4571a3d8e4ce469bf.png?for=bg",width: width,height: height,fit: BoxFit.cover,),],),);Stack stack = Stack(alignment: Alignment.centerRight,children: [singleChildScrollView,IconButton(onPressed: () {var old = scrollController.offset;scrollController.jumpTo(old +width);}, icon: Icon(Icons.navigate_next))],);return Scaffold(appBar: AppBar(title: Text(""), centerTitle: true),body: stack,);}
}
http://www.lryc.cn/news/614384.html

相关文章:

  • 大语言模型提示工程与应用:提示词基础使用方式
  • 【重学MySQL】事务隔离
  • 单层 PDF 与双层 PDF:一字之差,功能大不同
  • 学生如何使用 DeepSeek 帮助自己的学习?
  • 自有域名功能详解——安全可控的企业级访问方案
  • select ... for update阻塞
  • 智慧社区(九)——事务加持下的小区删除操作
  • 前端老项目依赖安全漏洞解决
  • 如何使用 pg_rman 进行 PostgreSQL 的备份与恢复
  • 超算中心的基本组成,国内有哪些比较有名的超算中心?
  • 【网络】TCP/UDP协议
  • Word中怎样插入特殊符号
  • Spring Boot + ECharts 极简整合指南:从零实现动态数据可视化大屏
  • Linux常见服务器配置(三):MariaDB数据库管理和WEB服务器
  • 京东一面:MySQL 主备延迟有哪些坑?主备切换策略
  • Linux 学习 ------Linux 入门(上)
  • LINUX88 变量:命令定义;普通数组定义(复);declare -i /-x
  • 医防融合中心-智慧化慢病全程管理医疗AI系统开发(中)
  • (数据结构)链表
  • 从零开始构建【顺序表】:C语言实现与项目实战准备
  • Autosar AP中Promise和Future的异步消息通信的详细解析
  • 深入理解VideoToolbox:iOS/macOS视频硬编解码实战指南
  • FreeRTOS入门知识(初识RTOS)(二)
  • 2025-08-08 李沐深度学习11——深度学习计算
  • 【网络运维】Linux:MariaDB 数据库介绍及管理
  • duxapp 2025-06-04 更新 UI库导出方式更新
  • Java学习Collection单列集合中的三种通用遍历方法
  • 【洛谷题单】--分支结构(二)
  • [GESP202506 五级] 最大公因数
  • 豆包新模型矩阵+PromptPilot:AI开发效率革命的终极方案