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,),],),),),);}
}

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

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

相关文章

性能分析--内存知识

内存相关知识 计算机中与CPU进行数据交换的桥梁。内存的速度&#xff0c;比CPU的速度要慢很多。比磁盘速度要快很多。内存中存放数据&#xff0c;一旦断电就会消失。linux系统的 /proc路径下的文件&#xff0c;都是内存文件。内存大小&#xff0c;一般 是GB为单位。 现在都操作…

WebKit是什么?

WebKit是一个开源的浏览器引擎&#xff0c;它用于呈现网页内容在许多现代浏览器中&#xff0c;包括Safari浏览器、iOS内置浏览器、以及一些其他浏览器如Google Chrome的早期版本。以下是一些关于WebKit的重要信息&#xff1a; 起源和发展&#xff1a;WebKit最初是由苹果公司为其…

K8s学习四(资源调度_1)

资源调度 发现对Pod操作不方便&#xff0c;不能直接操作&#xff0c;而且不能直接编辑&#xff0c;需要对原来的配置文件进行操作&#xff0c;而且需要删除之后再创建Pod&#xff0c;不方便&#xff0c;更多是通过控制器来操作。 Label和Selector 通过设置标签和选择器来确定…

HTTP【超文本传输协议】和HTTPS【超文本传输安全协议】有什么区别?

文章目录 一、HTTP和HTTPS是什么&#xff1f;1、HTTP&#xff08;超文本传输协议&#xff09;2、HTTPS&#xff08;超文本传输安全协议&#xff09;3、HTTPS中加入的SSL/TLS层是什么&#xff1f; 二、HTTP和HTTPS的差异1、安全性2、URL表示3、端口4、证书5、性能6、浏览器显示 …

Python爬虫:为什么你爬取不到网页数据

目录 前言 一、网络请求被拒绝 二、数据是通过JavaScript加载的 三、需要进行登录 四、网站反爬虫策略 五、网站结构变更 总结 前言 作为一名开发者&#xff0c;使用Python编写爬虫程序是一项常见的任务。爬虫程序的目的是收集互联网上的数据&#xff0c;并将其保存或使…

P1616 疯狂的采药(完全背包问题)

题目&#xff1a;P1616 疯狂的采药 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) #include<iostream> #include<algorithm> using namespace std; const int maxm 10010, maxt 10000010; long long v[maxm], t[maxm], f[maxt];//开longlong&#xff01; int m…

解决IDEA 控制台中文乱码

运行某个项目时IntelliJ IDEA 控制台中文乱码&#xff0c;但其他的项目是正常的。接口文档也显示乱码&#xff1a; 一、修改 IntelliJ IDEA 全局编码、项目编码、属性文件编码 上方导航栏“File→Settings…”进入配置页面&#xff0c;在“Editor”中下滑找到“File Encodings…

LeetCode 面试题 02.07.链表相交(判断两个结点是否相同)

给你两个单链表的头节点 headA 和 headB &#xff0c;请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点&#xff0c;返回 null 。 图示两个链表在节点 c1 开始相交&#xff1a; 题目数据 保证 整个链式结构中不存在环。 注意&#xff0c;函数返回结果后&#x…

Elasticsearch如何选择版本

不同版本的ES差异非常大&#xff0c;包括不局限于ES语法、架构、API、集群搭建等等。这些差异足以导致不同版本是否能满足你的业务场景以及后续开发维护成本等各种问题。 先说结论&#xff0c;以个人实践经验及综合考虑推荐使用 7.x 版本中的 7.10版本 ES版本对比 以下是通过…

内外网数据交换发展进程:安全与便捷并行

随着信息化的不断推进&#xff0c;医院、党政以及企业的内外网数据交换正成为日益关注的焦点。在保障数据安全的前提下&#xff0c;需要寻求一种既安全可靠又操作便捷的数据传输方式。本文将探讨内外网数据交换发展进程&#xff0c;分析各种传输方式的优缺点&#xff0c;以及它…

