Skip to content

Commit 4f1a2bc

Browse files
ShiftHackZdan1603
authored andcommitted
Fix profile load behavior
1 parent d3f6811 commit 4f1a2bc

4 files changed

Lines changed: 133 additions & 131 deletions

File tree

lib/screens/profile/edit/profile_edit_view.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ProfileEditView extends StatelessWidget {
2121
@override
2222
Widget build(BuildContext context) => StatefulWrapper(
2323
onInit: () {
24-
context.read<ProfileCubit>().getProfile();
24+
//context.read<ProfileCubit>().getProfile();
2525
},
2626
child: Scaffold(
2727
backgroundColor: Colors.white,

lib/screens/profile/profile_cubit.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class ProfileCubit extends Cubit<ProfileState> {
99
final CustomerProfileDataSource _ds = locator<CustomerProfileDataSource>();
1010
final AppDb _db = locator<AppDb>();
1111

12-
ProfileCubit() : super(InitialProfileState());
12+
ProfileCubit() : super(InitialProfileState()) {
13+
getProfile();
14+
}
1315

1416
void logout() async {
1517
emit(LoadingProfileState());
16-
_db.clear().then((_) {
17-
getProfile();
18-
});
18+
_db.clear().then((_) => getProfile());
1919
}
2020

2121
void getProfile() async {

lib/screens/profile/profile_view.dart

Lines changed: 123 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ class ProfileView extends StatelessWidget {
4141
builder: (context, state) {
4242
switch (state.runtimeType) {
4343
case InitialProfileState:
44-
return _loadingState();
44+
return _loadingState(context);
4545
case LoadingProfileState:
46-
return _loadingState();
46+
return _loadingState(context);
4747
case ContentProfileState:
4848
return _contentState(context, (state as ContentProfileState).profile);
4949
case ErrorProfileState:
5050
return _errorState();
5151
case NoAuthProfileState:
5252
return _noAuth(context);
5353
default:
54-
return _loadingState();
54+
return _loadingState(context);
5555
}
5656
},
5757
),
@@ -65,7 +65,10 @@ class ProfileView extends StatelessWidget {
6565
});
6666
});
6767

68-
Widget _contentState(BuildContext context, CustomerProfile profile) => Scaffold(
68+
Widget _contentState(
69+
BuildContext context,
70+
CustomerProfile profile,
71+
) => Scaffold(
6972
appBar: AppBar(
7073
title: Text('tab_profile').tr(),
7174
elevation: 0,
@@ -75,9 +78,9 @@ class ProfileView extends StatelessWidget {
7578
Navigator
7679
.push(context, MaterialPageRoute(builder: (_) => ProfileEditScreen()))
7780
.then((value) {
78-
Future.delayed(Duration(milliseconds: 200), () {
81+
if (value) {
7982
context.read<ProfileCubit>().getProfile();
80-
});
83+
}
8184
print('back, context = $context');
8285
});
8386
},
@@ -105,31 +108,30 @@ class ProfileView extends StatelessWidget {
105108
backgroundColor: Colors.blueGrey,
106109
radius: 40,
107110
child: ClipRRect(
108-
borderRadius: BorderRadius.all(Radius.circular(40)),
109-
child: InkWell(
110-
onTap: () {
111-
Navigator.of(context)
112-
.push(MaterialPageRoute(builder: (_) => GalleryScreen([profile.avatar])));
113-
},
114-
child: CachedNetworkImage(
115-
imageUrl: profile.avatar,
116-
imageBuilder: (context, imageProvider) => Container(
117-
decoration: BoxDecoration(
118-
image: DecorationImage(
119-
image: imageProvider,
120-
fit: BoxFit.cover,
121-
),
111+
borderRadius: BorderRadius.all(Radius.circular(40)),
112+
child: InkWell(
113+
onTap: () => Navigator
114+
.of(context)
115+
.push(MaterialPageRoute(builder: (_) => GalleryScreen([profile.avatar]))),
116+
child: CachedNetworkImage(
117+
imageUrl: profile.avatar,
118+
imageBuilder: (context, imageProvider) => Container(
119+
decoration: BoxDecoration(
120+
image: DecorationImage(
121+
image: imageProvider,
122+
fit: BoxFit.cover,
122123
),
123124
),
124-
placeholder: (context, url) => Shimmer(
125-
duration: Duration(seconds: 1),
126-
enabled: true,
127-
direction: ShimmerDirection.fromLTRB(),
128-
child: Container(color: Colors.white10)
129-
),
130-
errorWidget: (context, url, error) => Center(child: Icon(Icons.error)),
131125
),
126+
placeholder: (context, url) => Shimmer(
127+
duration: Duration(seconds: 1),
128+
enabled: true,
129+
direction: ShimmerDirection.fromLTRB(),
130+
child: Container(color: Colors.white10)
131+
),
132+
errorWidget: (context, url, error) => Center(child: Icon(Icons.error)),
132133
),
134+
),
133135
),
134136
),
135137
),
@@ -149,102 +151,17 @@ class ProfileView extends StatelessWidget {
149151
),
150152
),
151153
),
152-
SizedBox(height: 8),
153-
_profileSection(
154-
FaIcon(FontAwesomeIcons.mapMarkerAlt),
155-
tr('shipping'),
156-
() => Navigator
157-
.push(context, MaterialPageRoute(builder: (_) => ShippingEditScreen()))
158-
.then((value) {
159-
Future.delayed(Duration(milliseconds: 200), () {
160-
context.read<ProfileCubit>().getProfile();
161-
});
162-
print('back, context = $context');
163-
})
164-
),
165-
_profileSection(
166-
FaIcon(FontAwesomeIcons.cog),
167-
tr('settings'),
168-
() => Navigator
169-
.push(context, MaterialPageRoute(builder: (_) => SettingsScreen()))
170-
.then((value) {
171-
Future.delayed(Duration(milliseconds: 200), () {
172-
context.read<ProfileCubit>().getProfile();
173-
});
174-
print('back, context = $context');
175-
})
176-
),
177-
SizedBox(height: 8),
178-
_profileSection(
179-
FaIcon(FontAwesomeIcons.heart),
180-
tr('wish_list'),
181-
() {}
182-
),
183-
_profileSection(
184-
FaIcon(FontAwesomeIcons.shoppingBag),
185-
tr('orders'),
186-
() => Navigator
187-
.push(context, MaterialPageRoute(builder: (_) => OrdersScreen()))
188-
// .then((value) {
189-
// Future.delayed(Duration(milliseconds: 200), () {
190-
// context.read<ProfileCubit>().getProfile();
191-
// });
192-
// print('back, context = $context');
193-
// })
194-
)
195-
,_profileSection(
196-
FaIcon(FontAwesomeIcons.ticketAlt),
197-
tr('coupons'),
198-
() {}
199-
),
200-
SizedBox(height: 8),
201-
_profileSection(
202-
FaIcon(FontAwesomeIcons.map),
203-
tr('shops'),
204-
() => Navigator.push(context, MaterialPageRoute(builder: (_) => ShopMap()))
205-
),
206-
_profileSection(
207-
FaIcon(FontAwesomeIcons.questionCircle),
208-
tr('help'),
209-
() => Navigator.of(context).push(MaterialPageRoute(builder: (_) => HelpScreen()))
210-
),
211-
SizedBox(height: 8),
212-
ElevatedButton(
213-
onPressed: () {
214-
context.read<ProfileCubit>().logout();
215-
},
216-
child: Container(
217-
width: 90,
218-
alignment: Alignment.center,
219-
child: Text(
220-
'logout',
221-
style: TextStyle(
222-
fontSize: 16,
223-
color: Colors.white,
224-
),
225-
).tr(),
226-
),
227-
style: ButtonStyle(
228-
backgroundColor: MaterialStateProperty.all(Color(0xFFF56E6E)),
229-
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
230-
RoundedRectangleBorder(
231-
borderRadius: BorderRadius.circular(36.0),
232-
side: BorderSide(color: Colors.redAccent)
233-
),
234-
),
235-
),
236-
),
237-
SizedBox(height: 4),
238-
Text('Version: 0.1 Alpha', style: TextStyle(color: Colors.grey),),
239-
SizedBox(height: 16),
154+
..._profileSections(context),
240155
],
241156
),
242157
),
243158
),
244159
)
245160
);
246161

