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

View File

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

View File

@ -5,7 +5,7 @@ class MLService {
// API URL - Change based on environment // API URL - Change based on environment
// Untuk emulator Android: 10.0.2.2 // Untuk emulator Android: 10.0.2.2
// Untuk device fisik: 192.168.x.x atau 127.0.0.1 kalau local // 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; static const int timeoutSeconds = 30;