get country api
This commit is contained in:
@@ -5,10 +5,11 @@ import '../../../../../shared/api/network_api_services.dart';
|
||||
|
||||
class GetCountryAPI {
|
||||
GetCountryAPI();
|
||||
Future<void> getcountryAPI() async {
|
||||
Future<ResponseData> getcountryAPI() async {
|
||||
String url=ApiEndpoints.getcountryurl;
|
||||
final response = await NetworkApiService().get(
|
||||
url,
|
||||
);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -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<GetCountryEvent, GetCountryState> {
|
||||
GetCountryBlock() : super(GetCountryState.initial) {
|
||||
GetCountryBlock() : super(CountryInitial()) {
|
||||
on<GetCountry>(mapEventToState);
|
||||
}
|
||||
Future<void> mapEventToState(
|
||||
GetCountry event, Emitter<GetCountryState> 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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import '../../../../../domain/model/GetCountry_model.dart';
|
||||
|
||||
abstract class GetCountryEvent {
|
||||
const GetCountryEvent();
|
||||
|
||||
List<Object> 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 }
|
||||
//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);
|
||||
}
|
||||
@@ -17,68 +17,118 @@ class CountrySelectionList extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final radioBloc = context.read<RadioBloc>();
|
||||
return BlocConsumer<GetCountryBlock, GetCountryState>(
|
||||
/* return BlocConsumer<GetCountryBlock, GetCountryState>(
|
||||
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<RadioBloc, RadioState>(
|
||||
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<int>(
|
||||
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<RadioBloc, RadioState>(
|
||||
builder: (context, state) {
|
||||
int selectedIndex = -1;
|
||||
if (state is RadioSelectionChanged) {
|
||||
selectedIndex = state.selectedIndex;
|
||||
}
|
||||
|
||||
return Column(
|
||||
children: List<Widget>.generate(countryFlag.length, (int index) {
|
||||
return ListTile(
|
||||
title: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Image.asset(
|
||||
countryFlag[index],
|
||||
width: 24,
|
||||
height: 24,
|
||||
return BlocConsumer<GetCountryBlock, GetCountryState>(
|
||||
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<GetCountryBlock>().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<int>(
|
||||
activeColor: AppColor.radioActiveColor,
|
||||
value: index,
|
||||
groupValue: selectedIndex,
|
||||
onChanged: (int? value) {
|
||||
if (value != null) {
|
||||
radioBloc.add(RadioSelected(value));
|
||||
}
|
||||
},
|
||||
),
|
||||
leading: Radio<int>(
|
||||
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'));
|
||||
}
|
||||
});
|
||||
},
|
||||
); */
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,10 +31,10 @@ class LoginSignUpButton extends StatelessWidget {
|
||||
height: 56.h,
|
||||
child: ButtonWidget().elevatedBtn(
|
||||
function: () {
|
||||
context.read<GetCountryBlock>().add(GetCountry());
|
||||
/* goRouter.goNamed(RouteName.registerStepScreen, pathParameters: {
|
||||
context.read<GetCountryBlock>().add(GetCountry());
|
||||
goRouter.goNamed(RouteName.registerStepScreen, pathParameters: {
|
||||
"fromScreentype": "welcome",
|
||||
}); */
|
||||
});
|
||||
},
|
||||
text: localizations.translate(AppText.signUpText),
|
||||
txtClr: AppColor.plainWhite,
|
||||
|
||||
@@ -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<Response> get(String url,
|
||||
Future<ResponseData> get(String url,
|
||||
{Map<String, dynamic>? 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<dynamic>("success", ResponseStatus.SUCCESS,
|
||||
data: response.data);
|
||||
} else {
|
||||
try {
|
||||
return ResponseData<dynamic>(
|
||||
response.data['message'].toString(), ResponseStatus.FAILED);
|
||||
} catch (_) {
|
||||
return ResponseData<dynamic>(
|
||||
data: response.data,
|
||||
response.statusMessage!,
|
||||
ResponseStatus.FAILED);
|
||||
}}
|
||||
} catch (e) {
|
||||
throw _handleError(e);
|
||||
return ResponseData<dynamic>(
|
||||
"Something went wrong", ResponseStatus.FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user