Flutter第六弹 基础列表ListView

2024-04-08 07:28

本文主要是介绍Flutter第六弹 基础列表ListView,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目标:

1)Flutter有哪些常用的列表组建

2)怎么定制列表项Item?

一、ListView简介

使用标准的 ListView 构造方法非常适合只有少量数据的列表。我们还将使用内置的 ListTile widget 来给我们的条目提供可视化结构。ListView支持横向列表和纵向列表。

ListTile相当于列表项 Item,可以定制列表项内容。

例如:可以在ListView的属性children中定义 ListTile组件列表。

ListView(children: const <Widget>[ListTile(leading: Icon(Icons.map),title: Text('Map'),),ListTile(leading: Icon(Icons.photo_album),title: Text('Album'),),ListTile(leading: Icon(Icons.phone),title: Text('Phone'),),],
),

1.1 ListView属性

padding属性

padding定义控件内边距

padding: EdgeInsets.all(8.0),

children属性

children属性是定义列表项Item,是一个ListTile列表。

scrollDirection属性

ListView列表滚动方向。包括:

Axis.vertical: 竖直方向滚动,对应的是纵向列表。

  Axis.horizontal: 水平方向滚动,对应的是横向列表。

1.2 ListTile组件

常用的ListView项,包含图标,Title,文本,按钮等。

