You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
car_provider_app/lib/pages/settings/define_branch_page.dart

231 lines
9.5 KiB
Dart

import 'package:car_provider_app/api/client/branch_api_client.dart';
import 'package:car_provider_app/api/client/user_api_client.dart';
import 'package:car_provider_app/classes/utils.dart';
import 'package:car_provider_app/extensions/int_extensions.dart';
import 'package:car_provider_app/models/m_response.dart';
import 'package:car_provider_app/models/profile/branch.dart';
import 'package:car_provider_app/models/user/cities.dart';
import 'package:car_provider_app/models/user/country.dart';
import 'package:car_provider_app/pages/location/pick_location_page.dart';
import 'package:car_provider_app/utils/utils.dart';
import 'package:car_provider_app/widgets/dropdown/dropdow_field.dart';
import 'package:car_provider_app/widgets/show_fill_button.dart';
import 'package:car_provider_app/widgets/txt_field.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import '../../generated/locale_keys.g.dart';
class DefineBranchPage extends StatefulWidget {
BranchData branchData;
DefineBranchPage(this.branchData);
@override
State<DefineBranchPage> createState() => _DefineBranchPageState();
}
class _DefineBranchPageState extends State<DefineBranchPage> {
String countryCode = "", address = "", branchName = "", branchDescription = "";
double latitude = 0, longitude = 0;
int role = -1, countryId = -1, cityId = -1;
List<DropValue> countryDropList = [];
List<DropValue> citiesDropList = [];
DropValue? countryValue;
DropValue? cityValue;
Country? country;
Cities? cities;
@override
void initState() {
// TODO: implement initState
super.initState();
fetchCountry();
}
fetchCountry() async {
country = await UserApiClent().getAllCountries();
setState(() {
country!.data?.forEach((element) {
if (widget.branchData.id != null) {
if (element.id == widget.branchData.countryID) {
countryValue = new DropValue(
element.id ?? 0, EasyLocalization.of(context)?.currentLocale?.countryCode == "SA" ? (element.countryNameN ?? "") : (element.countryName ?? ""), element.countryCode ?? "");
}
}
countryDropList.add(
new DropValue(element.id ?? 0, EasyLocalization.of(context)?.currentLocale?.countryCode == "SA" ? (element.countryNameN ?? "") : (element.countryName ?? ""), element.countryCode ?? ""));
});
if (widget.branchData.id != null) fetchCites();
});
}
fetchCites() async {
print("objectfffffff");
citiesDropList.clear();
cities = null;
cityId = -1;
cities = await UserApiClent().getAllCites(countryId.toString());
setState(() {
cities!.data?.forEach((element) {
if (widget.branchData.id != null) {
if (element.id == widget.branchData.cityId) {
address = widget.branchData.address!;
branchName = widget.branchData.branchName!;
branchDescription = widget.branchData.branchDescription!;
latitude = double.parse(widget.branchData.latitude ?? "");
longitude = double.parse(widget.branchData.longitude ?? "");
countryId = widget.branchData.countryID!;
cityId = widget.branchData.cityId!;
cityValue =
new DropValue(element.id ?? 0, EasyLocalization.of(context)?.currentLocale?.countryCode == "SA" ? (element.cityNameN ?? "") : (element.cityName ?? ""), element.id.toString() ?? "");
}
}
citiesDropList
.add(new DropValue(element.id ?? 0, EasyLocalization.of(context)?.currentLocale?.countryCode == "SA" ? (element.cityNameN ?? "") : (element.cityName ?? ""), element.id.toString() ?? ""));
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
LocaleKeys.defineBranches.tr(),
),
),
body: Container(
width: double.infinity,
height: double.infinity,
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
children: [
country != null
? DropdownField(
(DropValue value) {
setState(() {
countryCode = value.subValue;
countryId = value.id;
fetchCites();
});
},
list: countryDropList,
dropdownValue: countryValue,
hint: LocaleKeys.chooseCountry.tr() + "*",
)
: CircularProgressIndicator(),
12.height,
if (countryId != -1)
cities != null
? citiesDropList.length == 0
? Text(LocaleKeys.no_city_available.tr())
: DropdownField(
(DropValue value) {
setState(() {
// countryCode = value.subValue;
cityId = value.id;
});
},
list: citiesDropList,
dropdownValue: cityValue,
hint: LocaleKeys.chooseCity.tr() + "*",
)
: CircularProgressIndicator(),
12.height,
if (cityId != -1)
Column(
children: [
TxtField(
hint: LocaleKeys.branchName.tr() + "*",
value: branchName,
onChanged: (v) {
branchName = v;
},
),
12.height,
TxtField(
hint: LocaleKeys.branchDescription.tr() + "*",
value: branchDescription,
onChanged: (v) {
branchDescription = v;
},
),
12.height,
TxtField(
hint: LocaleKeys.address.tr(),
isNeedClickAll: false,
maxLines: 5,
value: address,
onChanged: (v) {
address = v;
},
),
12.height,
ShowFillButton(
title: LocaleKeys.pickAddress.tr(),
onPressed: () {
navigateTo(
context,
PickLocationPage(
onPickAddress: (double latitude, double longitude, String address) {
setState(() {
this.latitude = latitude;
this.longitude = longitude;
this.address = address;
});
},
),
);
},
),
],
),
],
),
),
),
),
Padding(
padding: const EdgeInsets.all(12.0),
child: ShowFillButton(
title: widget.branchData.id == null ? LocaleKeys.createBranch.tr() : LocaleKeys.updateBranch.tr(),
onPressed: () async {
if (widget.branchData.id == null) {
Utils.showLoading(context);
MResponse res = await BranchApiClent().createBranch(branchName, branchDescription, cityId.toString(), address, latitude.toString(), longitude.toString());
Utils.hideLoading(context);
if (res.messageStatus == 1) {
Utils.showToast(LocaleKeys.branch_created.tr());
} else {
Utils.showToast(res.message ?? "");
}
} else {
Utils.showLoading(context);
MResponse res =
await BranchApiClent().updateBranch(widget.branchData.id ?? 0, branchName, branchDescription, cityId.toString(), address, latitude.toString(), longitude.toString());
Utils.hideLoading(context);
if (res.messageStatus == 1) {
Utils.showToast(LocaleKeys.branch_updated.tr());
} else {
Utils.showToast(res.message ?? "");
}
}
},
),
),
],
),
),
);
}
}