From e0b5213a3b8b8ada0d3c94f6528f1e257cb19c74 Mon Sep 17 00:00:00 2001 From: kicap Date: Thu, 18 May 2023 18:58:21 +0800 Subject: [PATCH] added add siswa and dana sosial --- android/app/src/main/AndroidManifest.xml | 1 + lib/app/app.router.dart | 2 +- lib/model/dana_sosial_model.dart | 40 +++++ lib/model/siswa_model.dart | 80 +++++++++ lib/services/http_services.dart | 4 +- lib/services/other_function.dart | 11 ++ .../add_siswa_dialog_view.dart | 97 ++++++++--- .../add_siswa_dialog_view_model.dart | 26 ++- .../dana_sosial_admin_view.dart | 63 +++++-- .../dana_sosial_admin_view_model.dart | 40 ++++- .../data_siswa/data_siswa_view.dart | 68 ++++---- .../data_siswa/data_siswa_view_model.dart | 45 ++++- .../tambah_dana_sosial_view.dart | 164 +++++++++--------- .../tambah_dana_sosial_view_model.dart | 39 ++++- 14 files changed, 519 insertions(+), 161 deletions(-) create mode 100644 lib/model/dana_sosial_model.dart create mode 100644 lib/model/siswa_model.dart create mode 100644 lib/services/other_function.dart diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 7404c42..a67e0fc 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ + ( - builder: (context) => _i5.TambahDanaSosialView(), + builder: (context) => const _i5.TambahDanaSosialView(), settings: data, maintainState: false, ); diff --git a/lib/model/dana_sosial_model.dart b/lib/model/dana_sosial_model.dart new file mode 100644 index 0000000..d71bf39 --- /dev/null +++ b/lib/model/dana_sosial_model.dart @@ -0,0 +1,40 @@ +class DanaSosialModel { + String? idDanaSosial; + String? keterangan; + String? jumlah; + String? tanggal; + String? jenis; + String? createdAt; + String? updatedAt; + + DanaSosialModel( + {this.idDanaSosial, + this.keterangan, + this.jumlah, + this.tanggal, + this.jenis, + this.createdAt, + this.updatedAt}); + + DanaSosialModel.fromJson(Map json) { + idDanaSosial = json['id_dana_sosial']; + keterangan = json['keterangan']; + jumlah = json['jumlah']; + tanggal = json['tanggal']; + jenis = json['jenis']; + createdAt = json['created_at']; + updatedAt = json['updated_at']; + } + + Map toJson() { + final Map data = {}; + data['id_dana_sosial'] = idDanaSosial; + data['keterangan'] = keterangan; + data['jumlah'] = jumlah; + data['tanggal'] = tanggal; + data['jenis'] = jenis; + data['created_at'] = createdAt; + data['updated_at'] = updatedAt; + return data; + } +} diff --git a/lib/model/siswa_model.dart b/lib/model/siswa_model.dart new file mode 100644 index 0000000..1fca23d --- /dev/null +++ b/lib/model/siswa_model.dart @@ -0,0 +1,80 @@ +class SiswaModel { + String? idSiswa; + String? nama; + String? tanggalLahir; + String? jenisKelamin; + String? alamat; + String? kemampuan; + String? imgUrl; + String? createdAt; + String? updatedAt; + String? tempatLahir; + String? noTelpon; + String? agama; + String? kewarganegaraan; + String? pendidikanSd; + String? pendidikanSmp; + String? pendidikanSma; + String? hobi; + + SiswaModel( + {this.idSiswa, + this.nama, + this.tanggalLahir, + this.jenisKelamin, + this.alamat, + this.kemampuan, + this.imgUrl, + this.createdAt, + this.updatedAt, + this.tempatLahir, + this.noTelpon, + this.agama, + this.kewarganegaraan, + this.pendidikanSd, + this.pendidikanSmp, + this.pendidikanSma, + this.hobi}); + + SiswaModel.fromJson(Map json) { + idSiswa = json['id_siswa']; + nama = json['nama']; + tanggalLahir = json['tanggal_lahir']; + jenisKelamin = json['jenis_kelamin']; + alamat = json['alamat']; + kemampuan = json['kemampuan']; + imgUrl = json['img_url']; + createdAt = json['created_at']; + updatedAt = json['updated_at']; + tempatLahir = json['tempat_lahir']; + noTelpon = json['no_telpon']; + agama = json['agama']; + kewarganegaraan = json['kewarganegaraan']; + pendidikanSd = json['pendidikan_sd']; + pendidikanSmp = json['pendidikan_smp']; + pendidikanSma = json['pendidikan_sma']; + hobi = json['hobi']; + } + + Map toJson() { + final Map data = {}; + data['id_siswa'] = idSiswa; + data['nama'] = nama; + data['tanggal_lahir'] = tanggalLahir; + data['jenis_kelamin'] = jenisKelamin; + data['alamat'] = alamat; + data['kemampuan'] = kemampuan; + data['img_url'] = imgUrl; + data['created_at'] = createdAt; + data['updated_at'] = updatedAt; + data['tempat_lahir'] = tempatLahir; + data['no_telpon'] = noTelpon; + data['agama'] = agama; + data['kewarganegaraan'] = kewarganegaraan; + data['pendidikan_sd'] = pendidikanSd; + data['pendidikan_smp'] = pendidikanSmp; + data['pendidikan_sma'] = pendidikanSma; + data['hobi'] = hobi; + return data; + } +} diff --git a/lib/services/http_services.dart b/lib/services/http_services.dart index 0544446..372c30e 100644 --- a/lib/services/http_services.dart +++ b/lib/services/http_services.dart @@ -7,8 +7,8 @@ class MyHttpServices { final log = getLogger('MyHttpServices'); final _options = BaseOptions( baseUrl: dotenv.env['api_url']!, - connectTimeout: const Duration(seconds: 60), - receiveTimeout: const Duration(seconds: 60), + connectTimeout: const Duration(seconds: 120), + receiveTimeout: const Duration(seconds: 120), ); late Dio _dio; diff --git a/lib/services/other_function.dart b/lib/services/other_function.dart new file mode 100644 index 0000000..b46b224 --- /dev/null +++ b/lib/services/other_function.dart @@ -0,0 +1,11 @@ +class OtherFunction { + int umur(String tanggalLahir) { + // change tanggalLahir to DateTime + DateTime date = DateTime.parse(tanggalLahir); + // get current date + DateTime now = DateTime.now(); + // get difference in year + int year = now.year - date.year; + return year; + } +} diff --git a/lib/ui/views/admin_index_tracking/add_siswa_dialog/add_siswa_dialog/add_siswa_dialog_view.dart b/lib/ui/views/admin_index_tracking/add_siswa_dialog/add_siswa_dialog/add_siswa_dialog_view.dart index 60e29fb..0b4fa6a 100644 --- a/lib/ui/views/admin_index_tracking/add_siswa_dialog/add_siswa_dialog/add_siswa_dialog_view.dart +++ b/lib/ui/views/admin_index_tracking/add_siswa_dialog/add_siswa_dialog/add_siswa_dialog_view.dart @@ -101,16 +101,6 @@ class AddSiswaDialogView extends StatelessWidget { ], ), const SizedBox(height: 10), - MyTextFormField( - labelText: 'NIS', - controller: model.nisController, - keyboardType: TextInputType.number, - validator: Validatorless.multiple([ - Validatorless.required('NIS tidak boleh kosong'), - Validatorless.number('NIS harus angka'), - ]), - ), - const SizedBox(height: 10), MyTextFormField( labelText: 'Nama', controller: model.namaController, @@ -128,6 +118,14 @@ class AddSiswaDialogView extends StatelessWidget { model.changeDate(context); }, ), + const SizedBox(height: 10), + MyTextFormField( + labelText: 'Tempat Lahir', + controller: model.tempatLahirController, + validator: Validatorless.required( + 'Tempat lahir tidak boleh kosong'), + ), + const SizedBox(height: 10), // create dropdown button Container( @@ -164,6 +162,36 @@ class AddSiswaDialogView extends StatelessWidget { ), ), const SizedBox(height: 10), + MyTextFormField( + labelText: 'No. Telepon', + controller: model.noTelponController, + keyboardType: TextInputType.number, + validator: Validatorless.multiple( + [ + Validatorless.required( + 'No. telepon tidak boleh kosong'), + Validatorless.number( + 'No. telepon harus berupa angka'), + ], + ), + ), + const SizedBox(height: 10), + MyTextFormField( + labelText: 'Agama', + controller: model.agamaController, + keyboardType: TextInputType.emailAddress, + validator: + Validatorless.required('Agama tidak boleh kosong'), + ), + const SizedBox(height: 10), + MyTextFormField( + labelText: 'Kewarganegaraan', + controller: model.kewarganegaraanController, + keyboardType: TextInputType.emailAddress, + validator: Validatorless.required( + 'Kewarganegaraan tidak boleh kosong'), + ), + const SizedBox(height: 10), MyTextFormField( labelText: 'Alamat', controller: model.alamatController, @@ -173,11 +201,40 @@ class AddSiswaDialogView extends StatelessWidget { ), const SizedBox(height: 10), MyTextFormField( - labelText: 'Keahlian', - controller: model.keahlianController, + labelText: 'Pendidikan SD', + controller: model.pendidikanSDController, + validator: Validatorless.required( + 'Pendidikan SD tidak boleh kosong'), + ), + const SizedBox(height: 10), + MyTextFormField( + labelText: 'Pendidikan SMP', + controller: model.pendidikanSMPController, + validator: Validatorless.required( + 'Pendidikan SMP tidak boleh kosong'), + ), + const SizedBox(height: 10), + MyTextFormField( + labelText: 'Pendidikan SMA', + controller: model.pendidikanSMAController, + validator: Validatorless.required( + 'Pendidikan SMA tidak boleh kosong'), + ), + const SizedBox(height: 10), + MyTextFormField( + labelText: 'Kemampuan', + controller: model.kemampuanController, maxLines: 4, validator: Validatorless.required( - 'Keahlian tidak boleh kosong'), + 'Kemampuan tidak boleh kosong'), + ), + const SizedBox(height: 10), + MyTextFormField( + labelText: "Hobi", + controller: model.hobiController, + maxLines: 4, + validator: + Validatorless.required('Hobi tidak boleh kosong'), ), Row( mainAxisAlignment: MainAxisAlignment.end, @@ -200,13 +257,13 @@ class AddSiswaDialogView extends StatelessWidget { if (model.formKey.currentState!.validate()) { bool res = await model.postData(); model.log.i("res: $res"); - // if (res) { - // completer( - // DialogResponse( - // confirmed: true, - // ), - // ); - // } + if (res) { + completer( + DialogResponse( + confirmed: true, + ), + ); + } } }, child: const Text( diff --git a/lib/ui/views/admin_index_tracking/add_siswa_dialog/add_siswa_dialog/add_siswa_dialog_view_model.dart b/lib/ui/views/admin_index_tracking/add_siswa_dialog/add_siswa_dialog/add_siswa_dialog_view_model.dart index 2bae6b7..124bf88 100644 --- a/lib/ui/views/admin_index_tracking/add_siswa_dialog/add_siswa_dialog/add_siswa_dialog_view_model.dart +++ b/lib/ui/views/admin_index_tracking/add_siswa_dialog/add_siswa_dialog/add_siswa_dialog_view_model.dart @@ -4,10 +4,10 @@ import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_holo_date_picker/flutter_holo_date_picker.dart'; import 'package:image_picker/image_picker.dart'; -import 'package:panti_asuhan/app/app.locator.dart'; -import 'package:panti_asuhan/app/core/custom_base_view_model.dart'; +import '../../../../../app/app.locator.dart'; import '../../../../../app/app.logger.dart'; +import '../../../../../app/core/custom_base_view_model.dart'; import '../../../../../services/http_services.dart'; import '../../../../../services/my_easyloading.dart'; @@ -28,11 +28,18 @@ class AddSiswaDialogViewModel extends CustomBaseViewModel { // form and text controller final formKey = GlobalKey(); - TextEditingController nisController = TextEditingController(); TextEditingController namaController = TextEditingController(); TextEditingController tanggalLahirController = TextEditingController(); + TextEditingController tempatLahirController = TextEditingController(); TextEditingController alamatController = TextEditingController(); - TextEditingController keahlianController = TextEditingController(); + TextEditingController noTelponController = TextEditingController(); + TextEditingController agamaController = TextEditingController(); + TextEditingController kewarganegaraanController = TextEditingController(); + TextEditingController pendidikanSDController = TextEditingController(); + TextEditingController pendidikanSMPController = TextEditingController(); + TextEditingController pendidikanSMAController = TextEditingController(); + TextEditingController kemampuanController = TextEditingController(); + TextEditingController hobiController = TextEditingController(); Future init() async {} @@ -79,12 +86,19 @@ class AddSiswaDialogViewModel extends CustomBaseViewModel { easyLoading.customLoading('Menambahkan data...'); try { var formData = FormData.fromMap({ - 'nis': nisController.text, 'nama': namaController.text, 'jenis_kelamin': jenisKelamin, 'tanggal_lahir': tanggalLahirController.text, + 'tempat_lahir': tempatLahirController.text, 'alamat': alamatController.text, - 'keahlian': keahlianController.text, + 'no_telpon': noTelponController.text, + 'agama': agamaController.text, + 'kewarganegaraan': kewarganegaraanController.text, + 'pendidikan_sd': pendidikanSDController.text, + 'pendidikan_smp': pendidikanSMPController.text, + 'pendidikan_sma': pendidikanSMAController.text, + 'kemampuan': kemampuanController.text, + 'hobi': hobiController.text, 'foto': await MultipartFile.fromFile(_imagePath!), }); diff --git a/lib/ui/views/admin_index_tracking/dana_sosial_admin/dana_sosial_admin_view.dart b/lib/ui/views/admin_index_tracking/dana_sosial_admin/dana_sosial_admin_view.dart index d12eddd..657cbf3 100644 --- a/lib/ui/views/admin_index_tracking/dana_sosial_admin/dana_sosial_admin_view.dart +++ b/lib/ui/views/admin_index_tracking/dana_sosial_admin/dana_sosial_admin_view.dart @@ -88,27 +88,52 @@ class DanaSosialAdminView extends StatelessWidget { ), ], ), - child: ListView.builder( - padding: const EdgeInsets.symmetric( - horizontal: 15, vertical: 10), - itemCount: 20, - itemBuilder: (context, index) { - return Card( - child: GestureDetector( - onTap: () { - model.log.i('Card $index tapped'); - }, - child: ListTile( - title: Text('1/02/15 - 10.00 am', - style: boldTextStyle.copyWith( - fontSize: 13, color: mainColor)), - subtitle: Text('Progress $index'), - trailing: Text('Pembangunan $index'), + child: (model.danaSosialModelList.isEmpty) + ? Center( + child: Text( + 'Tidak ada data', + style: boldTextStyle.copyWith( + fontSize: 20, + ), ), + ) + : ListView.builder( + padding: const EdgeInsets.symmetric( + horizontal: 15, vertical: 10), + itemCount: model.danaSosialModelList.length, + itemBuilder: (context, index) { + return Card( + child: ListTile( + title: Text( + model.danaSosialModelList[index].tanggal ?? + '', + style: boldTextStyle.copyWith( + fontSize: 13, color: mainColor)), + subtitle: Text( + 'Rp. ${model.danaSosialModelList[index].jumlah}', + style: regularTextStyle.copyWith( + fontSize: 13, color: mainColor)), + trailing: Container( + width: 50, + height: 50, + decoration: BoxDecoration( + color: mainColor, + borderRadius: BorderRadius.circular(50), + ), + child: IconButton( + onPressed: () { + // model.goToTambahDanaSosial(); + }, + icon: const Icon( + Icons.edit, + color: Colors.white, + ), + ), + ), + ), + ); + }, ), - ); - }, - ), ), ), ], diff --git a/lib/ui/views/admin_index_tracking/dana_sosial_admin/dana_sosial_admin_view_model.dart b/lib/ui/views/admin_index_tracking/dana_sosial_admin/dana_sosial_admin_view_model.dart index ec0dbfe..9b680bf 100644 --- a/lib/ui/views/admin_index_tracking/dana_sosial_admin/dana_sosial_admin_view_model.dart +++ b/lib/ui/views/admin_index_tracking/dana_sosial_admin/dana_sosial_admin_view_model.dart @@ -1,10 +1,48 @@ +import '../../../../app/app.locator.dart'; import '../../../../app/app.logger.dart'; import '../../../../app/app.router.dart'; import '../../../../app/core/custom_base_view_model.dart'; +import '../../../../model/dana_sosial_model.dart'; +import '../../../../services/http_services.dart'; +import '../../../../services/my_easyloading.dart'; class DanaSosialAdminViewModel extends CustomBaseViewModel { final log = getLogger('DanaSosialAdminViewModel'); - Future init() async {} + final _httpService = locator(); + final easyLoading = locator(); + + List danaSosialModelList = []; + + Future init() async { + await getData(); + } + + getData() async { + setBusy(true); + easyLoading.showLoading(); + try { + var response = await _httpService.get('dana_sosial'); + log.i(response.data); + danaSosialModelList = []; + + var datanya = response.data['data']; + // log.i(datanya.length); + if (datanya.length > 0) { + for (var item in datanya) { + danaSosialModelList.add(DanaSosialModel.fromJson(item)); + } + } + + setBusy(false); + notifyListeners(); + log.i(danaSosialModelList); + } catch (e) { + log.e(e); + setBusy(false); + } finally { + easyLoading.dismissLoading(); + } + } goToTambahDanaSosial() { navigationService.navigateTo(Routes.tambahDanaSosialView); diff --git a/lib/ui/views/admin_index_tracking/data_siswa/data_siswa_view.dart b/lib/ui/views/admin_index_tracking/data_siswa/data_siswa_view.dart index eb39c74..49252bf 100644 --- a/lib/ui/views/admin_index_tracking/data_siswa/data_siswa_view.dart +++ b/lib/ui/views/admin_index_tracking/data_siswa/data_siswa_view.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:panti_asuhan/services/other_function.dart'; import 'package:stacked/stacked.dart'; import '../../../../app/themes/app_colors.dart'; @@ -75,38 +76,45 @@ class DataSiswaView extends StatelessWidget { ), ], ), - child: ListView.builder( - padding: const EdgeInsets.symmetric( - horizontal: 15, vertical: 10), - itemCount: 20, - itemBuilder: (context, index) { - return Card( - child: GestureDetector( - onTap: () { - model.log.i('Card $index tapped'); + child: (model.siswaModelList.isEmpty) + ? Center( + child: Text( + 'Tidak ada data', + style: boldTextStyle.copyWith( + fontSize: 20, + ), + ), + ) + : // ListView.builder( + ListView.builder( + padding: const EdgeInsets.symmetric( + horizontal: 15, vertical: 10), + itemCount: model.siswaModelList.length, + itemBuilder: (context, index) { + return Card( + child: ListTile( + title: Text( + model.siswaModelList[index].nama ?? '', + style: boldTextStyle.copyWith( + fontSize: 13, color: mainColor)), + subtitle: Text( + 'Umur : ${OtherFunction().umur(model.siswaModelList[index].tanggalLahir ?? '')}'), + // circle avatar + trailing: Container( + width: 50, + height: 50, + decoration: BoxDecoration( + color: mainColor, + borderRadius: BorderRadius.circular(50), + ), + child: const Icon( + Icons.person, + color: Colors.white, + ), + )), + ); }, - child: ListTile( - title: Text('Namanya', - style: boldTextStyle.copyWith( - fontSize: 13, color: mainColor)), - subtitle: Text('Umurnya : $index'), - // circle avatar - trailing: Container( - width: 50, - height: 50, - decoration: BoxDecoration( - color: mainColor, - borderRadius: BorderRadius.circular(50), - ), - child: const Icon( - Icons.person, - color: Colors.white, - ), - )), ), - ); - }, - ), ), ), ], diff --git a/lib/ui/views/admin_index_tracking/data_siswa/data_siswa_view_model.dart b/lib/ui/views/admin_index_tracking/data_siswa/data_siswa_view_model.dart index fd5d3b6..6a9c311 100644 --- a/lib/ui/views/admin_index_tracking/data_siswa/data_siswa_view_model.dart +++ b/lib/ui/views/admin_index_tracking/data_siswa/data_siswa_view_model.dart @@ -1,12 +1,49 @@ -import 'package:panti_asuhan/app/core/custom_base_view_model.dart'; - import '../../../../app/app.dialogs.dart'; +import '../../../../app/app.locator.dart'; import '../../../../app/app.logger.dart'; +import '../../../../app/core/custom_base_view_model.dart'; +import '../../../../model/siswa_model.dart'; +import '../../../../services/http_services.dart'; +import '../../../../services/my_easyloading.dart'; import '../add_siswa_dialog/add_siswa_dialog/add_siswa_dialog_view.dart'; class DataSiswaViewModel extends CustomBaseViewModel { final log = getLogger('DataSiswaViewModel'); - Future init() async {} + final _httpService = locator(); + final easyLoading = locator(); + + List siswaModelList = []; + + Future init() async { + await getData(); + } + + getData() async { + setBusy(true); + easyLoading.showLoading(); + try { + var response = await _httpService.get('siswa'); + log.i(response.data); + siswaModelList = []; + + var datanya = response.data['data']; + // log.i(datanya.length); + if (datanya.length > 0) { + for (var item in datanya) { + siswaModelList.add(SiswaModel.fromJson(item)); + } + } + + setBusy(false); + notifyListeners(); + log.i(siswaModelList); + } catch (e) { + log.e(e); + setBusy(false); + } finally { + easyLoading.dismissLoading(); + } + } void addSiswa() async { final res = await dialogService.showCustomDialog( @@ -15,5 +52,7 @@ class DataSiswaViewModel extends CustomBaseViewModel { ); if (res?.confirmed != true) return; + siswaModelList = []; + await getData(); } } diff --git a/lib/ui/views/tambah_dana_sosial/tambah_dana_sosial_view.dart b/lib/ui/views/tambah_dana_sosial/tambah_dana_sosial_view.dart index 6b84d96..73f7fa8 100644 --- a/lib/ui/views/tambah_dana_sosial/tambah_dana_sosial_view.dart +++ b/lib/ui/views/tambah_dana_sosial/tambah_dana_sosial_view.dart @@ -40,89 +40,97 @@ class TambahDanaSosialView extends StatelessWidget { padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15), child: Form( key: model.formKey, - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "Keterangan", - style: regularTextStyle.copyWith(color: mainColor), - ), - MyTextFormField( - hintText: "Keterangan", - controller: model.ketController, - maxLines: 3, - validator: Validatorless.required( - 'Keterangan tidak boleh kosong'), - ), - const SizedBox(height: 20), - Text( - "Jumlah (Rp. )", - style: regularTextStyle.copyWith(color: mainColor), - ), - MyTextFormField( - hintText: "Jumlah (Rp. )", - keyboardType: TextInputType.number, - controller: model.jumlahController, - validator: Validatorless.multiple( - [ - Validatorless.required('Jumlah tidak boleh kosong'), - Validatorless.number('Jumlah harus angka'), - ], + child: SingleChildScrollView( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "Keterangan", + style: regularTextStyle.copyWith(color: mainColor), ), - ), - const SizedBox(height: 20), - Text( - "Tanggal", - style: regularTextStyle.copyWith(color: mainColor), - ), - MyTextFormField( - hintText: 'Tanggal', - readOnly: true, - validator: Validatorless.required( - 'Tanggal lahir tidak boleh kosong'), - onTap: () { - model.changeDate(context); - }, - ), - const SizedBox(height: 20), - Text( - "Jenis Dana", - style: regularTextStyle.copyWith(color: mainColor), - ), - Container( - width: double.infinity, - height: 60, - padding: const EdgeInsets.symmetric(horizontal: 20), - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(25), - border: Border.all( - color: mainColor, + MyTextFormField( + hintText: "Keterangan", + controller: model.ketController, + maxLines: 3, + validator: Validatorless.required( + 'Keterangan tidak boleh kosong'), + ), + const SizedBox(height: 20), + Text( + "Jumlah (Rp. )", + style: regularTextStyle.copyWith(color: mainColor), + ), + MyTextFormField( + hintText: "Jumlah (Rp. )", + keyboardType: TextInputType.number, + controller: model.jumlahController, + validator: Validatorless.multiple( + [ + Validatorless.required('Jumlah tidak boleh kosong'), + Validatorless.number('Jumlah harus angka'), + ], ), ), - child: DropdownButtonHideUnderline( - child: DropdownButton( - value: model.jenisDana, - onChanged: (String? newValue) { - model.jenisDana = newValue!; - }, - items: model.jenisDanaList - .map>((String value) { - return DropdownMenuItem( - value: value, - child: Text(value, - style: const TextStyle(fontSize: 16)), - ); - }).toList(), + const SizedBox(height: 20), + Text( + "Tanggal", + style: regularTextStyle.copyWith(color: mainColor), + ), + MyTextFormField( + hintText: 'Tanggal', + readOnly: true, + controller: model.tanggalController, + validator: Validatorless.required( + 'Tanggal lahir tidak boleh kosong'), + onTap: () { + model.changeDate(context); + }, + ), + const SizedBox(height: 20), + Text( + "Jenis Dana", + style: regularTextStyle.copyWith(color: mainColor), + ), + Container( + width: double.infinity, + height: 60, + padding: const EdgeInsets.symmetric(horizontal: 20), + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(25), + border: Border.all( + color: mainColor, + ), + ), + child: DropdownButtonHideUnderline( + child: DropdownButton( + value: model.jenisDana, + onChanged: (String? newValue) { + model.jenisDana = newValue!; + }, + items: model.jenisDanaList + .map>((String value) { + return DropdownMenuItem( + value: value, + child: Text(value, + style: const TextStyle(fontSize: 16)), + ); + }).toList(), + ), ), ), - ), - const SizedBox(height: 20), - MyButton( - text: "Simpan", - onPressed: () {}, - ), - ], + const SizedBox(height: 20), + MyButton( + text: "Simpan", + onPressed: () { + if (model.formKey.currentState!.validate()) { + model.log.i('Form Valid'); + model.addData(); + } + }, + ), + ], + ), ), ), ), diff --git a/lib/ui/views/tambah_dana_sosial/tambah_dana_sosial_view_model.dart b/lib/ui/views/tambah_dana_sosial/tambah_dana_sosial_view_model.dart index 216a889..2167446 100644 --- a/lib/ui/views/tambah_dana_sosial/tambah_dana_sosial_view_model.dart +++ b/lib/ui/views/tambah_dana_sosial/tambah_dana_sosial_view_model.dart @@ -1,7 +1,18 @@ +import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; -import 'package:panti_asuhan/app/core/custom_base_view_model.dart'; + +import '../../../app/app.locator.dart'; +import '../../../app/app.logger.dart'; +import '../../../app/app.router.dart'; +import '../../../app/core/custom_base_view_model.dart'; +import '../../../services/http_services.dart'; +import '../../../services/my_easyloading.dart'; class TambahDanaSosialViewModel extends CustomBaseViewModel { + final log = getLogger('TambahDanaSosialViewModel'); + final _httpService = locator(); + final easyLoading = locator(); + String jenisDana = 'Pemasukan'; List jenisDanaList = ['Pemasukan', 'Pengeluaran']; @@ -28,4 +39,30 @@ class TambahDanaSosialViewModel extends CustomBaseViewModel { tanggalController.text = date; } } + + void addData() async { + easyLoading.customLoading('Menambahkan data...'); + try { + var formData = FormData.fromMap({ + 'jenis': jenisDana, + 'keterangan': ketController.text, + 'jumlah': jumlahController.text, + 'tanggal': tanggalController.text, + }); + var response = + await _httpService.postWithFormData('dana_sosial', formData); + log.i(response.data); + easyLoading.showSuccess(" Data berhasil ditambahkan"); + navigationService.navigateTo(Routes.adminIndexTrackingView); + } catch (e) { + log.e(e); + snackbarService.showSnackbar( + message: 'Gagal menambahkan data', + title: 'Error', + duration: const Duration(seconds: 4), + ); + } finally { + easyLoading.dismissLoading(); + } + } }