class ListTile extends StatelessWidget {

ListTile是StatelessWidget,常用的一些属性:

leading属性

标题Title之前Widget,通常是 Icon或者圆形图像CircleAvatar 组件。

ListTile(leading: Icon(Icons.photo_album), // 标题图片title: Text('Album'),),

title属性

列表Item标题。

titleTextStyle属性

标题文本样式

titleAlignment属性

标题文本的对齐属性

subtitle属性

subtitle 副标题,显示位置位于标题之下。

ListTile(leading: Icon(Icons.photo_album), // 标题图片title: Text('Album'),subtitle: Text('手机历史相册'),),

subtitleTextStyle属性

副标题文本样式

isThreeLine属性

布尔值,指示是否为三行列表项。如果为 true,则可以显示额外的文本行。否则,只有一行文本。

dense属性

是否是紧凑布局。布尔型,紧凑模式下,文本和图标的大小将减小。

textColor属性

文本颜色

contentPadding属性

内容区的内边距

enabled属性

布尔值,指示列表项是否可用。如果为 false,则列表项将不可点击

selected属性

布尔值,指示列表项是否已选择。列表选择时有效

focusColor属性

获取焦点时的背景颜色。

hoverColor属性

鼠标悬停时的背景颜色。

splashColor属性

点击列表项时的水波纹颜色。

tileColor属性

列表项的背景颜色

selectedTileColor属性

选中列表项时的背景颜色。

leadingAndTrailingTextStyle属性

前导和尾部部分文本的样式。

enableFeedback属性

是否启用触觉反馈。

horizontalTitleGap属性

标题与前导/尾部之间的水平间距。

minVerticalPadding属性

最小垂直内边距

minLeadingWidth属性

最小前导宽度

二、定制ListView的子项Item

ListView可以展现简单的列表。如果子项包括有状态更新,和原来一样,可以在State中进行状态更新。

2.1 竖直列表

对应的代码

import 'package:flutter/material.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});// This widget is the root of your application.@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(// This is the theme of your application.//// TRY THIS: Try running your application with "flutter run". You'll see// the application has a purple toolbar. Then, without quitting the app,// try changing the seedColor in the colorScheme below to Colors.green// and then invoke "hot reload" (save your changes or press the "hot// reload" button in a Flutter-supported IDE, or press "r" if you used// the command line to start the app).//// Notice that the counter didn't reset back to zero; the application// state is not lost during the reload. To reset the state, use hot// restart instead.//// This works for code too, not just values: Most code changes can be// tested with just a hot reload.colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),useMaterial3: true,),home: const MyHomePage(title: 'Flutter Demo Home Page'),);}
}class MyHomePage extends StatefulWidget {const MyHomePage({super.key, required this.title});// This widget is the home page of your application. It is stateful, meaning// that it has a State object (defined below) that contains fields that affect// how it looks.// This class is the configuration for the state. It holds the values (in this// case the title) provided by the parent (in this case the App widget) and// used by the build method of the State. Fields in a Widget subclass are// always marked "final".final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {int _counter = 0;void _incrementCounter() {setState(() {// This call to setState tells the Flutter framework that something has// changed in this State, which causes it to rerun the build method below// so that the display can reflect the updated values. If we changed// _counter without calling setState(), then the build method would not be// called again, and so nothing would appear to happen._counter++;});}@overrideWidget build(BuildContext context) {// This method is rerun every time setState is called, for instance as done// by the _incrementCounter method above.//// The Flutter framework has been optimized to make rerunning build methods// fast, so that you can just rebuild anything that needs updating rather// than having to individually change instances of widgets.return Scaffold(appBar: AppBar(// TRY THIS: Try changing the color here to a specific color (to// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar// change color while the other colors stay the same.backgroundColor: Theme.of(context).colorScheme.inversePrimary,// Here we take the value from the MyHomePage object that was created by// the App.build method, and use it to set our appbar title.title: Text(widget.title),),body: ListView(children: [const ListTile(leading: Icon(Icons.map), // 标题图片title: Text('Map'),),ListTile(leading: Icon(Icons.photo_album), // 标题图片title: Text('Album'),),ListTile(leading: Icon(Icons.photo_camera), // 标题图片title: Text('Camera'),),ListTile(leading: Icon(Icons.countertops), // 标题图片title: Text('当前计数$_counter',style: Theme.of(context).textTheme.headlineMedium,),),],),floatingActionButton: FloatingActionButton(onPressed: _incrementCounter,tooltip: 'Increment',child: const Icon(Icons.add),), // This trailing comma makes auto-formatting nicer for build methods.);}
}

2.2 水平列表

ListView设置显示水平布局,增加属性 scrollDirection: Axis.horizontal, 则显示水平列表。

import 'package:flutter/material.dart';void main() => runApp(const MyApp());class MyApp extends StatelessWidget {const MyApp({super.key});@overrideWidget build(BuildContext context) {const title = 'Horizontal List';return MaterialApp(title: title,home: Scaffold(appBar: AppBar(title: const Text(title),),body: Container(margin: const EdgeInsets.symmetric(vertical: 20),height: 200,child: ListView(// This next line does the trick.scrollDirection: Axis.horizontal,children: <Widget>[Container(width: 160,color: Colors.red,),Container(width: 160,color: Colors.blue,),Container(width: 160,color: Colors.green,),Container(width: 160,color: Colors.yellow,),Container(width: 160,color: Colors.orange,),],),),),);}
}

这篇关于Flutter第六弹 基础列表ListView的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

ps基础入门

1.基础      1.1新建文件      1.2创建指定形状      1.4移动工具          1.41移动画布中的任意元素          1.42移动画布          1.43修改画布大小          1.44修改图像大小      1.5框选工具      1.6矩形工具      1.7图层          1.71图层颜色修改          1

一道经典Python程序样例带你飞速掌握Python的字典和列表

Python中的列表(list)和字典(dict)是两种常用的数据结构,它们在数据组织和存储方面有很大的不同。 列表(List) 列表是Python中的一种有序集合,可以随时添加和删除其中的元素。列表中的元素可以是任何数据类型,包括数字、字符串、其他列表等。列表使用方括号[]表示,元素之间用逗号,分隔。 定义和使用 # 定义一个列表 fruits = ['apple', 'banana

[FPGA][基础模块]跨时钟域传播脉冲信号

clk_a 周期为10ns clk_b 周期为34ns 代码: module pulse(input clk_a,input clk_b,input signal_a,output reg signal_b);reg [4:0] signal_a_widen_maker = 0;reg signal_a_widen;always @(posedge clk_a)if(signal_a)

00 - React 基础

1. React 基础 安装react指令 可参考: 官网官网使用教程 如: npx create-react-app 项目名如:npx create-react-app react-redux-pro JSX JSX 是一种 JavaScript 的语法扩展,类似于 XML 或 HTML,允许我们在 JavaScript 代码中编写 HTML。 const element =

AI赋能天气:微软研究院发布首个大规模大气基础模型Aurora

编者按:气候变化日益加剧,高温、洪水、干旱,频率和强度不断增加的全球极端天气给整个人类社会都带来了难以估计的影响。这给现有的天气预测模型提出了更高的要求——这些模型要更准确地预测极端天气变化,为政府、企业和公众提供更可靠的信息,以便做出及时的准备和响应。为了应对这一挑战,微软研究院开发了首个大规模大气基础模型 Aurora,其超高的预测准确率、效率及计算速度,实现了目前最先进天气预测系统性能的显著

【软考】信息系统项目管理师(高项)备考笔记——信息系统项目管理基础

信息系统项目管理基础 日常笔记 项目的特点:临时性(一次性)、独特的产品、服务或成果、逐步完善、资源约束、目的性。 临时性是指每一个项目都有确定的开始和结束日期独特性,创造独特的可交付成果,如产品、服务或成果逐步完善意味着分步、连续的积累。例如,在项目早期,项目范围的说明是粗略的,随着项目团队对目标和可交付成果的理解更完整和深入时,项目的范围也就更具体和详细。 战略管理包括以下三个过程

众所周知,配置即代码≠基础设置即代码

​前段时间翻到几条留言,问: “配置即代码和基础设施即代码一样吗?” “配置即代码是什么?怎么都是基础设施即代码?” 我们都是知道,DevOp的快速发展,让服务器管理与配置的时间大大减少,配置即代码和基础设施即代码作为DevOps的重要实践,在其中起到了关键性作用。 不少人将二者看作是一件事,配置即大代码是关于管理特定的应用程序配置设置本身,而基础设施即代码更关注的是部署支持应用程序环境所需的