Skip to content

Commit

Permalink
fix: 상품 API 적용에 따른 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
changhwan77 committed Feb 22, 2024
1 parent 1d52321 commit 7fe11b6
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 87 deletions.
13 changes: 3 additions & 10 deletions lib/common/main_navigation_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ class _MainNavigationScreenState extends ConsumerState<MainNavigationScreen>
Widget build(BuildContext context) {
int selectedIndex = ref.watch(navigationSelectedIndexProvider);

void navigationToScreen(int index) {
setState(() {
// 유저가 클릭한 index를 navigationSelectedIndexProvider의 state에 할당
ref.read(navigationSelectedIndexProvider.notifier).state = index;
});
}

void onTap(int index) async {
// 유저가 마이페이지로 이동 시 authToken이 있는지 검증
// 토큰이 null || 비어있으면 로그인 안내 다이얼로그 띄움.
Expand All @@ -51,11 +44,11 @@ class _MainNavigationScreenState extends ConsumerState<MainNavigationScreen>
);
}
} else {
navigationToScreen(index);
ref.read(navigationSelectedIndexProvider.notifier).state = index;
}
// 마이페이지로 이동하는 것이 아닐 때는 바로 이동
} else {
navigationToScreen(index);
// 마이페이지로 이동하는 것이 아닐 때는 바로 이동
ref.read(navigationSelectedIndexProvider.notifier).state = index;
}
}

Expand Down
17 changes: 17 additions & 0 deletions lib/common/service/goods_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:tuti/constants/string.dart';
import 'package:tuti/features/tutis/models/goods.dart';

final goodsProvider = FutureProvider<List<Goods>>((ref) async {
final dio = Dio();
final response = await dio.get('${StringConstants.baseUrl}/products');

if (response.statusCode == 200) {
final List<dynamic> goods = response.data['data'];
final goodsList = goods.map((e) => Goods.fromJson(e)).toList();
return goodsList;
} else {
throw Exception('Failed to load Goods');
}
});
7 changes: 1 addition & 6 deletions lib/common/service/token_provider.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';

enum TokenState {
present,
absent,
}

final tokenProvider = StateProvider((ref) => TokenState.absent);
final tokenProvider = StateProvider<String?>((ref) => '');
6 changes: 6 additions & 0 deletions lib/features/auth/views/join_private_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:tuti/common/constraints_scaffold.dart';
import 'package:tuti/common/custom_token_manager.dart';
import 'package:tuti/common/service/token_provider.dart';
import 'package:tuti/features/auth/models/user_profile_model.dart';
import 'package:tuti/features/auth/widgets/auth_form_field.dart';

Expand Down Expand Up @@ -176,6 +178,10 @@ class _JoinPrivateScreenState extends ConsumerState<JoinPrivateScreen> {
);
final authService = ref.read(authServiceProvider);
await authService.signUp(context, userProfileModel);
final token = await CustomTokenManager.getToken();
print('로그인 시 저장되는 토큰: $token');

