import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:onufitness/constants/color_constant.dart'; import 'package:onufitness/constants/string_constant.dart'; import 'package:onufitness/constants/text_constant.dart'; import 'package:onufitness/screens/rise/controllers/create_challenge_controller.dart'; import 'package:onufitness/screens/rise/controllers/rise_controller.dart'; import 'package:onufitness/screens/rise/views/detailed_rise_screen.dart'; import 'package:onufitness/screens/rise/widgets/add_participent_bottom_sheet.dart'; import 'package:onufitness/screens/rise/widgets/challenge_card.dart'; import 'package:awesome_snackbar_content/awesome_snackbar_content.dart'; import 'package:onufitness/widgets/others/new_custom_sneakbar.dart'; class CreatedTab extends StatelessWidget { CreatedTab({super.key}); final RiseController controller = Get.find(); CreateChallengeController createChallengeController = Get.put( CreateChallengeController(), ); @override Widget build(BuildContext context) { return GestureDetector( onTap: () { FocusScope.of(context).unfocus(); }, child: Column( children: [ Container( margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12.r), ), child: TextField( controller: controller.createdSearchController, textInputAction: TextInputAction.search, onSubmitted: (value) { FocusScope.of(context).unfocus(); }, onChanged: (value) { controller.createdSearchText.value = value; }, decoration: InputDecoration( hintText: 'Search Challenges..', hintStyle: TextStyle( color: lightGreyColor, fontSize: smallSizeText, ), prefixIcon: Icon( Icons.search, color: lightGreyColor, size: 20.sp, ), suffixIcon: Obx( () => controller.createdSearchText.value.isNotEmpty ? IconButton( icon: Icon(Icons.clear, color: Colors.grey), onPressed: () { controller.clearCreatedSearch(); FocusScope.of(context).unfocus(); }, ) : SizedBox(), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: lightGreyColor), borderRadius: BorderRadius.circular(8.r), ), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: lightGreyColor), borderRadius: BorderRadius.circular(8.r), ), contentPadding: EdgeInsets.symmetric( horizontal: 16.w, vertical: 12.h, ), filled: true, fillColor: Colors.white, ), ), ), // Content Expanded( child: Obx(() { if (controller.isCreatedLoading.value && controller.createdChallenges.isEmpty) { return Center(child: CircularProgressIndicator()); } if (controller.createdChallenges.isEmpty) { return RefreshIndicator( onRefresh: () async { controller.refreshCreatedChallenges(); }, child: SingleChildScrollView( physics: AlwaysScrollableScrollPhysics(), child: SizedBox( height: 0.6.sh, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.emoji_events_outlined, size: 64.w, color: Colors.grey, ), SizedBox(height: 16.h), Text( 'No created challenges found', style: TextStyle( fontSize: 16.sp, color: Colors.grey[600], ), ), ], ), ), ), ), ); } return RefreshIndicator( onRefresh: () async { controller.refreshCreatedChallenges(); }, child: ListView.builder( padding: EdgeInsets.symmetric(horizontal: 16.w), itemCount: controller.createdChallenges.length + (controller.createdHasMoreData.value ? 1 : 0), itemBuilder: (context, index) { if (index == controller.createdChallenges.length) { if (controller.isCreatedLoading.value) { return Padding( padding: EdgeInsets.all(16.w), child: Center(child: CircularProgressIndicator()), ); } else { WidgetsBinding.instance.addPostFrameCallback((_) { controller.loadMoreCreatedChallenges(); }); return SizedBox(); } } final challenge = controller.createdChallenges[index]; return Padding( padding: EdgeInsets.only(bottom: 16.h), child: InkWell( onTap: () { Get.to( () => ChallengeDetailsScreen( index: index, challengeItem: challenge, riseController: controller, ), ); }, child: ChallengeCard( tabName: riseCreatedByMeTab, title: challenge.challengeTitle ?? 'Challenge', category: challenge.fitnessGoalTitle ?? 'Fitness', participants: '${challenge.totalParticipants ?? 0}', imageUrl: challenge.challengeImageName, onUpdatePrivacyPressed: () { //................................................................ createChallengeController .challengeVisibilityId .value = challenge.challengeVisibilityID ?? 1; //............................................................... if (challenge.isChallengeStarted == true) { AwesomeCustomSnackbar.show( context: context, title: 'Action Not Allowed', message: 'You cannot update privacy after the challenge has started.', contentType: ContentType.failure, duration: Duration(seconds: 3), ); return; } showModalBottomSheet( isScrollControlled: true, backgroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical( top: Radius.circular(15.r), ), ), context: context, builder: (context) => Padding( padding: EdgeInsets.only( left: 15.w, right: 15.w, top: 20.h, ), child: MembersSelectionPopup( riseController: controller, selectedChallengeId: challenge.challengeId!, isChallengePrivacyUpdate: true, currentVisibilityId: createChallengeController .challengeVisibilityId .value, ), ), ).whenComplete(() { createChallengeController.selectedConnections .clear(); createChallengeController.selectedConnections .refresh(); createChallengeController.selectedIds.clear(); }); }, participantImages: challenge.participants, onAddPeoplePressed: () { if (challenge.isChallengeStarted == true) { AwesomeCustomSnackbar.show( context: context, title: 'Action Not Allowed', message: 'You cannot add participants after the challenge has started.', contentType: ContentType.failure, duration: Duration(seconds: 3), ); return; } showModalBottomSheet( isScrollControlled: true, backgroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical( top: Radius.circular(15.r), ), ), context: context, builder: (context) => Padding( padding: EdgeInsets.only( left: 15.w, right: 15.w, top: 20.h, ), child: MembersSelectionPopup( selectedChallengeId: challenge.challengeId!, ), ), ).whenComplete(() { createChallengeController.selectedConnections .clear(); createChallengeController.selectedConnections .refresh(); createChallengeController.selectedIds.clear(); }); }, onJoinPressed: () async { await controller.joinChallenge( challengeID: challenge.challengeId!, ); controller.fetchCreatedChallenges(isRefresh: true); }, isLoading: controller.isJoiningChallengeLoading, challengeID: challenge.challengeId, isChallengeJoined: challenge.isChallengeJoined!, isChallengeStarted: challenge.isChallengeStarted!, ), ), ); }, ), ); }), ), ], ), ); } }