QueenFruits/Mobile Operasional/lib/features/outlets/presentation/screens/delete_outlet_screen.dart

270 lines
8.1 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/constants/app_font_size.dart';
import 'package:niogu_app/core/providers/app_provider.dart';
import 'package:niogu_app/core/utils/log_message.dart';
import 'package:niogu_app/core/system/system_setting.dart';
import 'package:niogu_app/core/widgets/custom_snackbar.dart';
import 'package:niogu_app/features/outlets/presentation/providers/outlet_provider.dart';
import 'package:sizer/sizer.dart';
class DeleteOutletScreen extends ConsumerStatefulWidget {
final String id;
final String name;
final int transactionAmount;
const DeleteOutletScreen({
super.key,
required this.id,
required this.name,
required this.transactionAmount,
});
@override
ConsumerState<DeleteOutletScreen> createState() => _DeleteOutletScreenState();
}
class _DeleteOutletScreenState extends ConsumerState<DeleteOutletScreen> {
late String name;
late int transactionAmount;
@override
void initState() {
// TODO: implement initState
super.initState();
name = widget.name;
transactionAmount = widget.transactionAmount;
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
Future<void> _onDeleteOutlet() async {
try {
await ref.read(outletControllerProvider.notifier).deleteOutlet(widget.id);
if (!mounted) return;
final currentOutletId = await ref.read(currentOutletIdProvider);
if (currentOutletId == widget.id) {
final outlet = await ref.read(outletRepositoryProvider).getMainOutlet();
await SystemSetting.switchOutlet(
outletId: outlet.id,
outletName: outlet.name,
);
ref.read(currentOutletIdProvider.notifier).state = outlet.id;
ref.read(currentOutletNameProvider.notifier).state = outlet.name;
}
CustomSnackbar.showSuccess(context, 'Outlet berhasil dihapus');
context.pop();
context.pop();
context.pop();
} catch (e, st) {
LogMessage.log.e(e.toString(), error: e, stackTrace: st);
CustomSnackbar.showError(context, "Ups, terjadi kesalahan");
context.pop();
context.pop();
context.pop();
}
}
@override
Widget build(BuildContext context) {
final bool isTablet = 100.w >= 600;
final double maxWidth = 100.w >= 1280
? 1200
: isTablet
? 800
: 400;
final outletControllerState = ref.watch(outletControllerProvider);
return Dialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.w)),
elevation: 0,
backgroundColor: Colors.transparent,
child: Container(
padding: EdgeInsets.all(5.w),
width: double.infinity,
constraints: BoxConstraints(maxWidth: maxWidth),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(4.w),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: EdgeInsets.all(3.w),
decoration: BoxDecoration(
color: Colors.red[50],
shape: BoxShape.circle,
),
child: Icon(Icons.delete_outline, color: Colors.red, size: 8.w),
),
SizedBox(height: 2.h),
Text(
"Hapus Outlet?",
style: TextStyle(
fontSize: AppFontSize.medium.sp,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
SizedBox(height: 1.h),
Text(
"Tindakan ini tidak dapat dibatalkan.",
style: TextStyle(
fontSize: isTablet
? AppFontSize.medium.sp
: AppFontSize.small.sp,
color: Colors.grey[800],
),
),
SizedBox(height: 3.h),
Container(
padding: EdgeInsets.symmetric(vertical: 3.w),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(2.5.w),
),
child: Row(
children: [
Container(
height: 15.w,
width: 15.w,
decoration: BoxDecoration(
color: Colors.grey.shade100,
borderRadius: BorderRadius.circular(2.5.w),
),
child: Icon(
Icons.store_rounded,
color: Colors.grey.shade700,
size: 8.w,
),
),
SizedBox(width: 3.w),
// Detail Text
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: isTablet
? AppFontSize.medium.sp
: AppFontSize.small.sp,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
SizedBox(height: 0.5.h),
// Badge Terjual
Container(
padding: EdgeInsets.symmetric(
horizontal: 2.w,
vertical: 0.5.h,
),
decoration: BoxDecoration(
color: Colors.green[50],
borderRadius: BorderRadius.circular(1.w),
),
child: Text(
"$transactionAmount Transaksi",
style: TextStyle(
fontSize: isTablet
? (AppFontSize.medium - 2).sp
: (AppFontSize.small - 2).sp,
color: Colors.green[700],
fontWeight: FontWeight.w600,
),
),
),
],
),
),
],
),
),
SizedBox(height: 3.h),
_buildButton(
context,
label: "Hapus",
color: Colors.red,
onPressed: outletControllerState.isLoading
? null
: _onDeleteOutlet,
),
SizedBox(height: 1.5.h),
TextButton(
onPressed: outletControllerState.isLoading
? null
: () => context.pop(),
child: Text(
"Batal",
style: TextStyle(
color: Colors.grey[600],
fontSize: AppFontSize.medium.sp,
fontWeight: FontWeight.w600,
),
),
),
],
),
),
);
}
Widget _buildButton(
BuildContext context, {
required String label,
required Color color,
required VoidCallback? onPressed,
}) {
return SizedBox(
width: double.infinity,
height: 6.h,
child: ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
backgroundColor: color,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(2.5.w),
),
disabledBackgroundColor: Colors.grey.shade300,
),
child: Text(
label,
style: TextStyle(
color: Colors.white,
fontSize: AppFontSize.medium.sp,
fontWeight: FontWeight.bold,
),
),
),
);
}
}