当前位置:   article > 正文

Flutter第六弹 基础列表ListView

Flutter第六弹 基础列表ListView

目标:

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

2)怎么定制列表项Item?

一、ListView简介

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

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

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

  1. ListView(
  2. children: const <Widget>[
  3. ListTile(
  4. leading: Icon(Icons.map),
  5. title: Text('Map'),
  6. ),
  7. ListTile(
  8. leading: Icon(Icons.photo_album),
  9. title: Text('Album'),
  10. ),
  11. ListTile(
  12. leading: Icon(Icons.phone),
  13. title: Text('Phone'),
  14. ),
  15. ],
  16. ),

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 组件。

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

title属性

列表Item标题。

titleTextStyle属性

标题文本样式

titleAlignment属性

标题文本的对齐属性

subtitle属性

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

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

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 竖直列表

对应的代码

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({super.key});
  7. // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. // This is the theme of your application.
  14. //
  15. // TRY THIS: Try running your application with "flutter run". You'll see
  16. // the application has a purple toolbar. Then, without quitting the app,
  17. // try changing the seedColor in the colorScheme below to Colors.green
  18. // and then invoke "hot reload" (save your changes or press the "hot
  19. // reload" button in a Flutter-supported IDE, or press "r" if you used
  20. // the command line to start the app).
  21. //
  22. // Notice that the counter didn't reset back to zero; the application
  23. // state is not lost during the reload. To reset the state, use hot
  24. // restart instead.
  25. //
  26. // This works for code too, not just values: Most code changes can be
  27. // tested with just a hot reload.
  28. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  29. useMaterial3: true,
  30. ),
  31. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  32. );
  33. }
  34. }
  35. class MyHomePage extends StatefulWidget {
  36. const MyHomePage({super.key, required this.title});
  37. // This widget is the home page of your application. It is stateful, meaning
  38. // that it has a State object (defined below) that contains fields that affect
  39. // how it looks.
  40. // This class is the configuration for the state. It holds the values (in this
  41. // case the title) provided by the parent (in this case the App widget) and
  42. // used by the build method of the State. Fields in a Widget subclass are
  43. // always marked "final".
  44. final String title;
  45. @override
  46. State<MyHomePage> createState() => _MyHomePageState();
  47. }
  48. class _MyHomePageState extends State<MyHomePage> {
  49. int _counter = 0;
  50. void _incrementCounter() {
  51. setState(() {
  52. // This call to setState tells the Flutter framework that something has
  53. // changed in this State, which causes it to rerun the build method below
  54. // so that the display can reflect the updated values. If we changed
  55. // _counter without calling setState(), then the build method would not be
  56. // called again, and so nothing would appear to happen.
  57. _counter++;
  58. });
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. // This method is rerun every time setState is called, for instance as done
  63. // by the _incrementCounter method above.
  64. //
  65. // The Flutter framework has been optimized to make rerunning build methods
  66. // fast, so that you can just rebuild anything that needs updating rather
  67. // than having to individually change instances of widgets.
  68. return Scaffold(
  69. appBar: AppBar(
  70. // TRY THIS: Try changing the color here to a specific color (to
  71. // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
  72. // change color while the other colors stay the same.
  73. backgroundColor: Theme.of(context).colorScheme.inversePrimary,
  74. // Here we take the value from the MyHomePage object that was created by
  75. // the App.build method, and use it to set our appbar title.
  76. title: Text(widget.title),
  77. ),
  78. body: ListView(
  79. children: [
  80. const ListTile(
  81. leading: Icon(Icons.map), // 标题图片
  82. title: Text('Map'),
  83. ),
  84. ListTile(
  85. leading: Icon(Icons.photo_album), // 标题图片
  86. title: Text('Album'),
  87. ),
  88. ListTile(
  89. leading: Icon(Icons.photo_camera), // 标题图片
  90. title: Text('Camera'),
  91. ),
  92. ListTile(
  93. leading: Icon(Icons.countertops), // 标题图片
  94. title: Text(
  95. '当前计数$_counter',
  96. style: Theme.of(context).textTheme.headlineMedium,
  97. ),
  98. ),
  99. ],
  100. ),
  101. floatingActionButton: FloatingActionButton(
  102. onPressed: _incrementCounter,
  103. tooltip: 'Increment',
  104. child: const Icon(Icons.add),
  105. ), // This trailing comma makes auto-formatting nicer for build methods.
  106. );
  107. }
  108. }

2.2 水平列表

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

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(const MyApp());
  3. class MyApp extends StatelessWidget {
  4. const MyApp({super.key});
  5. @override
  6. Widget build(BuildContext context) {
  7. const title = 'Horizontal List';
  8. return MaterialApp(
  9. title: title,
  10. home: Scaffold(
  11. appBar: AppBar(
  12. title: const Text(title),
  13. ),
  14. body: Container(
  15. margin: const EdgeInsets.symmetric(vertical: 20),
  16. height: 200,
  17. child: ListView(
  18. // This next line does the trick.
  19. scrollDirection: Axis.horizontal,
  20. children: <Widget>[
  21. Container(
  22. width: 160,
  23. color: Colors.red,
  24. ),
  25. Container(
  26. width: 160,
  27. color: Colors.blue,
  28. ),
  29. Container(
  30. width: 160,
  31. color: Colors.green,
  32. ),
  33. Container(
  34. width: 160,
  35. color: Colors.yellow,
  36. ),
  37. Container(
  38. width: 160,
  39. color: Colors.orange,
  40. ),
  41. ],
  42. ),
  43. ),
  44. ),
  45. );
  46. }
  47. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/406600
推荐阅读
相关标签
  

闽ICP备14008679号