Correctnewpage
Correctnewpage
dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
@override
void initState() {
super.initState();
_fetchInventoryDataStream();
}
void _fetchInventoryDataStream() {
_inventoryDataStream = FirebaseFirestore.instance
.collection('organicInputs')
.where('email', isEqualTo: _auth.currentUser?.email)
.snapshots();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.green[600],
title: Text('Inventory'),
),
body: StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
stream: _inventoryDataStream,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (snapshot.data!.docs.isEmpty) {
return Center(child: Text('No inventory data available.'));
} else {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
final document = snapshot.data!.docs[index];
Map<String, dynamic>? data = document.data();
if (data != null &&
data['name'] != null &&
data['dateCreated'] != null &&
data['volume'] != null) {
return _buildInventoryItem(
data['name']!,
data['dateCreated'].toDate(),
data['volume']!,
document.id,
);
} else {
return SizedBox();
}
},
);
}
},
),
);
}
Widget _buildInventoryItem(
String name, DateTime dateCreated, String volume, String documentId) {
final double initialVolume = double.tryParse(volume) ?? 0.0;
double currentVolume = initialVolume;
return Card(
margin: EdgeInsets.symmetric(vertical: 8.0),
child: ListTile(
title: Text('Name: $name'),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Date Created: ${_formatDate(dateCreated)}'),
SizedBox(height: 8.0),
FutureBuilder<int>(
future: _fetchTotalAmountUsed(documentId),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return SizedBox();
} else {
int totalAmountUsed = snapshot.data ?? 0;
currentVolume = initialVolume - totalAmountUsed;
globalRemainingAmount =
currentVolume; // Set global remaining amount
final double remainingAmount =
currentVolume < 0 ? 0 : currentVolume;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Remaining Amount: ${remainingAmount.toStringAsFixed(1)}',
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: 4.0),
Stack(
children: [
FractionallySizedBox(
alignment: Alignment.centerLeft,
widthFactor: 1.0,
child: Container(
height: 20.0,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(10.0),
),
),
),
FractionallySizedBox(
alignment: Alignment.centerRight,
widthFactor: usedPercentage / 100,
child: Container(
height: 20.0,
decoration: BoxDecoration(
color: Colors.grey[700],
borderRadius: BorderRadius.circular(10.0),
),
),
),
],
),
SizedBox(height: 8.0),
Text(
'Total Amount Used: $totalAmountUsed',
style: TextStyle(fontWeight: FontWeight.bold),
),
],
);
}
},
),
],
),
),
);
}