业务显示需要封装一个ListTile(cell),要求如下:
1.左边一个icon;
2.icon右边一个title,需要完全显示;
3.最右边一个右箭头icon;
4.右箭头左边一个subTitle,如果文本过长,可以省略号显示。
5.title与 subtitle之间最小距离为5 。
ListTile 代码如下:
import 'package:flutter/material.dart';
class UserInfoTile extends StatelessWidget {
final IconData leadingIcon;
final String title;
final String subTitle;
final IconData trailingIcon;
const UserInfoTile({
super.key,
required this.leadingIcon,
required this.title,
required this.subTitle,
this.trailingIcon = Icons.arrow_forward_ios,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 2.0),
child: Row(
children: [
Icon(leadingIcon),
const SizedBox(width: 5),
Text(
title,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(width: 5), // title与subtitle的最小距离
Expanded(
child: Container(
height: double.infinity, // 占据剩余空间的容器
color: Colors.green,
child: const Center(
child: Text(
subTitle,
style: TextStyle(color: Colors.white),
overflow: TextOverflow.ellipsis, // subtitle溢出时省略
maxLines: 3,
),
),
),
),
const SizedBox(width: 5),
Icon(trailingIcon),
],
),
);
}
}
使用:
import 'package:flutter/material.dart';
import 'user_info_tile.dart';
class ListPage extends StatelessWidget {
const ListPage({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('列表页面示例'),
),
body: type1(),
),
);
}
/// 样式1
Widget type1() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ListView.builder(
itemCount: 18,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(bottom: 5.0), // 设置cell上下间距
child: Container(
height: 77,
decoration: BoxDecoration(
color: Colors.blueAccent,
borderRadius: BorderRadius.circular(8.0),
),
child: UserInfoTile(
leadingIcon: Icons.account_circle,
title: '用户 $index',
subTitle: '特斯拉CEO埃隆·马斯克在社交媒体平台X上对苹果新发布的iPhone 16系列进行了公开的嘲讽,表示其新款手机基本上没有实质性的新意,令人不禁深思这一现象背后的意义。',
),
),
);
},
),
);
}
}