canteen_dish_listview.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'package:canteen/model/dish_model.dart';
  2. import 'package:flutter/material.dart';
  3. /// Description: 食堂菜谱
  4. /// Time : 07/25/2022 Monday
  5. /// Author : liuyuqi.gov@msn.cn
  6. class CanteenDishListView extends StatelessWidget {
  7. final List<DishModel> dishes;
  8. final void Function(int index)? onLikePressed;
  9. final void Function(int index)? onStarPressed;
  10. CanteenDishListView(this.dishes,
  11. {this.onLikePressed = null, this.onStarPressed = null});
  12. @override
  13. Widget build(BuildContext context) {
  14. /// 列表
  15. return ListView.separated(
  16. shrinkWrap: true,
  17. addRepaintBoundaries: true,
  18. itemCount: dishes.length,
  19. itemBuilder: (BuildContext context, int index) {
  20. return _DishListViewItem(
  21. index, dishes[index], onLikePressed, onStarPressed);
  22. },
  23. separatorBuilder: (context, index) {
  24. return const Divider(color: Colors.grey);
  25. },
  26. );
  27. }
  28. }
  29. class _DishListViewItem extends StatelessWidget {
  30. final int id;
  31. final DishModel dish;
  32. final void Function(int index)? onLikePressed;
  33. final void Function(int index)? onStarPressed;
  34. const _DishListViewItem(
  35. this.id, this.dish, this.onLikePressed, this.onStarPressed);
  36. @override
  37. //5:3=20:12
  38. //4:3=12:9
  39. Widget build(BuildContext context) {
  40. return Row(
  41. mainAxisAlignment: MainAxisAlignment.end,
  42. children: [
  43. Expanded(
  44. child: Column(
  45. children: [
  46. Padding(
  47. child: Row(
  48. children: [
  49. Text(
  50. dish.name,
  51. textScaleFactor: 2,
  52. ),
  53. Expanded(
  54. child: Row(
  55. mainAxisAlignment: MainAxisAlignment.end,
  56. children: [
  57. Padding(
  58. child: Text(
  59. dish.cost,
  60. textScaleFactor: 1.2,
  61. ),
  62. padding: EdgeInsets.fromLTRB(0, 0, 16, 0),
  63. )
  64. ]))
  65. ],
  66. ),
  67. padding: EdgeInsets.fromLTRB(6, 4, 4, 4)),
  68. Padding(
  69. child: Row(
  70. children: [
  71. Text(
  72. dish.canteen,
  73. textScaleFactor: 0.9,
  74. ),
  75. Expanded(
  76. child: Row(
  77. mainAxisAlignment: MainAxisAlignment.end,
  78. children: [
  79. Padding(
  80. child: Text(
  81. dish.time,
  82. textScaleFactor: 0.9,
  83. ),
  84. padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
  85. )
  86. ]))
  87. ],
  88. ),
  89. padding: EdgeInsets.fromLTRB(6, 0, 0, 6),
  90. )
  91. ],
  92. ),
  93. ),
  94. IconButton(
  95. onPressed: () {
  96. if (onLikePressed != null) onLikePressed!(this.id);
  97. },
  98. icon: Icon(dish.like ? Icons.favorite : Icons.favorite_border,
  99. color: dish.like ? Colors.red : null)),
  100. IconButton(
  101. onPressed: () {
  102. if (onStarPressed != null) onStarPressed!(this.id);
  103. },
  104. icon: Icon(dish.star ? Icons.star : Icons.star_border,
  105. color: dish.star ? Colors.amber : null)),
  106. ],
  107. );
  108. }
  109. }