这是CaiYouHui前端,一个关于flutter的安卓app,前端使用flutter实现

profile_screen.dart 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import '../../../core/constants/route_constants.dart';
  4. import '../../providers/auth_provider.dart';
  5. import '../../navigation/bottom_nav_bar.dart';
  6. import '../../widgets/common/app_button.dart';
  7. import 'profile_detail_screen.dart';
  8. class ProfileScreen extends StatelessWidget {
  9. const ProfileScreen({super.key});
  10. @override
  11. Widget build(BuildContext context) {
  12. // 直接显示内容,认证由路由层处理
  13. return _ProfileContent();
  14. }
  15. }
  16. class _ProfileContent extends StatefulWidget {
  17. @override
  18. State<_ProfileContent> createState() => _ProfileContentState();
  19. }
  20. class _ProfileContentState extends State<_ProfileContent> {
  21. // @override
  22. // void initState() {
  23. // super.initState();
  24. // WidgetsBinding.instance.addPostFrameCallback((_) {
  25. // final userProvider = Provider.of<UserProvider>(context, listen: false);
  26. // userProvider.loadUserProfile();
  27. // });
  28. // }
  29. @override
  30. Widget build(BuildContext context) {
  31. final authProvider = Provider.of<AuthProvider>(context);
  32. return Scaffold(
  33. appBar: AppBar(
  34. title: const Text('我的'),
  35. actions: [
  36. IconButton(
  37. icon: const Icon(Icons.settings_outlined),
  38. onPressed: () {
  39. // 跳转到设置页面
  40. },
  41. ),
  42. ],
  43. ),
  44. body: SingleChildScrollView(
  45. child: Column(
  46. children: [
  47. if (!authProvider.isAuthenticated) ...[
  48. // 欢迎区域
  49. Card(
  50. margin: const EdgeInsets.all(16),
  51. shape: RoundedRectangleBorder(
  52. borderRadius: BorderRadius.circular(16),
  53. ),
  54. // color: Colors.blue[50],
  55. elevation: 2,
  56. child: Padding(
  57. padding: const EdgeInsets.all(20),
  58. child: Column(
  59. crossAxisAlignment: CrossAxisAlignment.start,
  60. children: [
  61. Row(
  62. children: [
  63. CircleAvatar(
  64. radius: 30,
  65. backgroundColor: Colors.blue[100],
  66. child: Icon(
  67. Icons.person,
  68. size: 30,
  69. color: Colors.blue,
  70. ),
  71. ),
  72. const SizedBox(width: 16),
  73. Expanded(
  74. child: Column(
  75. crossAxisAlignment: CrossAxisAlignment.start,
  76. children: [
  77. Text(
  78. authProvider.isAuthenticated
  79. ? '您好,${authProvider.user?.fullName}'
  80. : '您好,游客',
  81. style: const TextStyle(
  82. fontSize: 18,
  83. fontWeight: FontWeight.bold,
  84. ),
  85. ),
  86. const SizedBox(height: 4),
  87. Text(
  88. authProvider.isAuthenticated
  89. ? '欢迎回来!'
  90. : '请登录以使用完整功能',
  91. style: TextStyle(
  92. fontSize: 14,
  93. color: Colors.grey[600],
  94. ),
  95. ),
  96. ],
  97. ),
  98. ),
  99. ],
  100. ),
  101. const SizedBox(height: 20),
  102. if (!authProvider.isAuthenticated)
  103. AppButton(
  104. text: '立即登录',
  105. onPressed: () {
  106. Navigator.of(context).pushNamed('/login');
  107. },
  108. backgroundColor: Colors.blue,
  109. height: 45,
  110. ),
  111. ],
  112. ),
  113. ),
  114. ),
  115. ] else ...[
  116. // 用户信息卡片
  117. Card(
  118. margin: const EdgeInsets.all(16),
  119. shape: RoundedRectangleBorder(
  120. borderRadius: BorderRadius.circular(16),
  121. ),
  122. // color: Colors.blue[50],
  123. elevation: 2,
  124. child: Padding(
  125. padding: const EdgeInsets.all(20),
  126. child: Column(
  127. children: [
  128. GestureDetector(
  129. onTap: () {
  130. // 点击头像
  131. },
  132. child: Stack(
  133. children: [
  134. CircleAvatar(
  135. radius: 50,
  136. backgroundColor: Colors.blue[100],
  137. backgroundImage: authProvider.user?.avatar != null
  138. ? NetworkImage(authProvider.user!.avatar!)
  139. : null,
  140. child: authProvider.user?.avatar == null
  141. ? const Icon(
  142. Icons.person,
  143. size: 60,
  144. color: Colors.blue,
  145. )
  146. : null,
  147. ),
  148. Positioned(
  149. bottom: 0,
  150. right: 0,
  151. child: Container(
  152. padding: const EdgeInsets.all(6),
  153. decoration: BoxDecoration(
  154. color: Colors.blue,
  155. borderRadius: BorderRadius.circular(20),
  156. border: Border.all(
  157. color: Colors.white,
  158. width: 2,
  159. ),
  160. ),
  161. ),
  162. ),
  163. ],
  164. ),
  165. ),
  166. const SizedBox(height: 16),
  167. Text(
  168. authProvider.user?.fullName ?? '用户',
  169. style: const TextStyle(
  170. fontSize: 22,
  171. fontWeight: FontWeight.bold,
  172. ),
  173. ),
  174. const SizedBox(height: 8),
  175. Text(
  176. authProvider.user?.email ?? '',
  177. style: TextStyle(
  178. fontSize: 14,
  179. color: Colors.grey[600],
  180. ),
  181. ),
  182. if (authProvider.user?.phone != null)
  183. Padding(
  184. padding: const EdgeInsets.only(top: 4),
  185. child: Text(
  186. authProvider.user!.phone!,
  187. style: TextStyle(
  188. fontSize: 14,
  189. color: Colors.grey[600],
  190. ),
  191. ),
  192. ),
  193. const SizedBox(height: 16),
  194. Row(
  195. mainAxisAlignment: MainAxisAlignment.center,
  196. children: [
  197. _buildStatItem('关注', '0'),
  198. _buildVerticalDivider(),
  199. _buildStatItem('粉丝', '0'),
  200. _buildVerticalDivider(),
  201. _buildStatItem('积分', '0'),
  202. ],
  203. ),
  204. ],
  205. ),
  206. ),
  207. ),
  208. ],
  209. // 菜单项
  210. Padding(
  211. padding: const EdgeInsets.symmetric(horizontal: 16),
  212. child: Column(
  213. children: [
  214. if (authProvider.isAuthenticated) ... [
  215. _buildMenuCard(
  216. title: '账户设置',
  217. items: [
  218. _buildMenuItem(
  219. icon: Icons.person_outline,
  220. title: '个人信息',
  221. subtitle: '查看和编辑个人信息',
  222. onTap: () {
  223. Navigator.of(context).push(
  224. MaterialPageRoute(
  225. builder: (_) => const ProfileDetailScreen(),
  226. ),
  227. );
  228. },
  229. ),
  230. _buildMenuItem(
  231. icon: Icons.lock_outline,
  232. title: '账号安全',
  233. subtitle: '修改密码和安全设置',
  234. onTap: () {},
  235. ),
  236. _buildMenuItem(
  237. icon: Icons.notifications_none,
  238. title: '消息通知',
  239. subtitle: '管理通知偏好设置',
  240. onTap: () {},
  241. ),
  242. ],
  243. ),
  244. const SizedBox(height: 16),
  245. _buildMenuCard(
  246. title: '我的内容',
  247. items: [
  248. _buildMenuItem(
  249. icon: Icons.bookmark_border,
  250. title: '我的收藏',
  251. subtitle: '查看收藏的内容',
  252. onTap: () {},
  253. ),
  254. _buildMenuItem(
  255. icon: Icons.history,
  256. title: '浏览历史',
  257. subtitle: '查看最近浏览记录',
  258. onTap: () {},
  259. ),
  260. _buildMenuItem(
  261. icon: Icons.download,
  262. title: '我的下载',
  263. subtitle: '管理下载的文件',
  264. onTap: () {},
  265. ),
  266. ],
  267. ),
  268. const SizedBox(height: 16),
  269. ],
  270. _buildMenuCard(
  271. title: '其他',
  272. items: [
  273. _buildMenuItem(
  274. icon: Icons.help_outline,
  275. title: '帮助中心',
  276. subtitle: '常见问题和帮助文档',
  277. onTap: () {},
  278. ),
  279. _buildMenuItem(
  280. icon: Icons.info_outline,
  281. title: '关于我们',
  282. subtitle: '了解应用信息',
  283. onTap: () {},
  284. ),
  285. if (authProvider.isAuthenticated) ... [
  286. _buildMenuItem(
  287. icon: Icons.logout,
  288. title: '退出登录',
  289. subtitle: '安全退出当前账号',
  290. onTap: () async {
  291. // 防止重复点击
  292. if (authProvider.isLoading) return;
  293. final shouldLogout = await showDialog<bool>(
  294. context: context,
  295. builder: (context) {
  296. final isIOS = Theme.of(context).platform == TargetPlatform.iOS;
  297. if (isIOS) {
  298. // iOS风格:确定在右边,取消在左边
  299. return AlertDialog(
  300. title: const Text('确认退出'),
  301. content: const Text('确定要退出登录吗?'),
  302. actionsAlignment: MainAxisAlignment.spaceBetween,
  303. actions: [
  304. TextButton(
  305. onPressed: () => Navigator.of(context).pop(true),
  306. child: const Text('确定'),
  307. ),
  308. TextButton(
  309. onPressed: () => Navigator.of(context).pop(false),
  310. child: const Text('取消'),
  311. ),
  312. ],
  313. );
  314. } else {
  315. // Android/Material风格:取消在左边,确定在右边
  316. return AlertDialog(
  317. title: const Text('确认退出'),
  318. content: const Text('确定要退出登录吗?'),
  319. actionsAlignment: MainAxisAlignment.spaceBetween,
  320. actions: [
  321. TextButton(
  322. onPressed: () => Navigator.of(context).pop(false),
  323. child: const Text('取消'),
  324. ),
  325. TextButton(
  326. onPressed: () => Navigator.of(context).pop(true),
  327. child: const Text('确定'),
  328. ),
  329. ],
  330. );
  331. }
  332. },
  333. );
  334. if (shouldLogout == true) {
  335. await authProvider.logout();
  336. // 登出后,确保在组件仍然存在时进行导航
  337. if (mounted) {
  338. // 使用pushReplacementNamed清空导航栈
  339. Navigator.of(context).pushReplacementNamed(RouteConstants.home);
  340. }
  341. }
  342. },
  343. ),
  344. ],
  345. ],
  346. ),
  347. const SizedBox(height: 20),
  348. // 版本信息
  349. Padding(
  350. padding: const EdgeInsets.symmetric(vertical: 20),
  351. child: Text(
  352. '版本 v1.0.0',
  353. style: TextStyle(
  354. fontSize: 12,
  355. color: Colors.grey[500],
  356. ),
  357. ),
  358. ),
  359. ],
  360. ),
  361. ),
  362. ],
  363. ),
  364. ),
  365. bottomNavigationBar: const BottomNavBar(initialIndex: 3),
  366. );
  367. }
  368. Widget _buildStatItem(String label, String value) {
  369. return Expanded(
  370. child: Column(
  371. children: [
  372. Text(
  373. value,
  374. style: const TextStyle(
  375. fontSize: 18,
  376. fontWeight: FontWeight.bold,
  377. ),
  378. ),
  379. const SizedBox(height: 4),
  380. Text(
  381. label,
  382. style: TextStyle(
  383. fontSize: 12,
  384. color: Colors.grey[600],
  385. ),
  386. ),
  387. ],
  388. ),
  389. );
  390. }
  391. Widget _buildVerticalDivider() {
  392. return Container(
  393. width: 1,
  394. height: 40,
  395. color: Colors.grey[300],
  396. margin: const EdgeInsets.symmetric(horizontal: 16),
  397. );
  398. }
  399. Widget _buildMenuCard({
  400. required String title,
  401. required List<Widget> items,
  402. }) {
  403. return Card(
  404. elevation: 2,
  405. shape: RoundedRectangleBorder(
  406. borderRadius: BorderRadius.circular(12),
  407. ),
  408. child: Padding(
  409. padding: const EdgeInsets.all(16),
  410. child: Column(
  411. crossAxisAlignment: CrossAxisAlignment.start,
  412. children: [
  413. Text(
  414. title,
  415. style: const TextStyle(
  416. fontSize: 16,
  417. fontWeight: FontWeight.bold,
  418. ),
  419. ),
  420. const SizedBox(height: 12),
  421. Column(children: items),
  422. ],
  423. ),
  424. ),
  425. );
  426. }
  427. Widget _buildMenuItem({
  428. required IconData icon,
  429. required String title,
  430. required String subtitle,
  431. required VoidCallback onTap,
  432. }) {
  433. return ListTile(
  434. contentPadding: EdgeInsets.zero,
  435. leading: Icon(icon),
  436. title: Text(title),
  437. subtitle: Text(subtitle),
  438. trailing: const Icon(Icons.chevron_right),
  439. onTap: onTap,
  440. );
  441. }
  442. }