-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ 스크롤 시 프로필 카드 10개씩 업데이트 기능 구현 #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,79 +23,87 @@ class TuTiCardMobile extends ConsumerStatefulWidget { | |
} | ||
|
||
class _TuTiCardMobileState extends ConsumerState<TuTiCardMobile> { | ||
Future<List<MemberModel>> getMembersBuilder() async { | ||
late ScrollController _scrollController; | ||
int _page = 0; | ||
List<MemberModel> _allMembers = []; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_scrollController = ScrollController(); | ||
_scrollController.addListener(_scrollListener); | ||
_initializeMemberData(); | ||
} | ||
|
||
Future<void> _initializeMemberData() async { | ||
await getMembersBuilder(page: _page); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Future<List<MemberModel>> getMembersBuilder({required int page}) async { | ||
final memberService = ref.read(memberServiceProvider); | ||
final members = await memberService.getMembers(context); | ||
return members; | ||
print(page); | ||
final members = await memberService.getMembers(context, page); | ||
|
||
if (members.isNotEmpty) { | ||
setState(() { | ||
_allMembers.addAll(members); | ||
}); | ||
} | ||
|
||
return _allMembers; | ||
} | ||
|
||
void _getDetailProfile(int memberId) async { | ||
if (context.mounted) { | ||
context.pushNamed( | ||
TuTiDetailScreen.routeName, | ||
params: {'tab': 'tuti'}, | ||
queryParams: { | ||
'memberId': memberId.toString(), | ||
}, | ||
); | ||
void _scrollListener() { | ||
// 리스트의 맨 아래에 도달했을 때 | ||
if (_scrollController.position.pixels == | ||
_scrollController.position.maxScrollExtent) { | ||
//서버에서 받아온 멤버 객체가 10의 배수일 때만 서버의 다음 페이지에 대한 멤버 정보를 받아옴. | ||
if (_allMembers.length % 10 == 0) { | ||
_page++; | ||
getMembersBuilder(page: _page); | ||
} | ||
Comment on lines
+56
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 창환님 궁금한 점이 있어 질문 드립니다!
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 답변 드립니다!
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 서버 응답 필드 중 hasNext는 다음 페이지의 여부 값이 담겨있는데요! 혹시 사용하시지 않고 직접 개수를 체크하시는 이유가 있을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앗 그 여부를 놓쳤네요..! 피드백 감사합니다! 수정하겠습니당 👍 |
||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Expanded( | ||
child: FutureBuilder<List<MemberModel>>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FutureBuilder로 해당 기능을 구현하려고 할 시, 유저의 스크롤 포인트가 화면 제일 위로 초기화되는 문제가 있어 이를 해결하기 위해 ListView.builder 위젯을 사용했습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다만 비동기 객체라 1) 로딩 2) 에러 시 분기 처리를 해줘야할 것 같습니다..! |
||
future: getMembersBuilder(), | ||
builder: (context, snapshot) { | ||
if (snapshot.connectionState == ConnectionState.waiting) { | ||
return const Center( | ||
child: CircularProgressIndicator(), | ||
); | ||
} | ||
if (snapshot.hasError) { | ||
return const Center( | ||
child: Text('데이터를 불러오는데 실패했습니다.'), | ||
); | ||
} | ||
final members = snapshot.data!; | ||
return ListView.separated( | ||
itemCount: members.length, | ||
separatorBuilder: (context, index) { | ||
return Gaps.h32; | ||
}, | ||
itemBuilder: (context, index) { | ||
final member = members[index]; | ||
return Padding( | ||
padding: EdgeInsets.symmetric(horizontal: 10.sp), | ||
child: Container( | ||
constraints: BoxConstraints( | ||
minHeight: 250.h, | ||
maxHeight: 250.h, | ||
), | ||
decoration: ShapeDecoration( | ||
color: Colors.white, | ||
shape: RoundedRectangleBorder( | ||
side: const BorderSide( | ||
width: 2, | ||
color: ColorConstants.primaryColor, | ||
), | ||
borderRadius: BorderRadius.circular(45), | ||
), | ||
), | ||
child: Row( | ||
mainAxisSize: MainAxisSize.max, | ||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: [ | ||
_buildLeftColumn(context, member), | ||
Gaps.w24, | ||
_buildRightColumn(context, member), | ||
], | ||
), | ||
child: ListView.builder( | ||
controller: _scrollController, | ||
itemCount: _allMembers.length, | ||
itemBuilder: (context, index) { | ||
final member = _allMembers[index]; | ||
return Padding( | ||
padding: EdgeInsets.symmetric(horizontal: 10.sp), | ||
child: Container( | ||
constraints: BoxConstraints( | ||
minHeight: 250.h, | ||
maxHeight: 250.h, | ||
), | ||
margin: EdgeInsets.symmetric(vertical: 10.sp), | ||
decoration: ShapeDecoration( | ||
color: Colors.white, | ||
shape: RoundedRectangleBorder( | ||
side: const BorderSide( | ||
width: 2, | ||
color: ColorConstants.primaryColor, | ||
), | ||
); | ||
}, | ||
); | ||
}), | ||
borderRadius: BorderRadius.circular(45), | ||
), | ||
), | ||
child: Row( | ||
mainAxisSize: MainAxisSize.max, | ||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: [ | ||
_buildLeftColumn(context, member), | ||
Gaps.w24, | ||
_buildRightColumn(context, member), | ||
], | ||
), | ||
), | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
|
||
|
@@ -157,6 +165,18 @@ class _TuTiCardMobileState extends ConsumerState<TuTiCardMobile> { | |
); | ||
} | ||
|
||
void _getDetailProfile(int memberId) async { | ||
if (context.mounted) { | ||
context.pushNamed( | ||
TuTiDetailScreen.routeName, | ||
params: {'tab': 'tuti'}, | ||
queryParams: { | ||
'memberId': memberId.toString(), | ||
}, | ||
); | ||
} | ||
} | ||
|
||
Widget _buildSwitchRow(BuildContext context, MemberModel member) { | ||
return Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
서버에서 page 별로 10개의 멤버 객체를 받을 수 있다고 해서