|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:luckincoffee/extensions/int-extensions.dart'; |
| 3 | +import 'package:luckincoffee/utils/icon.dart'; |
| 4 | + |
| 5 | +import 'home-swiper.dart'; |
| 6 | + |
| 7 | +final List<SelectItemViewModel> selects = [ |
| 8 | + SelectItemViewModel( |
| 9 | + title: '立即下单', |
| 10 | + subtitle: 'ORDER NOW', |
| 11 | + iconData: IconFonts.icontupian3, |
| 12 | + ), |
| 13 | + SelectItemViewModel( |
| 14 | + title: '咖啡钱包', |
| 15 | + subtitle: 'COFFEE WALLET', |
| 16 | + iconData: IconFonts.icontupian, |
| 17 | + ), |
| 18 | + SelectItemViewModel( |
| 19 | + title: '送Ta咖啡', |
| 20 | + subtitle: 'SEND COFFEE', |
| 21 | + iconData: IconFonts.icontupian1, |
| 22 | + ), |
| 23 | + SelectItemViewModel( |
| 24 | + title: '企业账户', |
| 25 | + subtitle: 'ENTERPRISE ACCOUNT', |
| 26 | + iconData: IconFonts.icontupian7, |
| 27 | + ), |
| 28 | +]; |
| 29 | + |
| 30 | +class HomeContent extends StatefulWidget { |
| 31 | + @override |
| 32 | + _HomeContentState createState() => _HomeContentState(); |
| 33 | +} |
| 34 | + |
| 35 | +class _HomeContentState extends State<HomeContent> { |
| 36 | + /// 购买类型 |
| 37 | + /// 1:自提 |
| 38 | + /// 2:外送 |
| 39 | + int type = 0; |
| 40 | + |
| 41 | + @override |
| 42 | + Widget build(BuildContext context) { |
| 43 | + return Column(children: [ |
| 44 | + HomeSwiper(), |
| 45 | + Container( |
| 46 | + padding: EdgeInsets.symmetric(horizontal: 20.px), |
| 47 | + child: Column( |
| 48 | + mainAxisSize: MainAxisSize.min, |
| 49 | + children: [ |
| 50 | + ListTile( |
| 51 | + contentPadding: EdgeInsets.zero, |
| 52 | + title: Text('青年汇店'), |
| 53 | + subtitle: Text('距你53m'), |
| 54 | + trailing: buildTrailing(context), |
| 55 | + ), |
| 56 | + buildSelects(), |
| 57 | + buildFooterImg(), |
| 58 | + ], |
| 59 | + )), |
| 60 | + ]); |
| 61 | + } |
| 62 | + |
| 63 | + /// 渲染右侧购买类型 |
| 64 | + Widget buildTrailing(BuildContext context) { |
| 65 | + /// 修改购买类型 |
| 66 | + void handleTypeChange() { |
| 67 | + setState(() { |
| 68 | + type = type == 0 ? 1 : 0; |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + return Container( |
| 73 | + width: 90.px, |
| 74 | + height: 36.px, |
| 75 | + padding: EdgeInsets.all(2.px), |
| 76 | + decoration: BoxDecoration( |
| 77 | + border: Border.all( |
| 78 | + color: Theme.of(context).primaryColor, |
| 79 | + width: 1.0, |
| 80 | + ), |
| 81 | + borderRadius: BorderRadius.circular(100.px), |
| 82 | + ), |
| 83 | + child: Row( |
| 84 | + mainAxisSize: MainAxisSize.max, |
| 85 | + mainAxisAlignment: MainAxisAlignment.spaceAround, |
| 86 | + children: [ |
| 87 | + buildTrailingItem(context: context, title: '自提', isActive: type == 0, onTap: handleTypeChange), |
| 88 | + buildTrailingItem(context: context, title: '外送', isActive: type == 1, onTap: handleTypeChange), |
| 89 | + ], |
| 90 | + ), |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /// 渲染购买类型 |
| 95 | + Widget buildTrailingItem({required BuildContext context, required String title, required bool isActive, required Function onTap}) { |
| 96 | + return Expanded( |
| 97 | + child: GestureDetector( |
| 98 | + onTap: () { |
| 99 | + onTap(); |
| 100 | + }, |
| 101 | + child: Container( |
| 102 | + height: double.infinity, |
| 103 | + decoration: BoxDecoration(borderRadius: BorderRadius.circular(100.px), color: isActive ? Theme.of(context).primaryColor : Colors.transparent), |
| 104 | + alignment: Alignment(0, 0), |
| 105 | + child: Text(title, style: Theme.of(context).textTheme.headline1?.copyWith(color: isActive ? Colors.white : Colors.black87)), |
| 106 | + ), |
| 107 | + ), |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + Widget buildSelects() { |
| 112 | + return Container( |
| 113 | + child: ListView.separated( |
| 114 | + padding: EdgeInsets.zero, |
| 115 | + shrinkWrap: true, |
| 116 | + physics: ScrollPhysics(), |
| 117 | + itemCount: selects.length, |
| 118 | + itemBuilder: (ctx, index) { |
| 119 | + return buildSelectsItem(title: selects[index].title, subtitle: selects[index].subtitle, iconData: selects[index].iconData); |
| 120 | + }, |
| 121 | + separatorBuilder: (ctx, index) => Divider(), |
| 122 | + ), |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + /// 渲染底部选择菜单 |
| 127 | + Widget buildSelectsItem({ |
| 128 | + required String title, |
| 129 | + required String subtitle, |
| 130 | + required IconData iconData, |
| 131 | + }) { |
| 132 | + return ListTile( |
| 133 | + contentPadding: EdgeInsets.zero, |
| 134 | + title: Text( |
| 135 | + title, |
| 136 | + style: TextStyle(fontWeight: FontWeight.bold), |
| 137 | + ), |
| 138 | + subtitle: Text( |
| 139 | + subtitle, |
| 140 | + ), |
| 141 | + trailing: CircleAvatar( |
| 142 | + backgroundColor: Colors.transparent, |
| 143 | + child: Container( |
| 144 | + width: double.infinity, |
| 145 | + height: double.infinity, |
| 146 | + decoration: BoxDecoration(border: Border.all(), borderRadius: BorderRadius.circular(100)), |
| 147 | + child: Icon( |
| 148 | + iconData, |
| 149 | + color: Color(0xff64443c), |
| 150 | + ))), |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + /// 渲染底部图片 |
| 155 | + Image buildFooterImg() { |
| 156 | + return Image.asset( |
| 157 | + 'lib/assets/images/home/footer-img.png', |
| 158 | + fit: BoxFit.cover, |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +/// 菜单 item 数据模型 |
| 164 | +class SelectItemViewModel { |
| 165 | + late final String title; |
| 166 | + late final String subtitle; |
| 167 | + late final IconData iconData; |
| 168 | + |
| 169 | + SelectItemViewModel({required this.title, required this.subtitle, required this.iconData}); |
| 170 | +} |
0 commit comments