Files
Regroup/lib/Main_Screens/ProfileTab/Certificate/AddCertificate.dart
2024-08-20 20:01:30 +05:30

400 lines
18 KiB
Dart

import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart' hide MultipartFile, FormData;
import 'package:regroup/Common/base_manager.dart';
import 'package:regroup/Main_Screens/ProfileTab/view_model/profilePostmethod.dart';
import 'package:regroup/Utils/Common/CommonAppbar.dart';
import 'package:regroup/Utils/Common/CustomNextButton.dart';
import 'package:regroup/Utils/Common/CustomTextformfield.dart';
import 'package:regroup/Utils/Common/ImageUpload.dart';
import 'package:regroup/Utils/Common/sized_box.dart';
import 'package:regroup/Utils/dialogs.dart';
import 'package:regroup/Utils/texts.dart';
import 'package:regroup/resources/routes/route_name.dart';
import 'package:remove_emoji_input_formatter/remove_emoji_input_formatter.dart';
import 'package:path/path.dart' as path;
class AddCertificate extends StatefulWidget {
const AddCertificate({super.key});
@override
State<AddCertificate> createState() => _AddCertificateState();
}
class _AddCertificateState extends State<AddCertificate> {
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
TextEditingController certicationname = TextEditingController();
TextEditingController certificationreason = TextEditingController();
TextEditingController datecontroller = TextEditingController();
TextEditingController querycontroller = TextEditingController();
DateTime? _selectedDate;
Future<void> _selectDate(BuildContext context) async {
DateTime yesterday = DateTime.now().subtract(const Duration(days: 1));
DateTime eighteenYearsAgo =
DateTime.now().subtract(const Duration(days: 365 * 18));
final ThemeData customTheme = Theme.of(context).copyWith(
colorScheme: const ColorScheme.light(
primary: Color(0xFFD90B2E),
surfaceTint: Color(0xFF222935),
surface: Color(0xFF222935),
onPrimary: Colors.white,
onSurface: Colors.white,
onSecondary: Colors.white),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
foregroundColor: const Color(0xFFD90B2E),
),
));
final DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: yesterday,
firstDate: DateTime(1950),
lastDate: DateTime(2026),
builder: (BuildContext context, Widget? child) {
return Theme(
data: customTheme,
// ThemeData.light().copyWith(
// colorScheme: ColorScheme.dark(
// primary: Color(0XFF222935).withOpacity(0.60),
// onPrimary: Colors.white, // Change text color
// ),
// textButtonTheme: TextButtonThemeData(
// style: TextButton.styleFrom(
// foregroundColor: Colors.white, // Change button text color
// ),
// ),
// dialogBackgroundColor: Color(0XFF222935)
// .withOpacity(0.60), // Change dialog background color
// ),
child: child!,
);
},
);
if (pickedDate != null) {
// if (pickedDate.isBefore(eighteenYearsAgo)) {
setState(() {
_selectedDate = pickedDate;
datecontroller.text =
"${_selectedDate!.year.toString().padLeft(2, '0')}-${_selectedDate!.month.toString().padLeft(2, '0')}-${_selectedDate!.day.toString().padLeft(2, '0')}";
});
// }
}
}
List<File?> filePath = [];
bool isImageAdded = false;
UploadData() async {
utils.loader();
List<MultipartFile> certificatelist = [];
for (var file in filePath.where((file) => file != null)) {
certificatelist.add(
await MultipartFile.fromFile(
file!.path,
filename: path.basename(file.path),
),
);
}
FormData formdata = FormData.fromMap({
"certification_name": certicationname.text,
"certification_reason": certificationreason.text,
"certification_date": datecontroller.text,
"certification_image": certificatelist[0],
});
final data = await Profilepostmethod().postCertification(formdata);
if (data.status == ResponseStatus.SUCCESS) {
Get.back();
// Get.back();
Get.toNamed(RouteName.mainscreen, arguments: 4);
return utils.showToast(data.message);
} else {
Get.back();
return utils.showToast(data.message);
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: Scaffold(
// key: _scaffoldKey1,
backgroundColor: const Color(0xFF222935),
extendBody: true,
appBar: const CommonAppbar(
titleTxt: "Add certifications",
),
body: Stack(children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/png/Ellipse 1496.png"),
fit: BoxFit.fill)),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: SingleChildScrollView(
child: Form(
key: _formkey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
sizedBoxHeight(20.h),
text16400white("Certification name"),
sizedBoxHeight(16.h),
CustomTextFormField(
leadingIcon: SizedBox(
height: 17.h,
width: 18.w,
child: Center(
child: Image.asset(
"assets/images/png/Frame 24.png",
height: 17.h,
width: 18.w,
fit: BoxFit.cover,
),
),
),
validator: (value) {
if (value!.isEmpty) {
return 'Enter your certification name ';
}
return null;
},
inputFormatters: [
// LengthLimitingTextInputFormatter(20),
RemoveEmojiInputFormatter(),
FilteringTextInputFormatter.allow(
RegExp('[a-zA-Z ]'))
],
hintText: "Enter certification name",
textEditingController: certicationname,
),
sizedBoxHeight(10.h),
text16400white("Certification reason"),
sizedBoxHeight(16.h),
CustomTextFormField(
leadingIcon: SizedBox(
height: 17.h,
width: 18.w,
child: Center(
child: Image.asset(
"assets/images/png/Frame 24.png",
height: 17.h,
width: 18.w,
fit: BoxFit.cover,
),
),
),
validator: (value) {
if (value!.isEmpty) {
return 'Enter your certification reason ';
}
return null;
},
inputFormatters: [
// LengthLimitingTextInputFormatter(20),
RemoveEmojiInputFormatter(),
FilteringTextInputFormatter.allow(
RegExp('[a-zA-Z ]'))
],
hintText: "Enter certification reason",
textEditingController: certificationreason,
),
sizedBoxHeight(10.h),
text16400white("Certification date"),
sizedBoxHeight(16.h),
CustomTextFormField(
textEditingController: datecontroller,
readonly: true,
onTap: () {
_selectDate(context);
},
// texttype: TextInputType.text,
hintText: "Enter your certification date",
leadingIcon:
// const Icon(Icons.mail_outline),
Image.asset(
width: 22.w,
height: 17.h,
'assets/images/png/dateimage.png',
),
// validatorText: "Enter date of birth",
validator: (value) {
if (value!.isEmpty) {
return 'Enter your certification date';
}
return null;
},
),
sizedBoxHeight(10.h),
text16400white("Certification image"),
sizedBoxHeight(16.h),
filePath.isNotEmpty && isImageAdded
? Container(
height: 167.h,
width: double.infinity,
decoration: ShapeDecoration(
gradient: LinearGradient(
begin: const Alignment(0.98, -0.21),
end: const Alignment(-0.98, 0.21),
colors: [
Colors.white
.withOpacity(0.30000001192092896),
Colors.white
.withOpacity(0.2800000011920929)
],
),
shape: RoundedRectangleBorder(
side: const BorderSide(
width: 0.50,
color: Color(0xFF7E7E7E)),
borderRadius: BorderRadius.circular(10),
),
),
child: Stack(children: [
Image.file(
filePath[0]!,
fit: BoxFit.cover,
width: double.infinity,
),
Positioned(
// top: 0,
// left: 0,
right: 12,
bottom: 12,
child: GestureDetector(
onTap: () {
// Clear the list and show the upload button
filePath.clear();
isImageAdded = false;
setState(() {});
},
child: Container(
width: 27,
height: 27,
decoration: ShapeDecoration(
color: Colors.black,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
5)),
),
child: const Icon(
Icons.delete_outline_outlined,
color: Colors.white,
))),
),
// ),
]),
)
: GestureDetector(
// onTap: () {
// ImageUploadBottomSheet().showModal(
// context,
// false,
// (result) {
// var file = File(result);
// filePath.add(file);
// // isImageAdded = true;
// setState(() {
// isImageAdded = true;
// });
// },
// );
// },
onTap: () {
ImageUploadBottomSheet().showModal(
context,
false,
(result) {
var file = File(result);
filePath.add(file);
isImageAdded = true;
setState(() {
// Navigator.of(context).pop();
Get.back();
});
},
);
},
child: Container(
width: double.infinity,
height: 167,
decoration: ShapeDecoration(
gradient: LinearGradient(
begin: const Alignment(0.98, -0.21),
end: const Alignment(-0.98, 0.21),
colors: [
Colors.white
.withOpacity(0.30000001192092896),
Colors.white
.withOpacity(0.2800000011920929)
],
),
shape: RoundedRectangleBorder(
side: const BorderSide(
width: 0.50,
color: Color(0xFF7E7E7E)),
borderRadius: BorderRadius.circular(10),
),
),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.center,
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Image.asset(
'assets/images/png/onlycamera.png'),
sizedBoxHeight(5.h),
Text(
'Upload certification \nimage',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 14.sp,
fontFamily: 'Poppins',
fontWeight: FontWeight.w400,
),
),
],
),
),
),
sizedBoxHeight(50.h),
CustomButton(
onPressed: () {
// final isValid =
// _formkey.currentState?.validate();
if (certicationname.text.isBlank! &&
certificationreason.text.isBlank! &&
datecontroller.text.isBlank! &&
filePath.isEmpty) {
utils.showToast("Please fill all fields");
} else {
UploadData();
}
},
text: "Send")
]),
),
))
])),
);
}
}