CanteenMain.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import 'package:canteen/ui/listviewexample.dart';
  2. import 'package:canteen/widget/ChznExpansionPanelList.dart';
  3. import 'package:flutter/material.dart';
  4. class CanteenMain extends StatefulWidget {
  5. const CanteenMain({Key? key}) : super(key: key);
  6. @override
  7. State<CanteenMain> createState() => CanteenMainState();
  8. }
  9. class CanteenMainState extends State<CanteenMain> {
  10. @override
  11. Widget build(BuildContext context) {
  12. return Scaffold(
  13. appBar: AppBar(
  14. title: const Text("食堂菜谱"),
  15. leading: Builder(
  16. builder: (context) {
  17. return IconButton(
  18. icon: const Icon(Icons.arrow_back, color: Colors.white),
  19. onPressed: () {
  20. Navigator.pop(context);
  21. },
  22. );
  23. },
  24. ),
  25. ),
  26. body: Container(
  27. color: Colors.white,
  28. child: Column(
  29. children: [
  30. Row(children: [
  31. Expanded(
  32. child: Container(
  33. margin: const EdgeInsets.fromLTRB(15, 10, 15, 10),
  34. padding: const EdgeInsets.fromLTRB(15, 0, 0, 0),
  35. child: TextField(
  36. decoration: InputDecoration(
  37. hintText: "请输入查询内容",
  38. prefixIcon: Row(children: [
  39. Icon(Icons.search,
  40. color: Theme.of(context).primaryColor),
  41. Icon(Icons.keyboard_arrow_down,
  42. color: Theme.of(context).primaryColor)
  43. ]),
  44. border: InputBorder.none),
  45. focusNode: () {
  46. var focusNode = FocusNode();
  47. focusNode.addListener(() {
  48. if (focusNode.hasFocus) {
  49. focusNode.unfocus();
  50. //跳转至搜索页面
  51. }
  52. });
  53. return focusNode;
  54. }(),
  55. ),
  56. decoration: const BoxDecoration(
  57. color: Color(0xFFF0F0F0),
  58. borderRadius: BorderRadius.all(Radius.circular(1e18)),
  59. ),
  60. )),
  61. IconButton(
  62. icon: Icon(Icons.star_border,
  63. color: Theme.of(context).primaryColor),
  64. onPressed: () {
  65. //此处应当跳转至收藏页面
  66. Navigator.of(context)
  67. .push(MaterialPageRoute(builder: (context) {
  68. return listviewexpamle();
  69. }));
  70. },
  71. )
  72. ]),
  73. _CampusListView()
  74. ],
  75. ),
  76. ),
  77. );
  78. }
  79. }
  80. class _CampusListView extends StatefulWidget {
  81. static const campus = ["中心", "软件园", "洪家楼", "趵突泉", "千佛山", "兴隆山", "青岛", "威海"];
  82. static const campusImage = [
  83. "zhongxin",
  84. "ruanjian",
  85. "honglou",
  86. "baottu",
  87. "qianfo",
  88. "xinglong",
  89. "qianfo",
  90. "zhongxin"
  91. ];
  92. final _campusList = _createCampus(campus, campusImage);
  93. @override
  94. State<_CampusListView> createState() => _CampusListViewState();
  95. }
  96. int _selectCampus = -1;
  97. class _CampusListViewState extends State<_CampusListView> {
  98. int select = -1;
  99. @override
  100. Widget build(BuildContext context) {
  101. return Expanded(
  102. child: SingleChildScrollView(
  103. child: ChznExpansionPanelList(
  104. expansionCallback: (int index, bool isExpanded) {
  105. setState(() {
  106. if (!isExpanded) {
  107. if (select != -1) widget._campusList[select].isExpanded = false;
  108. select = index;
  109. widget._campusList[select].isExpanded = true;
  110. } else {
  111. widget._campusList[index].isExpanded = false;
  112. select = -1;
  113. }
  114. _selectCampus = select;
  115. });
  116. },
  117. children: widget._campusList.map<ExpansionPanel>((_CampusViewItem item) {
  118. return ExpansionPanel(
  119. headerBuilder: (BuildContext context, bool isExpanded) {
  120. return item.header;
  121. },
  122. body: item.body,
  123. isExpanded: item.isExpanded,
  124. canTapOnHeader: true,
  125. backgroundColor:
  126. item.isExpanded ? const Color(0xFFF0F0F0) : Colors.white);
  127. }).toList(),
  128. expandedHeaderPadding: const EdgeInsets.all(0),
  129. elevation: 0,
  130. )));
  131. }
  132. }
  133. class _CampusViewItem {
  134. Widget body;
  135. Widget header;
  136. bool isExpanded;
  137. int id;
  138. _CampusViewItem(
  139. {required this.body,
  140. required this.header,
  141. this.isExpanded = false,
  142. this.id = 0});
  143. }
  144. List<_CampusViewItem> _createCampus(
  145. List<String> campus, List<String> campusImage) {
  146. return List.generate(campus.length, (index) {
  147. return _CampusViewItem(
  148. header: ListTile(
  149. leading: ClipOval(
  150. child: Image.asset(
  151. "assets/location_${campusImage[index]}.png",
  152. width: 30,
  153. height: 30,
  154. )),
  155. title: Text('${campus[index]}校区'),
  156. ),
  157. body: Container(
  158. color: Colors.white,
  159. child: Padding(
  160. padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
  161. child: Container(
  162. decoration: BoxDecoration(
  163. color: Colors.white,
  164. borderRadius: BorderRadius.circular(10.0),
  165. boxShadow: const [
  166. BoxShadow(color: Colors.black12, blurRadius: 8.0)
  167. ]),
  168. child: Center(
  169. child: Column(
  170. children: [
  171. ElevatedButton(
  172. onPressed: () {
  173. //跳转到相应界面 传参_selectCampus
  174. },
  175. child: const Text(" 当日菜品(固定) "), //这里对齐太难处理,我偷懒用全角空格对齐
  176. style: ButtonStyle(
  177. shape: MaterialStateProperty.all(
  178. const StadiumBorder(side: BorderSide.none)),
  179. ),
  180. ),
  181. ElevatedButton(
  182. onPressed: () {},
  183. child: const Text("   特色菜品   "),
  184. style: ButtonStyle(
  185. shape: MaterialStateProperty.all(
  186. const StadiumBorder(side: BorderSide.none)))),
  187. ElevatedButton(
  188. onPressed: () {},
  189. child: const Text("   当日新菜   "),
  190. style: ButtonStyle(
  191. shape: MaterialStateProperty.all(
  192. const StadiumBorder(side: BorderSide.none)))),
  193. ],
  194. ),
  195. ),
  196. ))),
  197. );
  198. });
  199. }