Просмотр исходного кода

修改authProvider与userProvider数据同步

刘清 1 месяц назад
Родитель
Сommit
3ae7996e3f

+ 1
- 1
lib/presentation/navigation/bottom_nav_bar.dart Просмотреть файл

73
         BottomNavigationBarItem(
73
         BottomNavigationBarItem(
74
           icon: Icon(Icons.newspaper_outlined),
74
           icon: Icon(Icons.newspaper_outlined),
75
           activeIcon: Icon(Icons.newspaper),
75
           activeIcon: Icon(Icons.newspaper),
76
-          label: '资讯',
76
+          label: '推荐',
77
         ),
77
         ),
78
         BottomNavigationBarItem(
78
         BottomNavigationBarItem(
79
           icon: Icon(Icons.work_outline),
79
           icon: Icon(Icons.work_outline),

+ 5
- 0
lib/presentation/providers/auth_provider.dart Просмотреть файл

112
     _error = null;
112
     _error = null;
113
     notifyListeners();
113
     notifyListeners();
114
   }
114
   }
115
+
116
+  void updateUser(User? user) {
117
+    _user = user;
118
+    notifyListeners();
119
+  }
115
 }
120
 }

+ 5
- 0
lib/presentation/providers/user_provider.dart Просмотреть файл

59
     _error = null;
59
     _error = null;
60
     notifyListeners();
60
     notifyListeners();
61
   }
61
   }
62
+
63
+  void updateUser(User? user) {
64
+    _user = user;
65
+    notifyListeners();
66
+  }
62
 }
67
 }

+ 5
- 0
lib/presentation/screens/auth/login_screen.dart Просмотреть файл

2
 import 'package:provider/provider.dart';
2
 import 'package:provider/provider.dart';
3
 import '../../providers/auth_provider.dart';
3
 import '../../providers/auth_provider.dart';
4
 import '../../../core/constants/route_constants.dart';
4
 import '../../../core/constants/route_constants.dart';
5
+import '../../providers/user_provider.dart';
5
 import '../../widgets/common/app_button.dart';
6
 import '../../widgets/common/app_button.dart';
6
 import '../../widgets/common/app_text_field.dart';
7
 import '../../widgets/common/app_text_field.dart';
7
 import '../../widgets/common/loading_indicator.dart';
8
 import '../../widgets/common/loading_indicator.dart';
120
                           _passwordController.text,
121
                           _passwordController.text,
121
                         );
122
                         );
