From feb9aa2efd14c0808e1d6a5802a36b2bb3902324 Mon Sep 17 00:00:00 2001 From: poojapandeyx Date: Thu, 11 Jul 2024 10:51:29 +0530 Subject: [PATCH] get country api --- .../bloc/GetCountry/GetCountryAPI.dart | 3 +- .../bloc/GetCountry/getcountry_bloc.dart | 19 ++- .../bloc/GetCountry/getcountryevent_bloc.dart | 23 ++- .../widgets/country_selection_list.dart | 142 ++++++++++++------ .../widgets/login_signup_button.dart | 6 +- lib/shared/api/network_api_services.dart | 27 +++- 6 files changed, 156 insertions(+), 64 deletions(-) diff --git a/lib/features/countrySelection/presentation/bloc/GetCountry/GetCountryAPI.dart b/lib/features/countrySelection/presentation/bloc/GetCountry/GetCountryAPI.dart index d020d42..7cc0a8b 100644 --- a/lib/features/countrySelection/presentation/bloc/GetCountry/GetCountryAPI.dart +++ b/lib/features/countrySelection/presentation/bloc/GetCountry/GetCountryAPI.dart @@ -5,10 +5,11 @@ import '../../../../../shared/api/network_api_services.dart'; class GetCountryAPI { GetCountryAPI(); - Future getcountryAPI() async { + Future getcountryAPI() async { String url=ApiEndpoints.getcountryurl; final response = await NetworkApiService().get( url, ); + return response; } } \ No newline at end of file diff --git a/lib/features/countrySelection/presentation/bloc/GetCountry/getcountry_bloc.dart b/lib/features/countrySelection/presentation/bloc/GetCountry/getcountry_bloc.dart index a5f1bda..564c41c 100644 --- a/lib/features/countrySelection/presentation/bloc/GetCountry/getcountry_bloc.dart +++ b/lib/features/countrySelection/presentation/bloc/GetCountry/getcountry_bloc.dart @@ -2,26 +2,29 @@ import 'package:bloc/bloc.dart'; import 'package:tanami_app/features/countrySelection/presentation/bloc/GetCountry/getcountryevent_bloc.dart'; import '../../../../../Api_Helper/base_manager.dart'; +import '../../../../../domain/model/GetCountry_model.dart'; import 'GetCountryAPI.dart'; -dynamic responseFromApi = []; - class GetCountryBlock extends Bloc { - GetCountryBlock() : super(GetCountryState.initial) { + GetCountryBlock() : super(CountryInitial()) { on(mapEventToState); } Future mapEventToState( GetCountry event, Emitter emit) async { if (event is GetCountry) { - emit(GetCountryState.loading); + emit(CountryLoading()); try { - var resp = await GetCountryAPI().getcountryAPI(); + ResponseData response = await GetCountryAPI().getcountryAPI(); + if (response.status == ResponseStatus.SUCCESS) { + GetCountryModel countryModel = + GetCountryModel.fromJson(response.data); + emit(CountryLoaded(countryModel)); + } print("//"); } catch (e) { - emit(GetCountryState.failure); - + emit(CountryError("Oops Something went wrong")); } } } -} \ No newline at end of file +} diff --git a/lib/features/countrySelection/presentation/bloc/GetCountry/getcountryevent_bloc.dart b/lib/features/countrySelection/presentation/bloc/GetCountry/getcountryevent_bloc.dart index dea2a67..bc6bb65 100644 --- a/lib/features/countrySelection/presentation/bloc/GetCountry/getcountryevent_bloc.dart +++ b/lib/features/countrySelection/presentation/bloc/GetCountry/getcountryevent_bloc.dart @@ -1,7 +1,9 @@ +import '../../../../../domain/model/GetCountry_model.dart'; + abstract class GetCountryEvent { const GetCountryEvent(); - List get props => []; + get props => []; } class GetCountry extends GetCountryEvent { @@ -9,6 +11,21 @@ class GetCountry extends GetCountryEvent { } - +abstract class GetCountryState{} // Define states -enum GetCountryState { initial, loading, success, failure, error } \ No newline at end of file +//enum GetCountryState { initial, loading, success, failure, error } +class CountryInitial extends GetCountryState {} + +class CountryLoading extends GetCountryState {} + +class CountryLoaded extends GetCountryState { + final GetCountryModel countryModel; + + CountryLoaded(this.countryModel); +} + +class CountryError extends GetCountryState { + final String message; + + CountryError(this.message); +} \ No newline at end of file diff --git a/lib/features/countrySelection/presentation/widgets/country_selection_list.dart b/lib/features/countrySelection/presentation/widgets/country_selection_list.dart index 2785e44..db26358 100644 --- a/lib/features/countrySelection/presentation/widgets/country_selection_list.dart +++ b/lib/features/countrySelection/presentation/widgets/country_selection_list.dart @@ -17,68 +17,118 @@ class CountrySelectionList extends StatelessWidget { @override Widget build(BuildContext context) { final radioBloc = context.read(); - return BlocConsumer( + /* return BlocConsumer( listener: (context, state) { - if (state == GetCountryState.success) { + /* if (state == GetCountryState.success) { const SnackBar(content: Text("Successfully fetch")); } else if (state == GetCountryState.error) { const SnackBar(content: Text(" error")); } else { const SnackBar(content: Text(" not fetch")); - } + } */ }, builder: (context, state) { print(state); - if (state == GetCountryState.loading) { - return const Center( - child: SizedBox( - height: 40, - width: 40, - child: // ShimmerCommon(), - CircularProgressIndicator( - color: Color(0xFF0093FF), - ), - ), - ); - } - return Text("wait"); - }); - /* BlocBuilder( + if (state is CountryLoading) { + return Center(child: CircularProgressIndicator()); + } else if (state is CountryLoaded) { + return ListView.builder( + itemCount: state.countryModel.data?.length ?? 0, + itemBuilder: (context, index) { + var country = state.countryModel.data![index]; + return ListTile( + title: Row( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + // Adjust according to how you handle flags + Image.asset( + countryFlag[index], + width: 24, + height: 24, + ), + const SizedBox(width: 10), + Text(country.countryName ?? ''), + ], + ), + leading: Radio( + activeColor: Colors.blue, + value: index, + groupValue: -1, // You can update this to manage selected index + onChanged: (int? value) { + // Handle radio button change + }, + ), + ); + }, + ); + } else if (state is CountryError) { + return Center(child: Text(state.message)); + } else { + return Center(child: Text('Press button to fetch country data')); + } + }); */ + + return BlocBuilder( builder: (context, state) { int selectedIndex = -1; if (state is RadioSelectionChanged) { selectedIndex = state.selectedIndex; } - return Column( - children: List.generate(countryFlag.length, (int index) { - return ListTile( - title: Row( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - Image.asset( - countryFlag[index], - width: 24, - height: 24, + return BlocConsumer( + listener: (context, state) { + if (state == CountryLoaded) { + const SnackBar(content: Text("Successfully fetch")); + } else if (state == CountryError) { + const SnackBar(content: Text("error while fetching data")); + Future.delayed(Duration(milliseconds: 3), () { + context.read().add(GetCountry()); + }); + } else { + const SnackBar(content: Text(" not fetch")); + } + }, builder: (context, state) { + print(state); + if (state is CountryLoading) { + return Center(child: CircularProgressIndicator()); + } else if (state is CountryLoaded) { + return ListView.builder( + itemCount: state.countryModel.data?.length ?? 0, + itemBuilder: (context, index) { + var country = state.countryModel.data![index]; + return ListTile( + title: Row( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Image.asset( + countryFlag[index], + width: 24, + height: 24, + ), + const Gap(10), + TextWidget().text14W500(country.countryName.toString(), + clr: AppColor.charcoalColor), + ], ), - const Gap(10), - TextWidget().text14W500(countryName[index], - clr: AppColor.charcoalColor), - ], - ), - leading: Radio( - activeColor: AppColor.radioActiveColor, - value: index, - groupValue: selectedIndex, - onChanged: (int? value) { - if (value != null) { - radioBloc.add(RadioSelected(value)); - } - }, - ), + leading: Radio( + activeColor: AppColor.radioActiveColor, + value: index, + groupValue: selectedIndex, + onChanged: (int? value) { + if (value != null) { + radioBloc.add(RadioSelected(value)); + } + }, + ), + ); + }, ); - }), - ); + } else if (state is CountryError) { + return Center(child: Text(state.message)); + } else { + return Center(child: Text('Press button to fetch country data')); + } + }); }, - ); */ + ); } } diff --git a/lib/features/welcome/presentation/widgets/login_signup_button.dart b/lib/features/welcome/presentation/widgets/login_signup_button.dart index 0b03ee8..7770381 100644 --- a/lib/features/welcome/presentation/widgets/login_signup_button.dart +++ b/lib/features/welcome/presentation/widgets/login_signup_button.dart @@ -31,10 +31,10 @@ class LoginSignUpButton extends StatelessWidget { height: 56.h, child: ButtonWidget().elevatedBtn( function: () { - context.read().add(GetCountry()); - /* goRouter.goNamed(RouteName.registerStepScreen, pathParameters: { + context.read().add(GetCountry()); + goRouter.goNamed(RouteName.registerStepScreen, pathParameters: { "fromScreentype": "welcome", - }); */ + }); }, text: localizations.translate(AppText.signUpText), txtClr: AppColor.plainWhite, diff --git a/lib/shared/api/network_api_services.dart b/lib/shared/api/network_api_services.dart index 09f30ed..b3a486a 100644 --- a/lib/shared/api/network_api_services.dart +++ b/lib/shared/api/network_api_services.dart @@ -1,17 +1,38 @@ // common_api.dart import 'package:dio/dio.dart'; +import 'package:flutter/foundation.dart'; + +import '../../Api_Helper/base_manager.dart'; class NetworkApiService { final Dio _dio = Dio(); // Common function for GET requests - Future get(String url, + Future get(String url, {Map? queryParameters}) async { + if (kDebugMode) { + print("api url is >>> $url"); + } + Response response; try { - return await _dio.get(url); + response = await _dio.get(url); + if (response.statusCode == 201 || response.statusCode == 200) { + return ResponseData("success", ResponseStatus.SUCCESS, + data: response.data); + } else { + try { + return ResponseData( + response.data['message'].toString(), ResponseStatus.FAILED); + } catch (_) { + return ResponseData( + data: response.data, + response.statusMessage!, + ResponseStatus.FAILED); + }} } catch (e) { - throw _handleError(e); + return ResponseData( + "Something went wrong", ResponseStatus.FAILED); } }