ListTile 一般用于在 Flutter 中填充 ListView。在这篇文章中,我将用可视化的例子来讲明全部的参数。bash
title 参数能够接受任何小部件,但一般是文本小部件app
ListTile(
title: Text('Horse'),
)
复制代码
副标题是标题下面较小的文本less
ListTile(
title: Text('Horse'),
subtitle: Text('A strong animal'),
)
复制代码
使文本更小,并将全部内容打包在一块儿ide
ListTile(
title: Text('Horse'),
subtitle: Text('A strong animal'),
dense: true,
)
复制代码
将图像或图标添加到列表的开头。这一般是一个图标。布局
ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(imageUrl),
),
title: Text('Horse'),
)
复制代码
ListTile(
leading: Icon(Icons.home),
title: Text('House'),
)
复制代码
设置拖尾将在列表的末尾放置一个图像。这对于指示主-细节布局特别有用。ui
ListTile(
title: Text('Horse'),
trailing: Icon(Icons.keyboard_arrow_right),
)
复制代码
设置内容边距,默认是 16,但咱们在这里设置为 0this
ListTile(
title: Text('Horse'),
trailing: Icon(Icons.keyboard_arrow_right),
contentPadding: EdgeInsets.symmetric(horizontal: 0.0),
)
复制代码
若是选中列表的 item 项,那么文本和图标的颜色将成为主题的主颜色。spa
ListTile(
title: Text('Horse'),
trailing: Icon(Icons.keyboard_arrow_right),
selected: true,
)
复制代码
ListTile 能够检测用户的点击和长按事件,onTap 为单击,onLongPress 为长按。对于波纹效果是内置的debug
ListTile(
title: Text('Horse'),
onTap: () {
// do something
},
onLongPress: (){
// do something else
},
)
复制代码
经过将 enable 设置为 false,来禁止点击事件3d
ListTile(
title: Text('Horse'),
onTap: () {
// this will not get called
},
enabled: false,
)
复制代码
静态方法 divideTiles 能够在 titles 之间添加分隔符,这个颜色有点淡,须要看仔细点才能看出来,哈哈哈哈
ListView(
children: ListTile.divideTiles(
context: context,
tiles: [
ListTile(
title: Text('Horse'),
),
ListTile(
title: Text('Cow'),
),
ListTile(
title: Text('Camel'),
),
ListTile(
title: Text('Sheep'),
),
ListTile(
title: Text('Goat'),
),
]
).toList(),
)
复制代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(title: Text('ListTile guide')),
body: BodyWidget(),
),
);
}
}
String horseUrl = 'https://i.stack.imgur.com/Dw6f7.png';
String cowUrl = 'https://i.stack.imgur.com/XPOr3.png';
String camelUrl = 'https://i.stack.imgur.com/YN0m7.png';
String sheepUrl = 'https://i.stack.imgur.com/wKzo8.png';
String goatUrl = 'https://i.stack.imgur.com/Qt4JP.png';
class BodyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListView(
children: <Widget>[
ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(horseUrl),
),
title: Text('Horse'),
subtitle: Text('A strong animal'),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
print('horse');
},
selected: true,
),
ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(cowUrl),
),
title: Text('Cow'),
subtitle: Text('Provider of milk'),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
print('cow');
},
),
ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(camelUrl),
),
title: Text('Camel'),
subtitle: Text('Comes with humps'),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
print('camel');
},
enabled: false,
),
ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(sheepUrl),
),
title: Text('Sheep'),
subtitle: Text('Provides wool'),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
print('sheep');
},
),
ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(goatUrl),
),
title: Text('Goat'),
subtitle: Text('Some have horns'),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
print('goat');
},
),
],
);
}
}
复制代码
- 陈坚润:广州芦苇科技 APP 团队 Android 开发工程师
- 咱们正在招募小伙伴,有兴趣的小伙伴能够把简历发到 app@talkmoney.cn,备注:来自掘金社区
- 详情能够戳这里--> 广州芦苇信息科技