campus_listview.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'package:canteen/views/chzn_expansion_panel_list.dart';
  2. import 'package:flutter/material.dart';
  3. /// Description: 首页-校区列表
  4. /// Time : 07/25/2022 Monday
  5. /// Author : liuyuqi.gov@msn.cn
  6. class CampusListView extends StatefulWidget {
  7. static const campus = ["中心", "软件园", "洪家楼", "趵突泉", "千佛山", "兴隆山", "青岛", "威海"];
  8. static const campusImage = [
  9. "zhongxin",
  10. "ruanjian",
  11. "honglou",
  12. "baottu",
  13. "qianfo",
  14. "xinglong",
  15. "qianfo",
  16. "zhongxin"
  17. ];
  18. late final _campusList;
  19. @override
  20. State<CampusListView> createState() {
  21. _campusList = _createCampus(campus, campusImage);
  22. return _CampusListViewState();
  23. }
  24. List<_CampusViewItem> _createCampus(
  25. List<String> campus, List<String> campusImage) {
  26. return List.generate(campus.length, (index) {
  27. return _CampusViewItem(
  28. header: ListTile(
  29. leading: ClipOval(
  30. child: Image.asset(
  31. "assets/location_${campusImage[index]}.png",
  32. width: 30,
  33. height: 30,
  34. )),
  35. title: Text('${campus[index]}校区'),
  36. ),
  37. body: Container(
  38. color: Colors.white,
  39. child: Padding(
  40. padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
  41. child: Container(
  42. decoration: BoxDecoration(
  43. color: Colors.white,
  44. borderRadius: BorderRadius.circular(10.0),
  45. boxShadow: const [
  46. BoxShadow(color: Colors.black12, blurRadius: 8.0)
  47. ]),
  48. child: Center(
  49. child: Column(
  50. children: [
  51. ElevatedButton(
  52. onPressed: () {
  53. //跳转到相应界面 传参_selectCampus
  54. },
  55. child: const Text("当日菜品(固定)"), //这里对齐太难处理,我偷懒用全角空格对齐
  56. style: ButtonStyle(
  57. shape: MaterialStateProperty.all(
  58. const StadiumBorder(side: BorderSide.none)),
  59. ),
  60. ),
  61. ElevatedButton(
  62. onPressed: () {},
  63. child: const Text("特色菜品"),
  64. style: ButtonStyle(
  65. shape: MaterialStateProperty.all(
  66. const StadiumBorder(
  67. side: BorderSide.none)))),
  68. ElevatedButton(
  69. onPressed: () {},
  70. child: const Text("当日新菜"),
  71. style: ButtonStyle(
  72. shape: MaterialStateProperty.all(
  73. const StadiumBorder(
  74. side: BorderSide.none)))),
  75. ],
  76. ),
  77. ),
  78. ))),
  79. );
  80. });
  81. }
  82. }
  83. class _CampusListViewState extends State<CampusListView> {
  84. int select = -1;
  85. int _selectCampus = -1;
  86. @override
  87. Widget build(BuildContext context) {
  88. return Expanded(
  89. child: SingleChildScrollView(
  90. /// 折叠列表
  91. child: ChznExpansionPanelList(
  92. expansionCallback: (int index, bool isExpanded) {
  93. setState(() {
  94. if (!isExpanded) {
  95. if (select != -1) widget._campusList[select].isExpanded = false;
  96. select = index;
  97. widget._campusList[select].isExpanded = true;
  98. } else {
  99. widget._campusList[index].isExpanded = false;
  100. select = -1;
  101. }
  102. _selectCampus = select;
  103. });
  104. },
  105. children: widget._campusList.map<ExpansionPanel>((_CampusViewItem item) {
  106. return ExpansionPanel(
  107. headerBuilder: (BuildContext context, bool isExpanded) {
  108. return item.header;
  109. },
  110. body: item.body,
  111. isExpanded: item.isExpanded,
  112. canTapOnHeader: true,
  113. backgroundColor:
  114. item.isExpanded ? const Color(0xFFF0F0F0) : Colors.white);
  115. }).toList(),
  116. expandedHeaderPadding: const EdgeInsets.all(0),
  117. elevation: 0,
  118. )));
  119. }
  120. }
  121. class _CampusViewItem {
  122. Widget body;
  123. Widget header;
  124. bool isExpanded;
  125. int id;
  126. _CampusViewItem(
  127. {required this.body,
  128. required this.header,
  129. this.isExpanded = false,
  130. this.id = 0});
  131. }