node_list.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:flutter_sticky_header/flutter_sticky_header.dart';
  4. import 'package:flutter_svg/flutter_svg.dart';
  5. import 'package:get/get.dart';
  6. import 'package:nomo/app/widgets/click_opacity.dart';
  7. import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';
  8. import '../../../constants/assets.dart';
  9. import '../controllers/node_controller.dart';
  10. class NodeList extends StatefulWidget {
  11. final int tabIndex;
  12. const NodeList({super.key, required this.tabIndex});
  13. @override
  14. State<NodeList> createState() {
  15. return _NodeListState();
  16. }
  17. }
  18. class _NodeListState extends State<NodeList>
  19. with AutomaticKeepAliveClientMixin {
  20. @override
  21. bool get wantKeepAlive => true;
  22. // 获取 Controller
  23. NodeController get controller => Get.find<NodeController>();
  24. /// 获取国家的展开状态
  25. bool _getExpandedState(String countryCode) {
  26. return controller.getExpandedState(widget.tabIndex, countryCode);
  27. }
  28. /// 设置国家的展开状态
  29. void _setExpandedState(String countryCode, bool expanded) {
  30. setState(() {
  31. controller.setExpandedState(widget.tabIndex, countryCode, expanded);
  32. });
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. super.build(context);
  37. final data = {
  38. 'Europe': [
  39. {
  40. 'title': 'United Kingdom',
  41. 'code': 'GB',
  42. 'cities': [
  43. 'London',
  44. 'Edinburgh',
  45. 'Cardiff',
  46. 'Liverpool',
  47. 'Manchester',
  48. ],
  49. },
  50. {
  51. 'title': 'Italy',
  52. 'code': 'IT',
  53. 'cities': ['Rome', 'Milan', 'Naples', 'Turin', 'Genoa'],
  54. },
  55. {
  56. 'title': 'Germany',
  57. 'code': 'DE',
  58. 'cities': ['Berlin', 'Hamburg', 'Munich', 'Frankfurt', 'Cologne'],
  59. },
  60. ],
  61. 'Asia': [
  62. {
  63. 'title': 'China',
  64. 'code': 'CN',
  65. 'cities': ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Chengdu'],
  66. },
  67. {
  68. 'title': 'Japan',
  69. 'code': 'JP',
  70. 'cities': ['Tokyo', 'Osaka', 'Nagoya', 'Sapporo', 'Fukuoka'],
  71. },
  72. {
  73. 'title': 'Korea',
  74. 'code': 'KR',
  75. 'cities': ['Seoul', 'Busan', 'Incheon', 'Daegu', 'Gwangju'],
  76. },
  77. {
  78. 'title': 'India',
  79. 'code': 'IN',
  80. 'cities': ['Mumbai', 'Delhi', 'Bangalore', 'Chennai', 'Hyderabad'],
  81. },
  82. ],
  83. 'America': [
  84. {
  85. 'title': 'United States',
  86. 'code': 'US',
  87. 'cities': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami'],
  88. },
  89. {
  90. 'title': 'Canada',
  91. 'code': 'CA',
  92. 'cities': ['Toronto', 'Montreal', 'Vancouver', 'Calgary', 'Edmonton'],
  93. },
  94. {
  95. 'title': 'Mexico',
  96. 'code': 'MX',
  97. 'cities': [
  98. 'Mexico City',
  99. 'Guadalajara',
  100. 'Monterrey',
  101. 'Tijuana',
  102. 'Puebla',
  103. ],
  104. },
  105. {
  106. 'title': 'Brazil',
  107. 'code': 'BR',
  108. 'cities': [
  109. 'Sao Paulo',
  110. 'Rio de Janeiro',
  111. 'Brasilia',
  112. 'Salvador',
  113. 'Fortaleza',
  114. ],
  115. },
  116. ],
  117. };
  118. return CustomScrollView(
  119. slivers: [
  120. for (var region in data.entries)
  121. SliverStickyHeader(
  122. header: Container(
  123. color: Get.reactiveTheme.scaffoldBackgroundColor,
  124. padding: const EdgeInsets.all(16),
  125. child: Text(
  126. region.key,
  127. style: TextStyle(
  128. color: Get.reactiveTheme.hintColor,
  129. fontSize: 16,
  130. fontWeight: FontWeight.bold,
  131. ),
  132. ),
  133. ),
  134. sliver: SliverList(
  135. delegate: SliverChildBuilderDelegate((context, i) {
  136. final country = region.value[i];
  137. final countryCode = country['code'] as String;
  138. final cities = country['cities'] as List<dynamic>;
  139. return _CountrySection(
  140. title: country['title'] as String,
  141. code: countryCode,
  142. cities: cities,
  143. // 传递展开状态
  144. expanded: _getExpandedState(countryCode),
  145. // 展开状态变化回调
  146. onExpandedChanged: (expanded) {
  147. _setExpandedState(countryCode, expanded);
  148. },
  149. );
  150. }, childCount: region.value.length),
  151. ),
  152. ),
  153. ],
  154. );
  155. }
  156. }
  157. class _CountrySection extends StatelessWidget {
  158. final String title;
  159. final String code;
  160. final List<dynamic> cities;
  161. final bool expanded;
  162. final ValueChanged<bool> onExpandedChanged;
  163. const _CountrySection({
  164. required this.title,
  165. required this.code,
  166. required this.cities,
  167. required this.expanded,
  168. required this.onExpandedChanged,
  169. });
  170. @override
  171. Widget build(BuildContext context) {
  172. return Container(
  173. margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
  174. decoration: BoxDecoration(
  175. color: Get.reactiveTheme.highlightColor,
  176. borderRadius: BorderRadius.circular(12),
  177. ),
  178. child: Column(
  179. children: [
  180. ClickOpacity(
  181. onTap: () => onExpandedChanged(!expanded),
  182. child: Padding(
  183. padding: EdgeInsets.all(14.w),
  184. child: Row(
  185. children: [
  186. // 国旗图标
  187. ClipRRect(
  188. borderRadius: BorderRadius.circular(4.r), // 设置圆角
  189. child: SvgPicture.asset(
  190. Assets.getCountryFlagImage(code),
  191. width: 32.w,
  192. height: 24.w,
  193. fit: BoxFit.cover,
  194. // placeholderBuilder: (context) => Container(
  195. // width: 32.w,
  196. // height: 24.w,
  197. // decoration: BoxDecoration(
  198. // borderRadius: BorderRadius.circular(4.r),
  199. // color: Colors.grey[200],
  200. // ),
  201. // alignment: Alignment.center,
  202. // child: Icon(
  203. // Icons.flag,
  204. // size: 16.w,
  205. // color: Colors.grey[400],
  206. // ),
  207. // ),
  208. ),
  209. ),
  210. 10.horizontalSpace,
  211. Text(
  212. title,
  213. style: TextStyle(
  214. fontSize: 16.sp,
  215. height: 1.5,
  216. fontWeight: FontWeight.w500,
  217. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  218. ),
  219. ),
  220. const Spacer(),
  221. // 箭头图标
  222. AnimatedRotation(
  223. turns: expanded ? 0.25 : 0.0,
  224. duration: const Duration(milliseconds: 300),
  225. child: Icon(
  226. Icons.keyboard_arrow_right,
  227. size: 20.w,
  228. color: Get.reactiveTheme.hintColor,
  229. ),
  230. ),
  231. ],
  232. ),
  233. ),
  234. ),
  235. ClipRect(
  236. child: AnimatedAlign(
  237. duration: const Duration(milliseconds: 300),
  238. curve: Curves.easeInOut,
  239. heightFactor: expanded ? 1.0 : 0.0,
  240. alignment: Alignment.topLeft,
  241. child: AnimatedOpacity(
  242. opacity: expanded ? 1.0 : 0.0,
  243. duration: const Duration(milliseconds: 300),
  244. child: Column(
  245. children: cities.map((city) {
  246. final cityName = city as String;
  247. return ClickOpacity(
  248. onTap: () {},
  249. child: Column(
  250. children: [
  251. Divider(
  252. height: 1,
  253. color: Get.reactiveTheme.dividerColor,
  254. ),
  255. Container(
  256. alignment: Alignment.centerLeft,
  257. margin: EdgeInsets.only(
  258. top: 14.w,
  259. bottom: 14.w,
  260. left: 24.w,
  261. right: 14.w,
  262. ),
  263. child: Row(
  264. children: [
  265. ClipRRect(
  266. borderRadius: BorderRadius.circular(
  267. 4.r,
  268. ), // 设置圆角
  269. child: SvgPicture.asset(
  270. Assets.getCountryFlagImage(code),
  271. width: 32.w,
  272. height: 24.w,
  273. fit: BoxFit.cover,
  274. // placeholderBuilder: (context) => Container(
  275. // width: 32.w,
  276. // height: 24.w,
  277. // decoration: BoxDecoration(
  278. // borderRadius: BorderRadius.circular(4.r),
  279. // color: Colors.grey[200],
  280. // ),
  281. // alignment: Alignment.center,
  282. // child: Icon(
  283. // Icons.flag,
  284. // size: 16.w,
  285. // color: Colors.grey[400],
  286. // ),
  287. // ),
  288. ),
  289. ),
  290. 10.horizontalSpace,
  291. Text(
  292. cityName,
  293. style: TextStyle(
  294. fontSize: 14.sp,
  295. fontWeight: FontWeight.w500,
  296. color: Get.reactiveTheme.hintColor,
  297. ),
  298. ),
  299. ],
  300. ),
  301. ),
  302. ],
  303. ),
  304. );
  305. }).toList(),
  306. ),
  307. ),
  308. ),
  309. ),
  310. ],
  311. ),
  312. );
  313. }
  314. }