实现步骤:
官方的 ListTile
虽然很方便,但是有两个主要问题
- 不能改高度
- 自定义能力还有点不足
所以我们项目中需要自己去实现一个 ListTileWidget
第 1 步:功能分析
功能:
- 左右图标
- 标题、子标题、描述
- 尺寸、间距、边距
- 事件、点击、长按
第 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';
class ListTileWidget extends StatelessWidget { final Widget? title;
final Widget? subtitle;
final Widget? description;
final Widget? leading;
final double? leadingSpace;
final List<Widget>? trailing;
final EdgeInsetsGeometry? padding;
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