| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import '../../../core/constants/route_constants.dart';
- import '../../providers/auth_provider.dart';
- import '../../navigation/bottom_nav_bar.dart';
- import '../../widgets/common/app_button.dart';
- import 'profile_detail_screen.dart';
-
- class ProfileScreen extends StatelessWidget {
- const ProfileScreen({super.key});
-
- @override
- Widget build(BuildContext context) {
- // 直接显示内容,认证由路由层处理
- return _ProfileContent();
- }
- }
-
- class _ProfileContent extends StatefulWidget {
- @override
- State<_ProfileContent> createState() => _ProfileContentState();
- }
-
- class _ProfileContentState extends State<_ProfileContent> {
- // @override
- // void initState() {
- // super.initState();
- // WidgetsBinding.instance.addPostFrameCallback((_) {
- // final userProvider = Provider.of<UserProvider>(context, listen: false);
- // userProvider.loadUserProfile();
- // });
- // }
-
- @override
- Widget build(BuildContext context) {
- final authProvider = Provider.of<AuthProvider>(context);
-
- return Scaffold(
- appBar: AppBar(
- title: const Text('我的'),
- actions: [
- IconButton(
- icon: const Icon(Icons.settings_outlined),
- onPressed: () {
- // 跳转到设置页面
- },
- ),
- ],
- ),
- body: SingleChildScrollView(
- child: Column(
- children: [
- if (!authProvider.isAuthenticated) ...[
- // 欢迎区域
- Card(
- margin: const EdgeInsets.all(16),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(16),
- ),
- // color: Colors.blue[50],
- elevation: 2,
- child: Padding(
- padding: const EdgeInsets.all(20),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- children: [
- CircleAvatar(
- radius: 30,
- backgroundColor: Colors.blue[100],
- child: Icon(
- Icons.person,
- size: 30,
- color: Colors.blue,
- ),
- ),
- const SizedBox(width: 16),
- Expanded(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- authProvider.isAuthenticated
- ? '您好,${authProvider.user?.fullName}'
- : '您好,游客',
- style: const TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.bold,
- ),
- ),
- const SizedBox(height: 4),
- Text(
- authProvider.isAuthenticated
- ? '欢迎回来!'
- : '请登录以使用完整功能',
- style: TextStyle(
- fontSize: 14,
- color: Colors.grey[600],
- ),
- ),
- ],
- ),
- ),
- ],
- ),
- const SizedBox(height: 20),
- if (!authProvider.isAuthenticated)
- AppButton(
- text: '立即登录',
- onPressed: () {
- Navigator.of(context).pushNamed('/login');
- },
- backgroundColor: Colors.blue,
- height: 45,
- ),
- ],
- ),
- ),
- ),
- ] else ...[
- // 用户信息卡片
- Card(
- margin: const EdgeInsets.all(16),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(16),
- ),
- // color: Colors.blue[50],
- elevation: 2,
- child: Padding(
- padding: const EdgeInsets.all(20),
- child: Column(
- children: [
- GestureDetector(
- onTap: () {
- // 点击头像
- },
- child: Stack(
- children: [
- CircleAvatar(
- radius: 50,
- backgroundColor: Colors.blue[100],
- backgroundImage: authProvider.user?.avatar != null
- ? NetworkImage(authProvider.user!.avatar!)
- : null,
- child: authProvider.user?.avatar == null
- ? const Icon(
- Icons.person,
- size: 60,
- color: Colors.blue,
- )
- : null,
- ),
- Positioned(
- bottom: 0,
- right: 0,
- child: Container(
- padding: const EdgeInsets.all(6),
- decoration: BoxDecoration(
- color: Colors.blue,
- borderRadius: BorderRadius.circular(20),
- border: Border.all(
- color: Colors.white,
- width: 2,
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- const SizedBox(height: 16),
- Text(
- authProvider.user?.fullName ?? '用户',
- style: const TextStyle(
- fontSize: 22,
- fontWeight: FontWeight.bold,
- ),
- ),
- const SizedBox(height: 8),
- Text(
- authProvider.user?.email ?? '',
- style: TextStyle(
- fontSize: 14,
- color: Colors.grey[600],
- ),
- ),
- if (authProvider.user?.phone != null)
- Padding(
- padding: const EdgeInsets.only(top: 4),
- child: Text(
- authProvider.user!.phone!,
- style: TextStyle(
- fontSize: 14,
- color: Colors.grey[600],
- ),
- ),
- ),
- const SizedBox(height: 16),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- _buildStatItem('关注', '0'),
- _buildVerticalDivider(),
- _buildStatItem('粉丝', '0'),
- _buildVerticalDivider(),
- _buildStatItem('积分', '0'),
- ],
- ),
- ],
- ),
- ),
- ),
- ],
- // 菜单项
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16),
- child: Column(
- children: [
- if (authProvider.isAuthenticated) ... [
- _buildMenuCard(
- title: '账户设置',
- items: [
- _buildMenuItem(
- icon: Icons.person_outline,
- title: '个人信息',
- subtitle: '查看和编辑个人信息',
- onTap: () {
- Navigator.of(context).push(
- MaterialPageRoute(
- builder: (_) => const ProfileDetailScreen(),
- ),
- );
- },
- ),
- _buildMenuItem(
- icon: Icons.lock_outline,
- title: '账号安全',
- subtitle: '修改密码和安全设置',
- onTap: () {},
- ),
- _buildMenuItem(
- icon: Icons.notifications_none,
- title: '消息通知',
- subtitle: '管理通知偏好设置',
- onTap: () {},
- ),
- ],
- ),
- const SizedBox(height: 16),
- _buildMenuCard(
- title: '我的内容',
- items: [
- _buildMenuItem(
- icon: Icons.bookmark_border,
- title: '我的收藏',
- subtitle: '查看收藏的内容',
- onTap: () {},
- ),
- _buildMenuItem(
- icon: Icons.history,
- title: '浏览历史',
- subtitle: '查看最近浏览记录',
- onTap: () {},
- ),
- _buildMenuItem(
- icon: Icons.download,
- title: '我的下载',
- subtitle: '管理下载的文件',
- onTap: () {},
- ),
- ],
- ),
- const SizedBox(height: 16),
- ],
- _buildMenuCard(
- title: '其他',
- items: [
- _buildMenuItem(
- icon: Icons.help_outline,
- title: '帮助中心',
- subtitle: '常见问题和帮助文档',
- onTap: () {},
- ),
- _buildMenuItem(
- icon: Icons.info_outline,
- title: '关于我们',
- subtitle: '了解应用信息',
- onTap: () {},
- ),
- if (authProvider.isAuthenticated) ... [
- _buildMenuItem(
- icon: Icons.logout,
- title: '退出登录',
- subtitle: '安全退出当前账号',
- onTap: () async {
- // 防止重复点击
- if (authProvider.isLoading) return;
-
- final shouldLogout = await showDialog<bool>(
- context: context,
- builder: (context) {
- final isIOS = Theme.of(context).platform == TargetPlatform.iOS;
-
- if (isIOS) {
- // iOS风格:确定在右边,取消在左边
- return AlertDialog(
- title: const Text('确认退出'),
- content: const Text('确定要退出登录吗?'),
- actionsAlignment: MainAxisAlignment.spaceBetween,
- actions: [
- TextButton(
- onPressed: () => Navigator.of(context).pop(true),
- child: const Text('确定'),
- ),
- TextButton(
- onPressed: () => Navigator.of(context).pop(false),
- child: const Text('取消'),
- ),
- ],
- );
- } else {
- // Android/Material风格:取消在左边,确定在右边
- return AlertDialog(
- title: const Text('确认退出'),
- content: const Text('确定要退出登录吗?'),
- actionsAlignment: MainAxisAlignment.spaceBetween,
- actions: [
- TextButton(
- onPressed: () => Navigator.of(context).pop(false),
- child: const Text('取消'),
- ),
- TextButton(
- onPressed: () => Navigator.of(context).pop(true),
- child: const Text('确定'),
- ),
- ],
- );
- }
- },
- );
-
- if (shouldLogout == true) {
- await authProvider.logout();
-
- // 登出后,确保在组件仍然存在时进行导航
- if (mounted) {
- // 使用pushReplacementNamed清空导航栈
- Navigator.of(context).pushReplacementNamed(RouteConstants.home);
- }
- }
- },
- ),
- ],
- ],
- ),
- const SizedBox(height: 20),
- // 版本信息
- Padding(
- padding: const EdgeInsets.symmetric(vertical: 20),
- child: Text(
- '版本 v1.0.0',
- style: TextStyle(
- fontSize: 12,
- color: Colors.grey[500],
- ),
- ),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- bottomNavigationBar: const BottomNavBar(initialIndex: 3),
- );
- }
-
- Widget _buildStatItem(String label, String value) {
- return Expanded(
- child: Column(
- children: [
- Text(
- value,
- style: const TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.bold,
- ),
- ),
- const SizedBox(height: 4),
- Text(
- label,
- style: TextStyle(
- fontSize: 12,
- color: Colors.grey[600],
- ),
- ),
- ],
- ),
- );
- }
-
- Widget _buildVerticalDivider() {
- return Container(
- width: 1,
- height: 40,
- color: Colors.grey[300],
- margin: const EdgeInsets.symmetric(horizontal: 16),
- );
- }
-
- Widget _buildMenuCard({
- required String title,
- required List<Widget> items,
- }) {
- return Card(
- elevation: 2,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(12),
- ),
- child: Padding(
- padding: const EdgeInsets.all(16),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- title,
- style: const TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.bold,
- ),
- ),
- const SizedBox(height: 12),
- Column(children: items),
- ],
- ),
- ),
- );
- }
-
- Widget _buildMenuItem({
- required IconData icon,
- required String title,
- required String subtitle,
- required VoidCallback onTap,
- }) {
- return ListTile(
- contentPadding: EdgeInsets.zero,
- leading: Icon(icon),
- title: Text(title),
- subtitle: Text(subtitle),
- trailing: const Icon(Icons.chevron_right),
- onTap: onTap,
- );
- }
- }
|