247-
Widget _loadingState() => ProfileShimmer();
162+
Widget _loadingState(BuildContext context) => ProfileShimmer(
163+
_profileSections(context)
164+
);
248165

249166
Widget _errorState() => Center(
250167
child: Text("Error"),
@@ -286,4 +203,94 @@ class ProfileView extends StatelessWidget {
286203
),
287204
),
288205
);
206+
207+
List<Widget> _profileSections(BuildContext context) => [
208+
SizedBox(height: 8),
209+
_profileSection(
210+
FaIcon(FontAwesomeIcons.mapMarkerAlt),
211+
tr('shipping'),
212+
() => Navigator.push(
213+
context,
214+
MaterialPageRoute(builder: (_) => ShippingEditScreen()),
215+
).then((value) {
216+
if (value) {
217+
context.read<ProfileCubit>().getProfile();
218+
}
219+
print('back, context = $context');
220+
}),
221+
),
222+
_profileSection(
223+
FaIcon(FontAwesomeIcons.cog),
224+
tr('settings'),
225+
() => Navigator.push(
226+
context,
227+
MaterialPageRoute(builder: (_) => SettingsScreen()),
228+
).then((value) {
229+
print('back, context = $context');
230+
}),
231+
),
232+
SizedBox(height: 8),
233+
_profileSection(FaIcon(FontAwesomeIcons.heart), tr('wish_list'), () {}),
234+
_profileSection(
235+
FaIcon(FontAwesomeIcons.shoppingBag),
236+
tr('orders'),
237+
() => Navigator.push(
238+
context,
239+
MaterialPageRoute(builder: (_) => OrdersScreen()),
240+
),
241+
),
242+
_profileSection(
243+
FaIcon(FontAwesomeIcons.ticketAlt),
244+
tr('coupons'),
245+
() {},
246+
),
247+
SizedBox(height: 8),
248+
_profileSection(
249+
FaIcon(FontAwesomeIcons.map),
250+
tr('shops'),
251+
() => Navigator.push(
252+
context,
253+
MaterialPageRoute(builder: (_) => ShopMap()),
254+
),
255+
),
256+
_profileSection(
257+
FaIcon(FontAwesomeIcons.questionCircle),
258+
tr('help'),
259+
() => Navigator
260+
.of(context)
261+
.push(MaterialPageRoute(builder: (_) => HelpScreen())),
262+
),
263+
SizedBox(height: 8),
264+
ElevatedButton(
265+
onPressed: () {
266+
context.read<ProfileCubit>().logout();
267+
},
268+
child: Container(
269+
width: 90,
270+
alignment: Alignment.center,
271+
child: Text(
272+
'logout',
273+
style: TextStyle(
274+
fontSize: 16,
275+
color: Colors.white,
276+
),
277+
).tr(),
278+
),
279+
style: ButtonStyle(
280+
backgroundColor: MaterialStateProperty.all(Color(0xFFF56E6E)),
281+
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
282+
RoundedRectangleBorder(
283+
borderRadius: BorderRadius.circular(36.0),
284+
side: BorderSide(color: Colors.redAccent),
285+
),
286+
),
287+
),
288+
),
289+
SizedBox(height: 4),
290+
Text(
291+
'Version: 0.1 Alpha',
292+
style: TextStyle(color: Colors.grey),
293+
),
294+
SizedBox(height: 16),
295+
];
289296
}

lib/widget/shimmer.dart

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ class FeaturedShimmer extends StatelessWidget {
387387
}
388388

389389
class ProfileShimmer extends StatelessWidget {
390+
final List<Widget> sections;
391+
392+
ProfileShimmer(this.sections);
393+
390394
@override
391395
Widget build(BuildContext context) => Scaffold(
392396
appBar: AppBar(
@@ -446,16 +450,7 @@ class ProfileShimmer extends StatelessWidget {
446450
),
447451
),
448452
),
449-
SizedBox(height: 8),
450-
_shimmerSection(),
451-
_shimmerSection(),
452-
SizedBox(height: 8),
453-
_shimmerSection(),
454-
_shimmerSection(),
455-
_shimmerSection(),
456-
SizedBox(height: 8),
457-
_shimmerSection(),
458-
_shimmerSection(),
453+
...sections,
459454
],
460455
),
461456
),

0 commit comments

Comments
 (0)