15.2 ListTileWidget 组件封装

15.2 ListTileWidget 组件封装

实现步骤:


官方的 ListTile 虽然很方便,但是有两个主要问题

  1. 不能改高度
  2. 自定义能力还有点不足

所以我们项目中需要自己去实现一个 ListTileWidget

第 1 步:功能分析

img

功能:

  1. 左右图标
  2. 标题、子标题、描述
  3. 尺寸、间距、边距
  4. 事件、点击、长按

第 2 步:组件代码

lib/common/widgets/list_tile.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import 'package:flutter/material.dart';

import '../index.dart';

/// 列表行 ListTile 替代版本
class ListTileWidget extends StatelessWidget {
/// 标题
final Widget? title;

/// 子标题
final Widget? subtitle;

/// 描述
final Widget? description;

/// 左侧图标
final Widget? leading;

/// 左侧图标间距
final double? leadingSpace;

/// 右侧图标
final List<Widget>? trailing;

/// padding 边框间距
final EdgeInsetsGeometry? padding;

/// cross 对齐方式
final CrossAxisAlignment? crossAxisAlignment;

/// 点击事件
final GestureTapCallback? onTap;

/// 长按事件
final GestureLongPressCallback? onLongPress;

ListTileWidget({
Key? key,
this.title,
this.subtitle,
this.description,
this.leading,
this.leadingSpace,
this.trailing,
EdgeInsetsGeometry? padding,
this.crossAxisAlignment,
this.onTap,
this.onLongPress,
}) : padding = padding ?? AppSpace.edgeInput,
super(key: key);

_buildView() {
List<Widget> ws = [];

// 头部图标
if (leading != null) {
ws.add(
leading!.paddingRight(
leadingSpace ?? AppSpace.iconTextSmail,
),
);
}

// 标题/子标题/描述
List<Widget> titles = [
if (title != null) title!,
if (subtitle != null) subtitle!,
if (description != null) description!,
];
MainAxisAlignment titleMainAxisAlignment = titles.length == 1
? MainAxisAlignment.center
: MainAxisAlignment.spaceBetween;
ws.add(
titles
.toColumn(
mainAxisAlignment: titleMainAxisAlignment,
crossAxisAlignment: CrossAxisAlignment.start,
)
.expanded(),
);

// 右侧图标
if (trailing != null) {
MainAxisAlignment trailingMainAxisAlignment = trailing?.length == 1
? MainAxisAlignment.center
: MainAxisAlignment.spaceBetween;
ws.add(
trailing!.toColumn(
mainAxisAlignment: trailingMainAxisAlignment,
),
);
}

return ws
.toRow(
crossAxisAlignment: crossAxisAlignment ?? CrossAxisAlignment.center,
)
.backgroundColor(Colors.transparent)
.padding(value: padding)
.onTap(onTap)
.onLongPress(onLongPress);
}

@override
Widget build(BuildContext context) {
return _buildView();
}
}

提交代码到 git