home_page.dart 7.1 KB

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