ref.read(tokenProvider.notifier).state = token;
if (context.mounted) {
context.goNamed(TuTiScreen.routeName);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/features/auth/views/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tuti/common/constraints_scaffold.dart';
import 'package:tuti/common/custom_token_manager.dart';
import 'package:tuti/common/service/token_provider.dart';
import 'package:tuti/features/auth/services/auth_service.dart';
import 'package:tuti/features/auth/widgets/auth_form_field.dart';
Expand Down Expand Up @@ -33,7 +34,9 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
final authService = ref.read(authServiceProvider);
await authService.login(
context, formData['email']!, formData['password']!);
ref.read(tokenProvider.notifier).state = TokenState.present;
final token = await CustomTokenManager.getToken();

ref.read(tokenProvider.notifier).state = token;
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/features/profile/views/profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ class _ProfileScreenState extends ConsumerState<ProfileScreen> {
TextButton(
onPressed: () async {
await CustomTokenManager.removeToken();
ref.read(tokenProvider.notifier).state =
TokenState.absent;
ref.read(tokenProvider.notifier).state = '';
ref
.read(navigationSelectedIndexProvider.notifier)
.state = 1;
Expand Down
17 changes: 14 additions & 3 deletions lib/features/tutis/models/goods.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
class Goods {
final String title;
final String name;
final double? regularPrice;
final double? discountRate;
final double discountedPrice;
final String? discountPolicy;

const Goods(
{required this.title,
{required this.name,
required this.discountedPrice,
this.discountRate,
this.regularPrice});
this.regularPrice,
this.discountPolicy});

factory Goods.fromJson(Map<String, dynamic> json) {
return Goods(
name: json['name'],
regularPrice: json['price'],
discountRate: json['discountValue'],
discountedPrice: json['discountedPrice'],
discountPolicy: json['discountPolicy']);
}
}
43 changes: 20 additions & 23 deletions lib/features/tutis/views/personal_branding_screen.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tuti/common/constraints_scaffold.dart';
import 'package:tuti/common/service/goods_provider.dart';
import 'package:tuti/common/tuti_text.dart';
import 'package:tuti/constants/color.dart';
import 'package:tuti/features/tutis/models/goods.dart';
Expand All @@ -11,14 +13,16 @@ import 'package:tuti/features/tutis/widgets/tuti_widgets/goods_container.dart';
import 'package:tuti/features/tutis/widgets/tuti_widgets/main_banner.dart';
import 'package:universal_html/js.dart';

class PersonalBrandingScreen extends StatelessWidget {
class PersonalBrandingScreen extends ConsumerWidget {
const PersonalBrandingScreen({super.key});

static const String routeName = "personalBranding";
static const String routePath = "/personalBranding";

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final AsyncValue<List<Goods>> goodsList = ref.watch(goodsProvider);

return ConstraintsScaffold(
child: Column(
children: [
Expand All @@ -31,30 +35,23 @@ class PersonalBrandingScreen extends StatelessWidget {
),
),
Expanded(
child: ListView.builder(
itemCount: goodsCards.length,
itemBuilder: (BuildContext context, index) {
return GoodsContainer(
title: goodsCards[index].title,
regularPrice: goodsCards[index].regularPrice,
discountRate: goodsCards[index].discountRate,
discountedPrice: goodsCards[index].discountedPrice);
},
),
child: switch (goodsList) {
AsyncData(:final value) => ListView.builder(
itemCount: value.length,
itemBuilder: (context, index) => GoodsContainer(
name: value[index].name,
discountRate: value[index].discountRate,
regularPrice: value[index].regularPrice,
discountedPrice: value[index].discountedPrice,
discountPolicy: value[index].discountPolicy,
),
),
AsyncError() => const Text('Oops, something unexpected happened'),
_ => const CircularProgressIndicator(),
},
),
],
),
);
}
}

List<Goods> goodsCards = const [
Goods(
title: '강점 발견 연구소 1기',
regularPrice: 600000,
discountRate: 50,
discountedPrice: 300000),
Goods(title: '발표잘하는 방법 특강 1회', discountedPrice: 250000),
Goods(title: '[건축학과] 공모전 컨설팅', discountedPrice: 250000),
Goods(title: '내 성격에 맞는\n외모 스타일링 컨설팅', discountedPrice: 250000),
];
81 changes: 54 additions & 27 deletions lib/features/tutis/widgets/tuti_widgets/goods_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import 'package:tuti/constants/color.dart';
class GoodsContainer extends StatelessWidget {
const GoodsContainer(
{super.key,
required this.title,
required this.name,
required this.discountedPrice,
this.discountRate,
this.regularPrice});
this.regularPrice,
this.discountPolicy});

final String title;
final String name;
final double? regularPrice;
final double? discountRate;
final double discountedPrice;
final String? discountPolicy;

@override
Widget build(BuildContext context) {
Expand All @@ -39,37 +41,62 @@ class GoodsContainer extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
name,
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(color: Colors.white),
),
if (regularPrice != null)
Text(
numberFormat.format(regularPrice),
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Colors.white,
fontSize: 22.sp,
decoration: TextDecoration.lineThrough,
decorationColor:
ColorConstants.personalBrandingDividerColor,
decorationThickness: 2.0),
if (discountPolicy == '정액')
Column(
children: [
Text(
numberFormat.format(regularPrice),
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Colors.white,
fontSize: 22.sp,
decoration: TextDecoration.lineThrough,
decorationColor:
ColorConstants.personalBrandingDividerColor,
decorationThickness: 2.0),
),
Text(
'${numberFormat.format(discountRate)}원 할인',
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
fontSize: 18.sp,
color:
ColorConstants.personalBrandingTextHighlightColor),
),
],
),
if (regularPrice == null)
RSizedBox(
height: 35.w,
if (discountPolicy == '정률')
Column(
children: [
Text(
numberFormat.format(regularPrice),
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Colors.white,
fontSize: 22.sp,
decoration: TextDecoration.lineThrough,
decorationColor:
ColorConstants.personalBrandingDividerColor,
decorationThickness: 2.0),
),
Text(
'${numberFormat.format(discountRate)}% 할인',
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
fontSize: 18.sp,
color:
ColorConstants.personalBrandingTextHighlightColor),
),
],
),
if (discountRate != null)
Text(
'${numberFormat.format(discountRate)}%할인',
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
fontSize: 18.sp,
color: ColorConstants.personalBrandingTextHighlightColor),
),
if (discountRate == null)
RSizedBox(
height: 35.w,
if (discountPolicy == '없음')
Column(
children: [
RSizedBox(height: 35.w),
RSizedBox(height: 35.w),
],
),
Text(
numberFormat.format(discountedPrice),
Expand Down
25 changes: 17 additions & 8 deletions lib/features/tutis/widgets/tuti_widgets/tuti_card_mobile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ class _TuTiCardMobileState extends ConsumerState<TuTiCardMobile> {
String _maskName(String name) {
if (name.length >= 3) {
return name.replaceRange(1, 2, '*');
} else if (name.length == 2) {
String firstName = name.substring(0, 1);
String maskName = firstName + "*";
return maskName;
}
return name;
}
Expand All @@ -183,19 +187,24 @@ class _TuTiCardMobileState extends ConsumerState<TuTiCardMobile> {
_buildCircleAvatar(),
Gaps.h8,
FutureBuilder(
future: _getDisplayName(member),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const SizedBox();
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
future: _getDisplayName(member),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const SizedBox();
}
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
return TuTiText.small(
context,
snapshot.data ?? '',
);
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
}),
}
return const SizedBox();
},
),
Gaps.h12,
TuTiText.small(
context,
Expand Down
11 changes: 4 additions & 7 deletions lib/features/tutis/widgets/tuti_widgets/tuti_header_mobile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class TuTiHeaderMobile extends ConsumerStatefulWidget {
class _TuTiHeaderMobileState extends ConsumerState<TuTiHeaderMobile> {
@override
Widget build(BuildContext context) {
// 토큰 유무 상태가 변경될 시 header 부분 rebuild
String? token = ref.watch(tokenProvider);
return Padding(
padding: EdgeInsets.symmetric(horizontal: 25.w, vertical: 20.h),
child: Row(
Expand Down Expand Up @@ -63,22 +65,17 @@ class _TuTiHeaderMobileState extends ConsumerState<TuTiHeaderMobile> {
//TODO: 에러 발생 시 어떻게 처리할 것인지 추가 필요
}
if (snapshot.hasData) {
// 토큰 유무 상태가 변경될 시 header 부분 rebuild
TokenState tokenState = ref.watch(tokenProvider);

if (snapshot.data!.isEmpty ||
snapshot.data == null ||
tokenState == TokenState.absent) {
token == null ||
token.isEmpty) {
return TuTiButton(
padding: EdgeInsets.symmetric(
vertical: 10.h, horizontal: 25.w),
fontSize: 10.sp,
title: '로그인',
onPressed: () => _showLoginDialog(context),
);
} else {
// 토큰이 있을 시 tokenProvider의 값을 토큰 있음으로 변경
ref.read(tokenProvider.notifier).state = TokenState.present;
}
}
} else {
Expand Down

0 comments on commit 7fe11b6

Please sign in to comment.