Flutter Widget 之ListTile

482 阅读1分钟

有时候你希望ListView里面的item,能够遵循Material Design规范, image.png

你可以花数小时实现完美布局,row、column、container或者padding,或者你可以只用ListTile

image.png

ListTile为你实现Material Design模式的列表规范,你只关注放什么内容进去就好

ListTile(
    
);

你想知道标题的前面的图标,使用leading属性,后面的图标使用trailing属性,中间的文本使用title属性,占用一行或两行的文本使用subtitle属性,三行使用isThreeLine属性

ListTile(
    leading: icon,
    title: "Widget of the week",
    subtitle: "#54...",
    isThreeLine: true,
    trailing: icon,
);

image.png

使用density属性,可以使内容变得更小、更紧密

ListTile(
    leading: icon,
    title: "Widget of the week",
    subtitle: "#54...",
    isThreeLine: true,
    dense: true,
    trailing: icon,
);

image.png

使用onTap和onLongPress回掉

ListTile(
    leading: icon,
    title: "Widget of the week",
    subtitle: "#54...",
    onTap: () => _tapCallback,
    trailing: icon,
);
ListTile(
    leading: icon,
    title: "Widget of the week",
    subtitle: "#54...",
    onLongPress: () => _pressCallback,
    trailing: icon,
);

ezgif.com-gif-maker.gif

给用户提供有关ListTile交互性的一些提示,使用enabled属性或者selected属性设置样式

ListTile(
    leading: icon,
    title: "Widget of the week",
    subtitle: "#54...",
    enabled: false,
    trailing: icon,
);

image.png

ListTile(
    leading: icon,
    title: "Widget of the week",
    subtitle: "#54...",
    selected: true,
    trailing: icon,
);

image.png

你现在有一个符合Material规范的列表了

如果想了解有关ListTile的内容,或者关于Flutter的其他功能,请访问flutter.dev

原文翻译自视频:视频地址