Flutter布局基础——Card

Card,卡片式布局,带有一点圆角和阴影。通常用于关联信息的展示,比如:相册信息、经纬度、联系人信息等等。

Card的使用

来看一下,如何做一个,常见的列表元素的控件,左侧是个Icon,上面是title,然后是desc,最下面是按钮,常见于订单列表。

要实现的效果如下:

Flutter布局基础——Card - 图1

然后看如何实现:

ListTile

这里需要介绍一下ListTile,Flutter提供的固定高度的,左侧或右侧带有Icon以及文案的控件。

可实现效果如下:

Flutter布局基础——Card - 图2

代码如下:

  1. class MyApp extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. home: Scaffold(
  6. appBar: new AppBar(
  7. title: new Text('ListTile View'),
  8. ),
  9. body: ListView(
  10. children: [
  11. Card(
  12. child: ListTile(
  13. title: Text('One-line ListTile'),
  14. ),
  15. ),
  16. Card(
  17. child: ListTile(
  18. leading: FlutterLogo(),
  19. title: Text('One-line with leading widget'),
  20. ),
  21. ),
  22. Card(
  23. child: ListTile(
  24. title: Text('One-Line with trailing widget'),
  25. trailing: Icon(Icons.more_vert),
  26. ),
  27. ),
  28. Card(
  29. child: ListTile(
  30. leading: FlutterLogo(),
  31. title: Text('One-line with both widgets'),
  32. trailing: Icon(Icons.more_vert),
  33. ),
  34. ),
  35. Card(
  36. child: ListTile(
  37. title: Text('One-line dense ListTile'),
  38. dense: true,
  39. ),
  40. ),
  41. Card(
  42. child: ListTile(
  43. leading: FlutterLogo(
  44. size: 56.0,
  45. ),
  46. title: Text('Two-line listTile'),
  47. subtitle: Text('Here is a second line'),
  48. trailing: Icon(Icons.more_vert),
  49. ),
  50. ),
  51. Card(
  52. child: ListTile(
  53. leading: FlutterLogo(),
  54. title: Text('Three-line ListTile'),
  55. subtitle:
  56. Text('A sufficiently long subtitle warrants three lines'),
  57. trailing: Icon(Icons.more_vert),
  58. isThreeLine: true,
  59. ),
  60. )
  61. ],
  62. ),
  63. ),
  64. );
  65. }
  66. }

上面的代码介绍了ListTile的使用,已经覆盖很全面。

然后再来看最初想要实现的效果,分割成已知的基础控件如下:

Flutter布局基础——Card - 图3

Icon加右侧的title和desc可以使用上面介绍的ListTile,下面单个按钮可以使用TextButton(额,还没介绍,稍等补上一篇,目前可以用Text),两个按钮水平布局可以使用RowListTileRow的上下布局可以使用Column,然后最外层再用Card,卡片对象包括起来,所以最终代码如下:

  1. class MyApp extends StatelessWidget {
  2. const MyApp({Key? key}) : super(key: key);
  3. static const String _title = 'Card View';
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: 'Card View',
  8. home: Scaffold(
  9. appBar: new AppBar(title: new Text(_title)),
  10. body: const MyStatelessWidget(),
  11. ));
  12. }
  13. }
  14. class MyStatelessWidget extends StatelessWidget {
  15. const MyStatelessWidget({Key? key}) : super(key: key);
  16. @override
  17. Widget build(BuildContext context) {
  18. return Center(
  19. child: Card(
  20. child: Column(
  21. children: [
  22. const ListTile(
  23. leading: Icon(Icons.album),
  24. title: Text('The Enchanted nigtingale'),
  25. subtitle: Text('Music by Julie Gable, Lyrics By Sidney Stenic'),
  26. ),
  27. Row(
  28. mainAxisAlignment: MainAxisAlignment.end,
  29. children: [
  30. TextButton(onPressed: () {}, child: Text('BUY TICKETS')),
  31. SizedBox(
  32. width: 8,
  33. ),
  34. TextButton(onPressed: () {}, child: const Text('LISTEN')),
  35. SizedBox(width: 8),
  36. ],
  37. )
  38. ],
  39. mainAxisSize: MainAxisSize.min,
  40. ),
  41. ),
  42. );
  43. }
  44. }

最终效果就为上面的效果了,要注意Card的使用,通常是结合ColumnRow等其他集合类的组件来实现的。

参考

Card Dev Doc
ListTile Dev Doc
Flutter免费视频第三季-布局