campus_listview.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. CampusListView({Key? key}) : super(key: key);
  20. @override
  21. State<CampusListView> createState() {
  22. _campusList = _createCampus(campus, campusImage);
  23. return _CampusListViewState();
  24. }
  25. List<_CampusViewItem> _createCampus(
  26. List<String> campus, List<String> campusImage) {
  27. return List.generate(campus.length, (index) {
  28. return _CampusViewItem(
  29. header: ListTile(
  30. leading: ClipOval(
  31. child: Image.asset(
  32. "assets/location_${campusImage[index]}.png",
  33. width: 30,
  34. height: 30,
  35. )),
  36. title: Text('${campus[index]}校区'),
  37. ),
  38. body: Container(
  39. color: Colors.white,
  40. child: Padding(
  41. padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
  42. child: Container(
  43. decoration: BoxDecoration(
  44. color: Colors.white,
  45. borderRadius: BorderRadius.circular(10.0),
  46. boxShadow: const [
  47. BoxShadow(color: Colors.black12, blurRadius: 8.0)
  48. ]),
  49. child: Center(
  50. child: Column(
  51. children: [
  52. ElevatedButton(
  53. onPressed: () {
  54. //跳转到相应界面 传参_selectCampus
  55. },
  56. child: const Text("当日菜品(固定)"), //这里对齐太难处理,我偷懒用全角空格对齐
  57. style: ButtonStyle(
  58. shape: MaterialStateProperty.all(
  59. const StadiumBorder(side: BorderSide.none)),
  60. ),
  61. ),
  62. ElevatedButton(
  63. onPressed: () {},
  64. child: const Text("特色菜品"),
  65. style: ButtonStyle(
  66. shape: MaterialStateProperty.all(
  67. const StadiumBorder(
  68. side: BorderSide.none)))),
  69. ElevatedButton(
  70. onPressed: () {},
  71. child: const Text("当日新菜"),
  72. style: ButtonStyle(
  73. shape: MaterialStateProperty.all(
  74. const StadiumBorder(
  75. side: BorderSide.none)))),
  76. ],
  77. ),
  78. ),
  79. ))),
  80. );
  81. });
  82. }
  83. }
  84. class _CampusListViewState extends State<CampusListView> {
  85. int select = -1;
  86. int _selectCampus = -1;
  87. @override
  88. Widget build(BuildContext context) {
  89. return Expanded(
  90. child: SingleChildScrollView(
  91. /// 折叠列表
  92. child: ChznExpansionPanelList(
  93. expansionCallback: (int index, bool isExpanded) {
  94. setState(() {
  95. if (!isExpanded) {
  96. if (select != -1) widget._campusList[select].isExpanded = false;
  97. select = index;
  98. widget._campusList[select].isExpanded = true;
  99. } else {
  100. widget._campusList[index].isExpanded = false;
  101. select = -1;
  102. }
  103. _selectCampus = select;
  104. });
  105. },
  106. children: widget._campusList.map<ExpansionPanel>((_CampusViewItem item) {
  107. return ExpansionPanel(
  108. headerBuilder: (BuildContext context, bool isExpanded) {
  109. return item.header;
  110. },
  111. body: item.body,
  112. isExpanded: item.isExpanded,
  113. canTapOnHeader: true,
  114. backgroundColor:
  115. item.isExpanded ? const Color(0xFFF0F0F0) : Colors.white);
  116. }).toList(),
  117. expandedHeaderPadding: const EdgeInsets.all(0),
  118. elevation: 0,
  119. )));
  120. }
  121. }
  122. class _CampusViewItem {
  123. Widget body;
  124. Widget header;
  125. bool isExpanded;
  126. int id;
  127. _CampusViewItem(
  128. {required this.body,
  129. required this.header,
  130. this.isExpanded = false,
  131. this.id = 0});
  132. }