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.
290 lines
12 KiB
Dart
290 lines
12 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:car_provider_app/view_models/items_view_model.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:mc_common_app/extensions/int_extensions.dart';
|
|
import 'package:mc_common_app/extensions/string_extensions.dart';
|
|
import 'package:mc_common_app/models/m_response.dart';
|
|
import 'package:mc_common_app/models/model/branch2.dart';
|
|
import 'package:mc_common_app/models/services/item_model.dart';
|
|
import 'package:mc_common_app/theme/colors.dart';
|
|
import 'package:mc_common_app/utils/AppPermissionHandler.dart';
|
|
import 'package:mc_common_app/utils/date_helper.dart';
|
|
import 'package:mc_common_app/utils/navigator.dart';
|
|
import 'package:mc_common_app/utils/utils.dart';
|
|
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
|
|
import 'package:mc_common_app/widgets/checkbox_with_title_desc.dart';
|
|
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
|
|
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
|
|
import 'package:mc_common_app/widgets/txt_field.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class CreateItemPage extends StatefulWidget {
|
|
const CreateItemPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<CreateItemPage> createState() => _CreateItemPageState();
|
|
}
|
|
|
|
class _CreateItemPageState extends State<CreateItemPage> {
|
|
String? name, description, price, year, itemImage;
|
|
bool isAppointmentAvailable = false;
|
|
bool isWorkshopAppointmentAvailable = false;
|
|
bool isHomeAppointmentAvailable = false;
|
|
bool isDefaultValudDone = false;
|
|
|
|
ItemsVM? model;
|
|
ItemData? itemData;
|
|
|
|
setDefaultData() {
|
|
name = itemData!.name;
|
|
description = itemData!.description;
|
|
price = itemData!.price;
|
|
print(itemData!.manufactureDate);
|
|
|
|
//TODO: need to discuss with zahoor year and picture
|
|
if (itemData!.manufactureDate != null) year = DateHelper.formatAsYearMonthDay(DateHelper.parseStringToDate(itemData!.manufactureDate ?? DateTime.now().toString()));
|
|
// itemImage=itemData.
|
|
|
|
isAppointmentAvailable = itemData!.isAllowAppointment ?? false;
|
|
isWorkshopAppointmentAvailable = itemData!.isAppointmentCompanyLoc ?? false;
|
|
isHomeAppointmentAvailable = itemData!.isAppointmentCustomerLoc ?? false;
|
|
isDefaultValudDone = true;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
model ??= context.read<ItemsVM>();
|
|
itemData ??= ModalRoute.of(context)!.settings.arguments as ItemData;
|
|
if (!isDefaultValudDone) setDefaultData();
|
|
return Scaffold(
|
|
appBar: const CustomAppBar(
|
|
title: "Add Items",
|
|
),
|
|
body: Container(
|
|
width: double.infinity,
|
|
height: double.infinity,
|
|
padding: const EdgeInsets.all(20),
|
|
child: Column(
|
|
children: [
|
|
Expanded(
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
children: [
|
|
TxtField(
|
|
hint: "Item Name",
|
|
value: name,
|
|
onChanged: (v) {
|
|
name = v;
|
|
},
|
|
),
|
|
12.height,
|
|
TxtField(
|
|
hint: "Item Description",
|
|
value: description,
|
|
onChanged: (v) {
|
|
description = v;
|
|
},
|
|
),
|
|
12.height,
|
|
TxtField(
|
|
hint: "Item Price",
|
|
value: price,
|
|
keyboardType: TextInputType.number,
|
|
onChanged: (v) {
|
|
price = v;
|
|
},
|
|
),
|
|
12.height,
|
|
TxtField(
|
|
hint: "Manufacture Year",
|
|
value: year,
|
|
keyboardType: TextInputType.number,
|
|
isNeedClickAll: true,
|
|
postfixWidget: const IconButton(
|
|
onPressed: null,
|
|
icon: Icon(Icons.date_range),
|
|
),
|
|
onTap: () async {
|
|
year = await Utils.pickDateFromDatePicker(context, firstDate: DateTime(1990), lastDate: DateTime.now());
|
|
setState(() {});
|
|
},
|
|
onChanged: (v) {
|
|
year = v;
|
|
},
|
|
),
|
|
12.height,
|
|
if (itemImage != null && itemImage!.isNotEmpty)
|
|
Column(
|
|
children: [
|
|
Image.memory(
|
|
base64Decode(itemImage ?? ""),
|
|
).toContainer(
|
|
isEnabledBorder: true,
|
|
paddingAll: 12,
|
|
),
|
|
12.height,
|
|
],
|
|
),
|
|
InkWell(
|
|
onTap: () async {
|
|
bool isPermissionsAvailable = await requestPermissionGranted(context, Permission.storage);
|
|
if (isPermissionsAvailable && model != null) {
|
|
itemImage = await model!.selectFile() ?? "";
|
|
setState(() {});
|
|
}
|
|
},
|
|
child: Container(
|
|
width: double.infinity,
|
|
height: 45,
|
|
decoration: BoxDecoration(
|
|
color: Colors.transparent,
|
|
border: Border.all(color: MyColors.greyACColor, width: 2),
|
|
borderRadius: const BorderRadius.all(Radius.circular(0)),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
const Icon(
|
|
Icons.attach_file,
|
|
size: 18,
|
|
color: MyColors.darkPrimaryColor,
|
|
),
|
|
8.width,
|
|
const Text(
|
|
"Attach Item Image",
|
|
style: TextStyle(
|
|
color: MyColors.darkPrimaryColor,
|
|
),
|
|
),
|
|
const Icon(
|
|
Icons.attach_file,
|
|
size: 18,
|
|
color: Colors.transparent,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
20.height,
|
|
CheckBoxWithTitleDescription(
|
|
isSelected: isAppointmentAvailable,
|
|
title: 'Available for appointment',
|
|
description: 'This option will allow customer to book appointment for these services',
|
|
onSelection: (bool v) {
|
|
setState(() {
|
|
isAppointmentAvailable = v;
|
|
// isWorkshopAppointmentAvailable = v;
|
|
});
|
|
},
|
|
),
|
|
12.height,
|
|
if (isAppointmentAvailable)
|
|
CheckBoxWithTitleDescription(
|
|
isSelected: isWorkshopAppointmentAvailable,
|
|
title: 'Allow Workshop service',
|
|
description: 'This option will show to customer that you can avail this service on workshop or not.',
|
|
onSelection: (bool v) {
|
|
setState(() {
|
|
isWorkshopAppointmentAvailable = v;
|
|
});
|
|
},
|
|
),
|
|
12.height,
|
|
if (isAppointmentAvailable)
|
|
CheckBoxWithTitleDescription(
|
|
isSelected: isHomeAppointmentAvailable,
|
|
title: 'Allow home services',
|
|
description: 'This option will allow customer to book appointment at their desired location',
|
|
onSelection: (bool v) {
|
|
setState(() {
|
|
isHomeAppointmentAvailable = v;
|
|
});
|
|
},
|
|
),
|
|
12.height,
|
|
],
|
|
),
|
|
),
|
|
),
|
|
ShowFillButton(
|
|
title: (itemData!.isUpdate ?? false) ? "Update Item" : "Create Item",
|
|
maxWidth: double.infinity,
|
|
onPressed: () async {
|
|
if (validation()) {
|
|
if (!(itemData?.isUpdate ?? false)) {
|
|
Map map = {
|
|
"name": name,
|
|
"price": price,
|
|
"description": description,
|
|
"itemImage": itemImage ?? "",
|
|
"companyID": 1,
|
|
"manufactureDate": year,
|
|
"serviceProviderServiceID": itemData!.serviceProviderServiceId,
|
|
"isActive": true,
|
|
"isAllowAppointment": isAppointmentAvailable,
|
|
"isAppointmentCompanyLoc": isWorkshopAppointmentAvailable,
|
|
"isAppointmentCustomerLoc": isHomeAppointmentAvailable
|
|
};
|
|
Utils.showLoading(context);
|
|
MResponse mResponse = await model!.createServiceItem(map);
|
|
Utils.hideLoading(context);
|
|
if (mResponse.messageStatus == 1) {
|
|
model!.getServiceItems(itemData!.serviceProviderServiceId ?? 0);
|
|
}
|
|
Utils.showToast(mResponse.message ?? "");
|
|
pop(context);
|
|
} else {
|
|
Map map = {
|
|
"id": itemData!.id,
|
|
"name": name,
|
|
"price": price,
|
|
"description": description,
|
|
"itemImage": itemImage ?? "",
|
|
"companyID": 1,
|
|
"manufactureDate": year,
|
|
"serviceProviderServiceID": itemData!.serviceProviderServiceId,
|
|
"isActive": true,
|
|
"isAllowAppointment": isAppointmentAvailable,
|
|
"isAppointmentCompanyLoc": isWorkshopAppointmentAvailable,
|
|
"isAppointmentCustomerLoc": isHomeAppointmentAvailable
|
|
};
|
|
Utils.showLoading(context);
|
|
MResponse mResponse = await model!.updateServiceItem(map);
|
|
Utils.hideLoading(context);
|
|
if (mResponse.messageStatus == 1) {
|
|
model!.getServiceItems(itemData!.serviceProviderServiceId ?? 0);
|
|
}
|
|
Utils.showToast(mResponse.message ?? "");
|
|
pop(context);
|
|
}
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
bool validation() {
|
|
bool valid = true;
|
|
if (name == null || name!.length < 3) {
|
|
Utils.showToast("Please add valid item name");
|
|
valid = false;
|
|
} else if (description == null || description!.length < 3) {
|
|
Utils.showToast("Please add valid item description");
|
|
valid = false;
|
|
} else if (price == null) {
|
|
Utils.showToast("Please add valid item price");
|
|
valid = false;
|
|
} else if (year == null) {
|
|
Utils.showToast("Please add valid year");
|
|
valid = false;
|
|
}
|
|
return valid;
|
|
}
|
|
}
|