import 'dart:math'; import 'package:flutter/material.dart'; import 'package:job_app/config/palette.dart'; import 'package:job_app/models/APiResponse.dart'; import 'package:job_app/models/categories.dart'; import 'package:job_app/models/employmentoffer.dart'; import 'package:job_app/models/employment_offers_paginator.dart'; import 'package:job_app/services/categoriesService.dart'; import 'package:job_app/services/employmentOffersService.dart'; import 'package:job_app/services/Auth/getCurrentUser.dart'; import 'package:job_app/services/getToken.dart'; import 'package:job_app/services/jobTypesService.dart'; import 'package:job_app/services/user_location.dart'; import 'package:job_app/services/wishlist.dart'; import 'package:job_app/singletons/bottom_navigator_index.dart'; import 'package:job_app/singletons/categories.dart'; import 'package:job_app/singletons/employment_offers.dart'; import 'package:job_app/singletons/job_types.dart'; import 'package:job_app/singletons/locators.dart'; import 'package:job_app/singletons/user_info.dart'; import 'package:job_app/singletons/wishlist.dart'; import 'package:job_app/views/company_details.dart'; import 'package:job_app/views/wishlist.dart'; import 'package:job_app/widgets/ads_container.dart'; import 'package:job_app/widgets/categories_slider.dart'; import 'package:job_app/widgets/employment_offer_card.dart'; import 'package:job_app/widgets/topcontainer.dart'; import 'package:provider/provider.dart'; import 'package:permission_handler/permission_handler.dart'; class Home extends StatefulWidget { Home({Key key}) : super(key: key); @override _HomeState createState() => _HomeState(); } class _HomeState extends State { bool showLocation = false; int _currentCategoryIndex = 0; int categoryId=0; bool isLoading=false; Random random = Random(); List categoriesList=[]; List employmentOffers=[]; CategoriesService categoriesService = CategoriesService(); JobTypesService jobTypesService=JobTypesService(); EmploymentOffersService employmentOffersService =EmploymentOffersService(); WishlistService wishlistService = WishlistService(); Future getCurrentToken()async{ GetCurrentUserService user = GetCurrentUserService(); ApiResponseModel response= await user.getCurrentUser(); String token =await locator.get().getTokenValue(); if(token!="" && token!="InvalidToken" && token!="token" && (response.statusCode==200 || response.statusCode==201)){ userInfo.isValidToken=true; } else{ userInfo.isValidToken=false; } } Future retrieveCategories()async { setState(() { isLoading=true; }); ApiResponseModel response = await categoriesService.getCategories(); categoriesList =response.data; ApiResponseModel offers =await employmentOffersService.getOffersByCategory(catId: response.data.first.id,page: 0,size: 4); setState(() { employmentOffers =offers.data.offers; isLoading=false; }); } Future retrieveJobTypes()async{ await jobTypesService.getJobTypes(); } checkLocationPermission()async{ var location=await Permission.location.status; if(!location.isGranted){ setState(() { showLocation=true; }); }else{ showLocation=false; } } _onItemSelect(int index) async{ setState(() { _currentCategoryIndex = index; isLoading=true; employmentOffers=[]; }); ApiResponseModel response =await employmentOffersService.getOffersByCategory(catId: categoryId,size: 4,page: 0); setState(() { employmentOffers=response.data.offers; isLoading=false; }); } FuturegetWishlist() async{ await wishlistService.getAllUserWishlist(); } @override void initState() { super.initState(); getCurrentToken(); retrieveCategories(); retrieveJobTypes(); checkLocationPermission(); getWishlist(); } @override Widget build(BuildContext context) { return ChangeNotifierProvider.value( value: CategoriesSingleTons(), child: Scaffold( backgroundColor: Color(0xffF0F5F9), body: GestureDetector( onTap: () { FocusScope.of(context).requestFocus(new FocusNode()); }, child: SingleChildScrollView( child: Column( children: [ TopContainer(), Image( image: AssetImage('assets/images/banners/banner.png'), ), if (showLocation) locationPopUp(), Padding( padding: const EdgeInsets.fromLTRB(20, 20, 20, 0), child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "By category", textAlign: TextAlign.start, style: TextStyle( fontFamily: "MontserratSemiBold", fontSize: 19), ), TextButton( onPressed: () { if (userInfo.isValidToken!=null && userInfo.isValidToken) { Navigator.pushNamed( context, '/all_categories'); } else { Navigator.pushNamed( context, '/login_register'); } }, child: Text( "see all", textAlign: TextAlign.end, style: TextStyle( fontFamily: "MontserratSemiBold", fontSize: 13, color: Colors.black, decoration: TextDecoration.underline), )), ], ), Padding( padding: const EdgeInsets.only( top: 10, ), child: Container( height: 60, width: double.maxFinite, child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Expanded( child: Consumer( builder:(context,data,child){ return ListView.builder( scrollDirection: Axis.horizontal, itemCount: data.categories.length, itemBuilder: (context,i)=>GestureDetector( onTap: (){ if(userInfo.isValidToken){ setState(() { categoryId=data.categories[i].id; }); _onItemSelect(i); } else{ Navigator.pushNamed( context, '/login_register'); } }, child:_currentCategoryIndex==i? CategorySlider(i, true):CategorySlider(i, false)), ); }, ), ), ], ), ), ), Padding( padding: const EdgeInsets.only(top: 10), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Jobs for you", textAlign: TextAlign.start, style: TextStyle( fontFamily: "MontserratSemiBold", fontSize: 19), ), TextButton( onPressed: () { if (userInfo.isValidToken) { bottomNavigatorIndex.setIndex(3); } else { Navigator.pushNamed( context, '/login_register'); } }, child: Text( "see all", textAlign: TextAlign.end, style: TextStyle( fontFamily: "MontserratSemiBold", fontSize: 13, color: Colors.black, decoration: TextDecoration.underline), )), ], ), ), ], ), ), /****************Employment offers*********************/ Padding( padding: const EdgeInsets.symmetric(horizontal: 20), child:!isLoading?ListView.separated( physics: NeverScrollableScrollPhysics(), shrinkWrap: true, itemBuilder: (context, i) => GestureDetector( onTap: () { if (userInfo.isValidToken) { Navigator.push( context, MaterialPageRoute(builder: (context)=>CompanyDetails(passedOffer: employmentOffers[i],offerIndex: i,)) ); } else { Navigator.pushNamed(context, '/login_register'); } }, child: EmploymentOfferCard( offerIndex: i,passedOffers: employmentOffers,)), separatorBuilder: (context, i) { return Container( child: (i != 0 && i % 4 == 0) ? AdsContainer(i) : SizedBox(), ); }, itemCount: employmentOffers.length):Padding( padding: const EdgeInsets.only(top: 40.0), child: Image(image: AssetImage("assets/loading.gif"),height: 30,), ), ) , ], ), ), )), ); } AnimatedContainer locationPopUp() { return AnimatedContainer( duration: Duration(milliseconds: 500), curve: Curves.linear, width: 270, height: 140, decoration: BoxDecoration( color: Palette.secendary, borderRadius: BorderRadius.circular(4)), child: Column( children: [ Padding( padding: const EdgeInsets.fromLTRB(55, 20, 55, 10), child: Text( 'Activate your location to find a nearby job', textAlign: TextAlign.center, style: TextStyle( height: 1.7, fontFamily: 'MontserratSemiBold', color: Colors.white), ), ), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ TextButton( style: ButtonStyle( shape: MaterialStateProperty.all(RoundedRectangleBorder( side: BorderSide(color: Colors.white), borderRadius: BorderRadius.circular(4))), ), onPressed: () async { if (userInfo.isValidToken) { setState(() { showLocation = false; }); } else { Navigator.pushNamed(context, '/login_register'); } }, child: Text( "Not now", style: TextStyle(color: Colors.white), )), SizedBox( width: 10, ), TextButton( style: ButtonStyle( backgroundColor: MaterialStateProperty.all(Palette.primary)), onPressed: () async { if (userInfo.isValidToken) { var location =await Permission.location.status; if(!location.isGranted){ await Permission.location.request(); if(location.isGranted){ setState(() { showLocation=false; }); } } } else { Navigator.pushNamed(context, '/login_register'); } }, child: Text( "OK", style: TextStyle(color: Colors.white), )) ], ), ], ), ); } }