Fitur Checklist pada kebutuhan bahan

This commit is contained in:
rhanarmt 2026-04-30 14:40:37 +07:00
parent eae95fa4eb
commit e08e9c91e9
3 changed files with 290 additions and 197 deletions

View File

@ -9,6 +9,7 @@ class PredictionController extends ChangeNotifier {
List<Map<String, dynamic>> recipes = [];
Map<String, Map<String, dynamic>> recipeIngredients = {};
Map<String, bool> ingredientSelections = {};
final Map<String, int> currentStock = {
'Tepung Terigu 1kg': 45000,
@ -46,6 +47,8 @@ class PredictionController extends ChangeNotifier {
}
}
_initializeIngredientSelections();
return null;
} catch (e) {
return 'Gagal memuat resep: $e';
@ -58,6 +61,7 @@ class PredictionController extends ChangeNotifier {
void setSelectedRecipe(String? value) {
selectedRecipe = value;
isCalculated = false;
_initializeIngredientSelections();
notifyListeners();
}
@ -78,9 +82,31 @@ class PredictionController extends ChangeNotifier {
selectedRecipe = null;
productionQuantity = 0;
isCalculated = false;
ingredientSelections = {};
notifyListeners();
}
void setIngredientSelection(String ingredient, bool isSelected) {
ingredientSelections[ingredient] = isSelected;
notifyListeners();
}
bool isIngredientSelected(String ingredient) {
return ingredientSelections[ingredient] ?? true;
}
void _initializeIngredientSelections() {
if (selectedRecipe == null) {
ingredientSelections = {};
return;
}
final ingredients = recipeIngredients[selectedRecipe] ?? {};
ingredientSelections = {
for (final ingredient in ingredients.keys) ingredient: true,
};
}
Map<String, int> get requiredIngredients {
if (selectedRecipe == null || productionQuantity == 0) {
return {};
@ -90,6 +116,7 @@ class PredictionController extends ChangeNotifier {
final required = <String, int>{};
ingredients.forEach((productName, details) {
if (!isIngredientSelected(productName)) return;
final quantity = (details['quantity'] as num).toInt();
required[productName] = quantity * productionQuantity;
});

View File

@ -31,6 +31,12 @@ class _PredictionScreenState extends State<PredictionScreen> {
return AnimatedBuilder(
animation: _controller,
builder: (context, _) {
final selectedRecipe = _controller.selectedRecipe;
final displayIngredients =
selectedRecipe == null
? <String, Map<String, dynamic>>{}
: (_controller.recipeIngredients[selectedRecipe] ??
<String, Map<String, dynamic>>{});
return Scaffold(
backgroundColor: const Color(0xFFF5F5F5),
appBar: PreferredSize(
@ -526,6 +532,30 @@ class _PredictionScreenState extends State<PredictionScreen> {
),
),
const SizedBox(height: 12),
if (displayIngredients.isEmpty)
Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 10,
offset: const Offset(0, 2),
),
],
),
child: const Text(
'Belum ada bahan untuk produk ini.',
style: TextStyle(
fontSize: 12,
color: Color(0xFF6B7280),
),
),
)
else
Container(
decoration: BoxDecoration(
color: Colors.white,
@ -540,26 +570,54 @@ class _PredictionScreenState extends State<PredictionScreen> {
),
child: Column(
children:
_controller.requiredIngredients.entries.toList().asMap().entries.map((
displayIngredients.entries.toList().asMap().entries.map((
entry,
) {
final isLast =
entry.key ==
_controller
.requiredIngredients
.length -
1;
displayIngredients.length - 1;
final ingredient = entry.value.key;
final details = entry.value.value;
final isSelected = _controller
.isIngredientSelected(
ingredient,
);
final unit =
(details['unit'] as String?) ??
_controller.getIngredientUnit(
ingredient,
);
final quantityPerUnit =
(details['quantity'] as num)
.toInt();
final neededAmount =
entry.value.value;
isSelected
? quantityPerUnit *
_controller
.productionQuantity
: 0;
final availableAmount =
_controller
.currentStock[ingredient] ??
0;
final unit = _controller
.getIngredientUnit(ingredient);
final isSufficient =
availableAmount >= neededAmount;
final statusColor =
isSelected
? (isSufficient
? const Color(
0xFF10B981,
)
: const Color(
0xFFDC2626,
))
: const Color(0xFF9CA3AF);
final statusLabel =
isSelected
? (isSufficient
? 'Aman'
: 'Kurang')
: 'Diabaikan';
return Column(
children: [
@ -574,6 +632,24 @@ class _PredictionScreenState extends State<PredictionScreen> {
CrossAxisAlignment
.start,
children: [
Checkbox(
value: isSelected,
onChanged: (value) {
_controller
.setIngredientSelection(
ingredient,
value ??
false,
);
},
activeColor:
const Color(
0xFFA89080,
),
),
const SizedBox(
width: 4,
),
Expanded(
child: Column(
crossAxisAlignment:
@ -586,7 +662,8 @@ class _PredictionScreenState extends State<PredictionScreen> {
ingredient,
),
style: const TextStyle(
fontSize: 13,
fontSize:
13,
fontWeight:
FontWeight
.w600,
@ -603,8 +680,7 @@ class _PredictionScreenState extends State<PredictionScreen> {
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment
.start,
CrossAxisAlignment.start,
children: [
const Text(
'Kebutuhan',
@ -634,8 +710,7 @@ class _PredictionScreenState extends State<PredictionScreen> {
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment
.start,
CrossAxisAlignment.start,
children: [
const Text(
'Stok Saat Ini',
@ -673,14 +748,12 @@ class _PredictionScreenState extends State<PredictionScreen> {
Container(
padding:
const EdgeInsets.symmetric(
horizontal: 10,
horizontal:
10,
vertical: 6,
),
decoration: BoxDecoration(
color: _controller
.getStatusColor(
ingredient,
)
color: statusColor
.withOpacity(
0.15,
),
@ -689,10 +762,7 @@ class _PredictionScreenState extends State<PredictionScreen> {
8,
),
border: Border.all(
color: _controller
.getStatusColor(
ingredient,
)
color: statusColor
.withOpacity(
0.3,
),
@ -700,18 +770,14 @@ class _PredictionScreenState extends State<PredictionScreen> {
),
),
child: Text(
isSufficient
? 'Aman'
: 'Kurang',
statusLabel,
style: TextStyle(
fontSize: 11,
fontWeight:
FontWeight
.w700,
color: _controller
.getStatusColor(
ingredient,
),
color:
statusColor,
),
),
),

View File

@ -5,7 +5,7 @@ class MLService {
// API URL - Change based on environment
// Untuk emulator Android: 10.0.2.2
// Untuk device fisik: 192.168.x.x atau 127.0.0.1 kalau local
static const String baseUrl = 'http://192.168.110.16:5000';
static const String baseUrl = 'http://192.168.1.62:5000 ';
static const int timeoutSeconds = 30;