122
                         if (authProvider.isAuthenticated) {
123
                         if (authProvider.isAuthenticated) {
124
+                          // ✅ 同时更新 userProvider
125
+                          final userProvider = Provider.of<UserProvider>(context, listen: false);
126
+                          userProvider.updateUser(authProvider.user);
127
+
123
                           // widget.onSuccess?.call();
128
                           // widget.onSuccess?.call();
124
                           Navigator.of(context).pushReplacementNamed(RouteConstants.home);
129
                           Navigator.of(context).pushReplacementNamed(RouteConstants.home);
125
                           // Navigator.of(context).pushNamedAndRemoveUntil(RouteConstants.home);
130
                           // Navigator.of(context).pushNamedAndRemoveUntil(RouteConstants.home);

+ 1
- 1
lib/presentation/screens/home/home_screen.dart Просмотреть файл

113
                 children: [
113
                 children: [
114
                   _buildFeatureCard(
114
                   _buildFeatureCard(
115
                     icon: Icons.newspaper,
115
                     icon: Icons.newspaper,
116
-                    title: '资讯',
116
+                    title: '推荐',
117
                     color: Colors.green,
117
                     color: Colors.green,
118
                     onTap: () {
118
                     onTap: () {
119
                       Navigator.of(context).pushNamed('/news');
119
                       Navigator.of(context).pushNamed('/news');

+ 1
- 1
lib/presentation/screens/news/news_screen.dart Просмотреть файл

8
   Widget build(BuildContext context) {
8
   Widget build(BuildContext context) {
9
     return Scaffold(
9
     return Scaffold(
10
       appBar: AppBar(
10
       appBar: AppBar(
11
-        title: const Text('资讯'),
11
+        title: const Text('推荐'),
12
         actions: [
12
         actions: [
13
           IconButton(
13
           IconButton(
14
             icon: const Icon(Icons.search),
14
             icon: const Icon(Icons.search),

+ 9
- 2
lib/presentation/screens/profile/profile_detail_screen.dart Просмотреть файл

1
 import 'package:flutter/material.dart';
1
 import 'package:flutter/material.dart';
2
 import 'package:provider/provider.dart';
2
 import 'package:provider/provider.dart';
3
 import '../../../data/models/user.dart';
3
 import '../../../data/models/user.dart';
4
+import '../../providers/auth_provider.dart';
4
 import '../../providers/user_provider.dart';
5
 import '../../providers/user_provider.dart';
5
 import '../../widgets/common/app_button.dart';
6
 import '../../widgets/common/app_button.dart';
6
 import '../../widgets/common/app_text_field.dart';
7
 import '../../widgets/common/app_text_field.dart';
24
     super.initState();
25
     super.initState();
25
     final userProvider = Provider.of<UserProvider>(context, listen: false);
26
     final userProvider = Provider.of<UserProvider>(context, listen: false);
26
     _currentUser = userProvider.user ?? User(id: -1, email: '', username: '', createdAt: DateTime.now());
27
     _currentUser = userProvider.user ?? User(id: -1, email: '', username: '', createdAt: DateTime.now());
27
-    _nameController = TextEditingController(text: _currentUser.username);
28
+    _nameController = TextEditingController(text: _currentUser.fullName);
28
     _emailController = TextEditingController(text: _currentUser.email);
29
     _emailController = TextEditingController(text: _currentUser.email);
29
     _phoneController = TextEditingController(text: _currentUser.phone ?? '');
30
     _phoneController = TextEditingController(text: _currentUser.phone ?? '');
30
   }
31
   }
183
                     
184
                     
184
                     await userProvider.updateUserProfile(updatedUser);
185
                     await userProvider.updateUserProfile(updatedUser);
185
                     
186
                     
186
-                    if (userProvider.error == null) {
187
+                    if (userProvider.error == null && mounted) {
188
+                      // ✅ 同时更新 AuthProvider
189
+                      final authProvider = Provider.of<AuthProvider>(context, listen: false);
190
+                      authProvider.updateUser(userProvider.user);  // 使用接口返回的完整用户数据
191
+
187
                       ScaffoldMessenger.of(context).showSnackBar(
192
                       ScaffoldMessenger.of(context).showSnackBar(
188
                         const SnackBar(
193
                         const SnackBar(
189
                           content: Text('个人信息已更新'),
194
                           content: Text('个人信息已更新'),
190
                           duration: Duration(seconds: 2),
195
                           duration: Duration(seconds: 2),
191
                         ),
196
                         ),
192
                       );
197
                       );
198
+
199
+                      // ✅ 直接返回 - 数据已同步更新
193
                       Navigator.of(context).pop();
200
                       Navigator.of(context).pop();
194
                     }
201
                     }
195
                   }
202
                   }

+ 16
- 18
lib/presentation/screens/profile/profile_screen.dart Просмотреть файл

5
 import '../../providers/user_provider.dart';
5
 import '../../providers/user_provider.dart';
6
 import '../../navigation/bottom_nav_bar.dart';
6
 import '../../navigation/bottom_nav_bar.dart';
7
 import 'profile_detail_screen.dart';
7
 import 'profile_detail_screen.dart';
8
-import '../../widgets/custom/protected_widget.dart';
9
 
8
 
10
 class ProfileScreen extends StatelessWidget {
9
 class ProfileScreen extends StatelessWidget {
11
   const ProfileScreen({super.key});
10
   const ProfileScreen({super.key});
23
 }
22
 }
24
 
23
 
25
 class _ProfileContentState extends State<_ProfileContent> {
24
 class _ProfileContentState extends State<_ProfileContent> {
26
-  @override
27
-  void initState() {
28
-    super.initState();
29
-    WidgetsBinding.instance.addPostFrameCallback((_) {
30
-      final userProvider = Provider.of<UserProvider>(context, listen: false);
31
-      userProvider.loadUserProfile();
32
-    });
33
-  }
25
+  // @override
26
+  // void initState() {
27
+  //   super.initState();
28
+  //   WidgetsBinding.instance.addPostFrameCallback((_) {
29
+  //     final userProvider = Provider.of<UserProvider>(context, listen: false);
30
+  //     userProvider.loadUserProfile();
31
+  //   });
32
+  // }
34
 
33
 
35
   @override
34
   @override
36
   Widget build(BuildContext context) {
35
   Widget build(BuildContext context) {
37
     final authProvider = Provider.of<AuthProvider>(context);
36
     final authProvider = Provider.of<AuthProvider>(context);
38
-    // final userProvider = Provider.of<UserProvider>(context);
39
     
37
     
40
     return Scaffold(
38
     return Scaffold(
41
       appBar: AppBar(
39
       appBar: AppBar(
96
                                   width: 2,
94
                                   width: 2,
97
                                 ),
95
                                 ),
98
                               ),
96
                               ),
99
-                              child: const Icon(
100
-                                Icons.edit,
101
-                                size: 16,
102
-                                color: Colors.white,
103
-                              ),
97
+                              // child: const Icon(
98
+                              //   Icons.edit,
99
+                              //   size: 16,
100
+                              //   color: Colors.white,
101
+                              // ),
104
                             ),
102
                             ),
105
                           ),
103
                           ),
106
                         ],
104
                         ],
137
                     Row(
135
                     Row(
138
                       mainAxisAlignment: MainAxisAlignment.center,
136
                       mainAxisAlignment: MainAxisAlignment.center,
139
                       children: [
137
                       children: [
140
-                        _buildStatItem('关注', '128'),
138
+                        _buildStatItem('关注', '0'),
141
                         _buildVerticalDivider(),
139
                         _buildVerticalDivider(),
142
-                        _buildStatItem('粉丝', '256'),
140
+                        _buildStatItem('粉丝', '0'),
143
                         _buildVerticalDivider(),
141
                         _buildVerticalDivider(),
144
-                        _buildStatItem('积分', '1024'),
142
+                        _buildStatItem('积分', '0'),
145
                       ],
143
                       ],
146
                     ),
144
                     ),
147
                   ],
145
                   ],