麒麟系统ARM安装rabbitmq

简单记录下&#xff0c;信创服务器&#xff1a;麒麟系统&#xff0c;安装rabbitmq的踩坑记录。 本文章参考了很多大佬文章&#xff0c;我整理后提供。 一、安装基础依赖 yum -y install make gcc gcc-c kernel-devel m4 ncurses-devel openssl-devel unixODBC-devel 二、下载…

k8s资源监控_bitnami metrics-server v0(1),2024一位Linux运维中级程序员的跳槽面经

错误3 也有可能会遇到以下错误&#xff0c;按照下面提示解决 Error from server (ServiceUnavailable): the server is currently unable to handle the request (get nodes.metrics.k8s.io) 如果metrics-server正常启动&#xff0c;没有错误&#xff0c;应该就是网络问题。修改…

花一分钟简单认识 CSS 中的规则 —— 级联层 @layer

layer 简介&#xff1a; 声明级联层时&#xff0c;越靠后优先级越高。不属于任何级联层的样式&#xff0c;将自成一层匿名级联层&#xff0c;并置于所有层之后 —— 级别最高。 用法一&#xff1a;在同一文件中 layer base, special; layer special {/* 优先 */li { color: …

Android查看SO库的依赖

➜ bin pwd /Users/xxx/Library/Android/sdk/ndk/21.1.6352462/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin ➜ bin ./aarch64-linux-android-readelf -d /Download/libxxx.so 0x0000000000000001 (NEEDED) Shared library: [liblog.so]0x…

Python学习笔记——heapq

堆排序 思路 堆排序思路是&#xff1a; 将数组以二叉树的形式分析&#xff0c;令根节点索引值为0&#xff0c;索引值为index的节点&#xff0c;子节点索引值分别为index*21、index*22&#xff1b;对二叉树进行维护&#xff0c;使得每个非叶子节点的值&#xff0c;都大于或者…

Day32|贪心算法part02:122.买卖股票的最佳时机II、55. 跳跃游戏、45. 跳跃游戏II

122. 买卖股票的最佳时机II 这题应该是dp的主菜&#xff0c;II的要求是可以无限次买无限次卖&#xff0c;可以用贪心做&#xff0c;想了下没想到思路&#xff0c;直接看题解。 贪心策略&#xff1a; 一直统计每次的差值&#xff0c;只要为负&#xff0c;不卖出&#xff0c;选…

ubuntu怎么按安装时间显示已安装的软件

在Ubuntu系统中&#xff0c;dpkg 或 apt 命令本身并不直接提供按照安装时间排序已安装软件的功能。然而&#xff0c;可以通过间接的方式获取这一信息。通常&#xff0c;软件包的安装时间记录在系统的日志文件中&#xff0c;尤其是与包管理相关的日志。以下是一种方法来查看已安…

2024-4-7 QT day1作业

myWidget.cpp #include "mywidget.h"MyWidget::MyWidget(QWidget *parent): QWidget(parent) {//设置窗口标题this->setWindowTitle("QQ");//设置窗口图标this->setWindowIcon(QIcon("C:\\Users\\张谦\\Desktop\\pictrue\\qq.png"));//设…

git bash上传文件至github仓库

Linux运维工具-ywtool 目录 一.访问github二.新建仓库1.点击自己头像2.选择"your repositories"3.点击"New"4.创建新仓库 三.通过git bash软件上传文件1.提示2.打开git bash软件3.切换到本地仓库目录4.配置github的用户名和邮箱信息5.生成SSH Key6.github添…

day19-二叉树part06

654.最大二叉树 class Solution {public TreeNode constructMaximumBinaryTree(int[] nums) {return constructMaximumBinaryTree1(nums,0,nums.length);}public TreeNode constructMaximumBinaryTree1(int[] nums,int leftIndex,int rightIndex){if(rightIndex - leftIndex &…