canteen_dish_listview.dart 3.7 KB

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