This commit is contained in:
JMARyA 2024-09-07 23:56:52 +02:00
parent 89bfbd2cb8
commit 310102e4ba
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 392 additions and 299 deletions

140
lib/pages/itemview.dart Normal file
View file

@ -0,0 +1,140 @@
import 'package:cdb_ui/api.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'supply.dart';
class ItemView extends StatelessWidget {
final Item item;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
Row(
children: [
const Align(
alignment: Alignment.centerLeft,
child: Placeholder(),
), // todo
Column(
children: [
Text(
item.name,
style: const TextStyle(fontWeight: FontWeight.bold),
),
Text(item.category)
],
)
],
),
const SizedBox(height: 10),
Row(
children: item.variants.entries.map((entry) {
return Text(entry.value.name);
}).toList()),
FutureBuilder(
future: API().getInventory(item.id),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
var data = snapshot.data!;
return Expanded(
child: ListView(
children: data.map((x) => TransactionCard(x)).toList()));
},
)
]),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => SupplyPage(item)));
},
child: const Icon(Icons.add)),
);
}
const ItemView({super.key, required this.item});
}
class TransactionCard extends StatelessWidget {
final Transaction t;
const TransactionCard(this.t, {super.key});
@override
Widget build(BuildContext context) {
return Card(
color: t.expired ? Colors.red[100] : Colors.white,
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
elevation: 4,
child: Padding(
padding: EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
t.item,
style: TextStyle(fontSize: 16),
),
SizedBox(
width: 4,
),
Text(
t.variant,
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
),
],
),
Text(
tsFormat(t.timestamp),
style: TextStyle(fontSize: 14, color: Colors.grey[700]),
),
],
),
SizedBox(
height: 10,
),
Row(
children: [
Icon(Icons.money, size: 18, color: Colors.green),
SizedBox(width: 6),
Text(
"${t.price.value} ${t.price.currency}",
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
],
),
if (t.origin != null) ...[
SizedBox(height: 8),
Row(
children: [
Icon(Icons.store, size: 18, color: Colors.blue),
SizedBox(width: 6),
Text(
t.origin!,
style: TextStyle(fontSize: 14, color: Colors.grey[700]),
),
],
),
],
],
),
),
);
}
}
String tsFormat(int ts) {
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(ts * 1000);
return DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
}

199
lib/pages/supply.dart Normal file
View file

@ -0,0 +1,199 @@
import 'package:cdb_ui/api.dart';
import 'package:flutter/material.dart';
import 'package:qr_bar_code_scanner_dialog/qr_bar_code_scanner_dialog.dart';
class SupplyPage extends StatefulWidget {
final Item item;
const SupplyPage(this.item, {super.key});
@override
State<SupplyPage> createState() => _SupplyPageState();
}
class _SupplyPageState extends State<SupplyPage> {
late String variant;
final _formKey = GlobalKey<FormState>();
String _selectedOrigin = "";
String _selectedLocation = "";
String _price = "";
@override
void initState() {
super.initState();
variant = widget.item.variants.keys.first;
}
Future<void> _supply() async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
await API().supplyItem(widget.item.name, variant, "${_price}",
_selectedOrigin, _selectedLocation);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item added successfully!')),
);
Navigator.of(context).pop();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Add New Item'),
),
body: FutureBuilder(
future: _fetchData(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
var data = snapshot.data as Map<String, List<dynamic>>;
var locations = data['locations']!;
var origins = data['origins']! as List<String>;
return Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Variant Selection
DropdownButtonFormField<String>(
hint: const Text('Select Variant'),
value: variant,
onChanged: (value) {
setState(() {
variant = value!;
});
},
items: widget.item.variants.entries.map((entry) {
return DropdownMenuItem<String>(
value: entry.key,
child: Text(entry.value.name),
);
}).toList(),
onSaved: (value) {
variant = value!;
},
),
const SizedBox(height: 16),
// Origin Field with Dropdown and Text Input
// todo : fix state
Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text.isEmpty) {
return origins;
}
return origins.where((String option) {
return option
.toLowerCase()
.contains(textEditingValue.text.toLowerCase());
});
},
onSelected: (String selection) {
setState(() {
_selectedOrigin = selection;
});
},
fieldViewBuilder: (context, textEditingController,
focusNode, onFieldSubmitted) {
textEditingController.text = _selectedOrigin;
return TextFormField(
controller: textEditingController,
focusNode: focusNode,
decoration: const InputDecoration(
labelText: 'Origin',
border: OutlineInputBorder(),
),
);
},
),
const SizedBox(height: 16),
// Price Field
TextFormField(
decoration: const InputDecoration(labelText: 'Price'),
keyboardType: TextInputType.number,
initialValue: _price,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a price';
}
if (double.tryParse(value) == null) {
return 'Please enter a valid number';
}
return null;
},
onSaved: (value) {
_price = value!;
},
),
const SizedBox(height: 16),
// Location Dropdown
DropdownButtonFormField<String>(
hint: const Text('Select Location'),
value: _selectedLocation,
onChanged: (value) {
setState(() {
_selectedLocation = value!;
});
},
items: locations.map<DropdownMenuItem<String>>((location) {
return DropdownMenuItem<String>(
value: location,
child: Text(location),
);
}).toList(),
onSaved: (value) {
_selectedLocation = value!;
},
),
IconButton(
onPressed: () {
QrBarCodeScannerDialog().getScannedQrBarCode(
context: context,
onCode: (code) {
setState(() {
_selectedLocation = code!;
});
},
);
},
icon: const Icon(Icons.qr_code),
),
const SizedBox(height: 20),
// Submit Button
ElevatedButton(
onPressed: _supply,
child: const Text('Add Item'),
),
],
),
),
);
},
),
);
}
Future<Map<String, List<dynamic>>> _fetchData() async {
var locations = await API().getLocations();
var origins = await API().getUniqueField(widget.item.id, variant, "origin");
origins.insert(0, "");
locations.insert(0, "");
return {'locations': locations, 'origins': origins};
}
}