CanteenMain.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. @override
  6. State<CanteenMain> createState() => CanteenMainState();
  7. }
  8. class CanteenMainState extends State<CanteenMain> {
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. appBar: AppBar(
  13. title: const Text("食堂菜谱"),
  14. leading: Builder(
  15. builder: (context) {
  16. return IconButton(
  17. icon: const Icon(Icons.arrow_back, color: Colors.white),
  18. onPressed: () {
  19. Navigator.pop(context);
  20. },
  21. );
  22. },
  23. ),
  24. ),
  25. body: Container(
  26. color: Colors.white,
  27. child: Column(
  28. children: [
  29. Row(children: [
  30. Expanded(
  31. child: Container(
  32. margin: const EdgeInsets.fromLTRB(15, 10, 15, 10),
  33. padding: const EdgeInsets.fromLTRB(15, 0, 0, 0),
  34. child: TextField(
  35. decoration: InputDecoration(
  36. hintText: "请输入查询内容",
  37. prefixIcon: Row(children: [
  38. Icon(Icons.search,
  39. color: Theme.of(context).primaryColor),
  40. Icon(Icons.keyboard_arrow_down,
  41. color: Theme.of(context).primaryColor)
  42. ]),
  43. border: InputBorder.none),
  44. focusNode: () {
  45. var focusNode = FocusNode();
  46. focusNode.addListener(() {
  47. if (focusNode.hasFocus) {
  48. focusNode.unfocus();
  49. //跳转至搜索页面
  50. }
  51. });
  52. return focusNode;
  53. }(),
  54. ),
  55. decoration: const BoxDecoration(
  56. color: Color(0xFFF0F0F0),
  57. borderRadius: BorderRadius.all(Radius.circular(1e18)),
  58. ),
  59. )),
  60. IconButton(
  61. icon: Icon(Icons.star_border,
  62. color: Theme.of(context).primaryColor),
  63. onPressed: () {
  64. //此处应当跳转至收藏页面
  65. Navigator.of(context).push(MaterialPageRoute(
  66. builder: (context){
  67. return listviewexpamle();
  68. }
  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(child:
  103. 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:
  118. widget._campusList.map<ExpansionPanel>((_CampusViewItem item) {
  119. return ExpansionPanel(
  120. headerBuilder: (BuildContext context, bool isExpanded) {
  121. return item.header;
  122. },
  123. body: item.body,
  124. isExpanded: item.isExpanded,
  125. canTapOnHeader: true,
  126. backgroundColor:
  127. item.isExpanded ? const Color(0xFFF0F0F0) : Colors.white);
  128. }).toList(),
  129. expandedHeaderPadding: const EdgeInsets.all(0),
  130. elevation: 0,
  131. )
  132. ));
  133. }
  134. }
  135. class _CampusViewItem {
  136. Widget body;
  137. Widget header;
  138. bool isExpanded;
  139. int id;
  140. _CampusViewItem(
  141. {required this.body,
  142. required this.header,
  143. this.isExpanded = false,
  144. this.id = 0});
  145. }
  146. List<_CampusViewItem> _createCampus(
  147. List<String> campus, List<String> campusImage) {
  148. return List.generate(campus.length, (index) {
  149. return _CampusViewItem(
  150. header: ListTile(
  151. leading: ClipOval(
  152. child: Image.asset(
  153. "assets/location_${campusImage[index]}.png",
  154. width: 30,
  155. height: 30,
  156. )),
  157. title: Text('${campus[index]}校区'),
  158. ),
  159. body: Container(
  160. color: Colors.white,
  161. child: Padding(
  162. padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
  163. child: Container(
  164. decoration: BoxDecoration(
  165. color: Colors.white,
  166. borderRadius: BorderRadius.circular(10.0),
  167. boxShadow: const [
  168. BoxShadow(color: Colors.black12, blurRadius: 8.0)
  169. ]),
  170. child: Center(
  171. child: Column(
  172. children: [
  173. ElevatedButton(
  174. onPressed: () {
  175. //跳转到相应界面 传参_selectCampus
  176. },
  177. child: const Text(" 当日菜品(固定) "), //这里对齐太难处理,我偷懒用全角空格对齐
  178. style: ButtonStyle(
  179. shape: MaterialStateProperty.all(
  180. const StadiumBorder(side: BorderSide.none)),
  181. ),
  182. ),
  183. ElevatedButton(
  184. onPressed: () {},
  185. child: const Text("   特色菜品   "),
  186. style: ButtonStyle(
  187. shape: MaterialStateProperty.all(
  188. const StadiumBorder(side: BorderSide.none)))),
  189. ElevatedButton(
  190. onPressed: () {},
  191. child: const Text("   当日新菜   "),
  192. style: ButtonStyle(
  193. shape: MaterialStateProperty.all(
  194. const StadiumBorder(side: BorderSide.none)))),
  195. ],
  196. ),
  197. ),
  198. ))),
  199. );
  200. });
  201. }