node_list.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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: Container(
  183. height: 52.w,
  184. padding: EdgeInsets.symmetric(horizontal: 14.w),
  185. child: Row(
  186. children: [
  187. // 国旗图标
  188. ClipRRect(
  189. borderRadius: BorderRadius.circular(4.r), // 设置圆角
  190. child: SvgPicture.asset(
  191. Assets.getCountryFlagImage(code),
  192. width: 32.w,
  193. height: 24.w,
  194. fit: BoxFit.cover,
  195. // placeholderBuilder: (context) => Container(
  196. // width: 32.w,
  197. // height: 24.w,
  198. // decoration: BoxDecoration(
  199. // borderRadius: BorderRadius.circular(4.r),
  200. // color: Colors.grey[200],
  201. // ),
  202. // alignment: Alignment.center,
  203. // child: Icon(
  204. // Icons.flag,
  205. // size: 16.w,
  206. // color: Colors.grey[400],
  207. // ),
  208. // ),
  209. ),
  210. ),
  211. 10.horizontalSpace,
  212. Text(
  213. title,
  214. style: TextStyle(
  215. fontSize: 16.sp,
  216. height: 1.5,
  217. fontWeight: FontWeight.w500,
  218. color: Get.reactiveTheme.textTheme.bodyLarge!.color,
  219. ),
  220. ),
  221. const Spacer(),
  222. // 箭头图标
  223. AnimatedRotation(
  224. turns: expanded ? 0.25 : 0.0,
  225. duration: const Duration(milliseconds: 300),
  226. child: Icon(
  227. Icons.keyboard_arrow_right,
  228. size: 20.w,
  229. color: Get.reactiveTheme.hintColor,
  230. ),
  231. ),
  232. ],
  233. ),
  234. ),
  235. ),
  236. ClipRect(
  237. child: AnimatedAlign(
  238. duration: const Duration(milliseconds: 300),
  239. curve: Curves.easeInOut,
  240. heightFactor: expanded ? 1.0 : 0.0,
  241. alignment: Alignment.topLeft,
  242. child: AnimatedOpacity(
  243. opacity: expanded ? 1.0 : 0.0,
  244. duration: const Duration(milliseconds: 300),
  245. child: Column(
  246. children: cities.map((city) {
  247. final cityName = city as String;
  248. return ClickOpacity(
  249. onTap: () {},
  250. child: Column(
  251. children: [
  252. Divider(
  253. height: 1,
  254. color: Get.reactiveTheme.dividerColor,
  255. ),
  256. Container(
  257. alignment: Alignment.centerLeft,
  258. margin: EdgeInsets.symmetric(
  259. vertical: 12.h,
  260. horizontal: 14.w,
  261. ),
  262. child: Text(
  263. cityName,
  264. style: TextStyle(
  265. fontSize: 14.sp,
  266. fontWeight: FontWeight.w500,
  267. color: Get.reactiveTheme.hintColor,
  268. ),
  269. ),
  270. ),
  271. ],
  272. ),
  273. );
  274. }).toList(),
  275. ),
  276. ),
  277. ),
  278. ),
  279. ],
  280. ),
  281. );
  282. }
  283. }