Professional Documents
Culture Documents
Inventory Exceed 2
Inventory Exceed 2
dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:helloworld/crop_summary.dart';
import 'package:helloworld/newpage.dart';
import 'auth.dart';
ApplicationOldCropPage({required this.organicInputId});
@override
_ApplicationOldCropPageState createState() => _ApplicationOldCropPageState();
}
@override
void initState() {
super.initState();
_getOrganicInputName(widget.organicInputId);
_totalAmountUsedFuture = _fetchTotalAmountUsed(widget.organicInputId);
}
setState(() {
organicInputName = organicInputDoc['name'] ?? '';
});
} catch (e) {
print('Error fetching organic input name: $e');
setState(() {
organicInputName = 'Unknown Organic Input';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.green[600],
title: Text('Application Page'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
Text('New Crop'),
Switch(
value: isNewCrop,
onChanged: (value) {
setState(() {
isNewCrop = value;
});
},
),
],
),
SizedBox(height: 20),
isNewCrop ? _buildNewCropForm() : _buildExistingCropsDropdown(),
isNewCrop ? SizedBox(height: 20) : _buildAmountTextField(),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
_showCreateConfirmationDialog(context);
},
child: Text('Create'),
),
],
),
),
);
}
Widget _buildNewCropForm() {
return Column(
children: [
TextField(
controller: cropController,
decoration: InputDecoration(
labelText: 'Name of Crop',
),
),
SizedBox(height: 20),
TextField(
controller: amountController,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly // Allow only integer input
],
decoration: InputDecoration(
labelText: 'Amount of Organic Input',
),
),
],
);
}
Widget _buildExistingCropsDropdown() {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('applications')
.where('email', isEqualTo: widget.auth.currentUser?.email)
.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
return DropdownButton<String>(
value: cropController.text.isNotEmpty ? cropController.text : null,
onChanged: (String? newValue) {
setState(() {
cropController.text = newValue ?? '';
});
},
items: existingCrops.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
hint: Text('Select Existing Crop'),
);
},
);
}
Widget _buildAmountTextField() {
return TextField(
controller: amountController,
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly // Allow only integer input
],
decoration: InputDecoration(
labelText: 'Amount of Organic Input',
),
);
}
if (isNewCrop) {
_saveNewCrop(userEmail, cropName, amount, context); // Pass context
} else {
_saveExistingCrop(userEmail, cropName, amount, context); // Pass context
}
}
void _saveNewCrop(
String userEmail, String cropName, int amount, BuildContext context) {
DateTime now = DateTime.now();
String formattedDate =
"${now.year}-${now.month}-${now.day} ${now.hour}:${now.minute}:$
{now.second}";
FirebaseFirestore.instance.collection('applications').add({
'cropName': cropName,
'amount': amount,
'email': userEmail,
'organicInputId': widget.organicInputId,
'organicInputName': organicInputName,
'dateCreated': formattedDate,
'cropId': '',
}).then((DocumentReference documentReference) {
String cropId = documentReference.id;
documentReference.update({'cropId': cropId});
_redirectToCropSummary(context, cropId); // Redirect to CropSummaryPage
});
}
void _saveExistingCrop(
String userEmail, String cropName, int amount, BuildContext context) {
FirebaseFirestore.instance
.collection('applications')
.where('email', isEqualTo: userEmail)
.where('cropName', isEqualTo: cropName)
.get()
.then((QuerySnapshot snapshot) {
if (snapshot.docs.isNotEmpty) {
var existingCropDoc = snapshot.docs.first;
String cropId = existingCropDoc['cropId'] ?? '';
_saveNewCropWithCropId(
userEmail, cropName, amount, cropId, context); // Pass context
}
});
}
FirebaseFirestore.instance.collection('applications').add({
'cropName': cropName,
'amount': amount,
'email': userEmail,
'organicInputId': widget.organicInputId,
'organicInputName': organicInputName,
'dateCreated': formattedDate,
'cropId': cropId,
}).then((DocumentReference documentReference) {
_redirectToCropSummary(context, cropId); // Redirect to CropSummaryPage
});
}
void _redirectToCropSummary(BuildContext context, String cropId) {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => CropSummaryPage(cropId: cropId)),
);
}