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/extensions/string_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/app_bar.dart'; import 'package:car_provider_app/widgets/dropdown/dropdow_field.dart'; import 'package:car_provider_app/widgets/button/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 '../../../classes/colors.dart'; import '../../../generated/locale_keys.g.dart'; import '../../../models/profile/branch2.dart'; import '../../../utils/navigator.dart'; class DefineBranchPage extends StatefulWidget { ServiceProviderBranch? branchData; DefineBranchPage(this.branchData); @override State createState() => _DefineBranchPageState(); } class _DefineBranchPageState extends State { String countryCode = "", address = "", branchName = "", branchDescription = ""; double latitude = 0, longitude = 0; int role = -1, countryId = -1, cityId = -1; List countryDropList = []; List 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 != null) 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 != null) 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 != null) 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(context), body: Container( width: double.infinity, height: double.infinity, child: Column( children: [ Expanded( child: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(20.0), child: Column( children: [ LocaleKeys.defineBranches.tr().toText20(isBold: true), 12.height, LocaleKeys.branchLocation.tr().toText14(color: MyColors.lightTextColor), 20.height, LocaleKeys.chooseCountry.tr().toText14(color: MyColors.lightTextColor), 8.height, 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, InkWell( onTap: () { navigateTo( context, PickLocationPage( onPickAddress: (double latitude, double longitude, String address) { setState(() { this.latitude = latitude; this.longitude = longitude; this.address = address; }); }, ), ); }, child: Container( width: double.infinity, height: 45, decoration: BoxDecoration( color: Colors.transparent, border: Border.all(color: MyColors.darkPrimaryColor, width: 2), borderRadius: BorderRadius.all(Radius.circular(8)), ), child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon( Icons.place_outlined, size: 18, color: MyColors.darkPrimaryColor, ), 8.width, Text( LocaleKeys.pickAddress.tr(), style: TextStyle( color: MyColors.darkPrimaryColor, ), ), Icon( Icons.place_outlined, size: 18, color: Colors.transparent, ), ], ), ), ), ], ), ], ), ), ), ), Container( width: double.infinity, child: Padding( padding: const EdgeInsets.only(left: 20, right: 20, bottom: 20), child: ShowFillButton( title: widget.branchData != null ? (widget.branchData!.id == null ? LocaleKeys.createBranch.tr() : LocaleKeys.updateBranch.tr()) : LocaleKeys.createBranch.tr(), onPressed: () async { if (widget.branchData == 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()); pop(context); } 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()); pop(context); pop(context); } else { Utils.showToast(res.message ?? ""); } } }, ), ), ), ], ), ), ); } }