QueenFruits/Mobile Operasional/lib/features/customer/presentation/screens/add_customer_screen.dart

215 lines
7.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:niogu_app/core/components/top_back_bar_app.dart';
import 'package:niogu_app/core/utils/log_message.dart';
import 'package:niogu_app/core/widgets/custom_snackbar.dart';
import 'package:niogu_app/core/widgets/custom_text_form_field.dart';
import 'package:niogu_app/features/customer/domain/entities/customer.dart';
import 'package:niogu_app/features/customer/presentation/providers/customer_provider.dart';
import 'package:sizer/sizer.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
class AddCustomerScreen extends ConsumerStatefulWidget {
final String? initialName;
final String? initialPhone;
const AddCustomerScreen({super.key, this.initialName, this.initialPhone});
@override
ConsumerState<AddCustomerScreen> createState() => _AddCustomerScreenState();
}
class _AddCustomerScreenState extends ConsumerState<AddCustomerScreen> {
final GlobalKey<FormState> _formKey = GlobalKey();
final TextEditingController _nameController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _phoneController = TextEditingController();
@override
void initState() {
super.initState();
_nameController.text = widget.initialName ?? '';
_phoneController.text = widget.initialPhone ?? '';
}
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
_phoneController.dispose();
super.dispose();
}
Future<void> _addCustomer() async {
if (!_formKey.currentState!.validate()) return;
final String name = _nameController.text;
final String? email = _emailController.text.isNotEmpty
? _emailController.text
: null;
final String? phoneNumber = _phoneController.text.isNotEmpty
? _phoneController.text
: null;
final customer = UpsertCustomer(
name: name,
email: email,
phoneNumber: phoneNumber,
);
try {
await ref
.read(customerControllerProvider.notifier)
.saveCustomer(customer);
if (!mounted) return;
CustomSnackbar.showSuccess(context, "Pelanggan berhasil disimpan");
context.pop();
} catch (e, st) {
LogMessage.log.e(e.toString(), error: e, stackTrace: st);
CustomSnackbar.showError(context, "Ups, terjadi kesalahan");
}
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final customerControllerState = ref.watch(customerControllerProvider);
return SafeArea(
top: false,
bottom: true,
right: false,
left: false,
child: Scaffold(
backgroundColor: Colors.white,
appBar: TopBackBarApp(
title: "Tambah Pelanggan",
onTap: () => context.pop(),
),
body: Column(
children: [
Expanded(
child: SingleChildScrollView(
padding: EdgeInsets.symmetric(
horizontal: 5.w,
vertical: 2.h,
),
child: Form(
key: _formKey,
child: Column(
children: [
Center(
child: Container(
width: 25.w,
height: 25.w,
decoration: BoxDecoration(
color: Colors.grey[100],
shape: BoxShape.circle,
border: Border.all(
color: Colors.grey.shade200,
width: 2,
),
),
child: Icon(
Icons.person_rounded,
size: 12.w,
color: Colors.grey[400],
),
),
),
SizedBox(height: 4.h),
CustomTextFormField(
label: "Nama Pelanggan",
controller: _nameController,
hint: "Masukkan nama",
validator: (value) {
if (value == null || value.isEmpty) {
return "Nama pelanggan belum diisi";
}
return null;
},
prefixIcon: Icons.person_outline,
),
SizedBox(height: 2.h),
CustomTextFormField(
label: "Email",
controller: _emailController,
hint: "Masukkan email (opsional)",
keyboardType: TextInputType.emailAddress,
prefixIcon: Icons.email_outlined,
),
SizedBox(height: 2.h),
CustomTextFormField(
label: "No. Handphone / WA",
controller: _phoneController,
hint: "Masukkan no handphone / wa (opsional)",
keyboardType: TextInputType.phone,
prefixIcon: Icons.phone_android_outlined,
),
],
),
),
),
),
Container(
padding: EdgeInsets.all(5.w),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 10,
offset: const Offset(0, -5),
),
],
),
child: SizedBox(
width: double.infinity,
height: 6.5.h,
child: ElevatedButton(
onPressed: customerControllerState.isLoading
? null
: _addCustomer,
style: ElevatedButton.styleFrom(
backgroundColor: AppColor.primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(2.5.w),
),
disabledBackgroundColor: Colors.grey.shade300,
),
child: Text(
"Simpan Pelanggan",
style: TextStyle(
color: Colors.white,
fontSize: AppFontSize.medium.sp,
fontWeight: FontWeight.bold,
),
),
),
),
),
],
),
),
);
},
);
}
}