cdb_ui/lib/itemview.dart

277 lines
8.3 KiB
Dart
Raw Normal View History

2024-09-04 18:05:04 +00:00
import 'package:cdb_ui/api.dart';
import 'package:flutter/material.dart';
2024-09-05 09:08:52 +00:00
import 'package:qr_bar_code_scanner_dialog/qr_bar_code_scanner_dialog.dart';
2024-09-04 18:05:04 +00:00
class ItemView extends StatelessWidget {
2024-09-05 08:12:56 +00:00
final Item item;
2024-09-04 18:05:04 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
2024-09-05 07:09:51 +00:00
Row(
children: [
2024-09-05 08:12:56 +00:00
const Align(
2024-09-05 07:09:51 +00:00
alignment: Alignment.centerLeft,
child: Placeholder(),
), // todo
Column(
children: [
Text(
item.name,
2024-09-05 08:12:56 +00:00
style: const TextStyle(fontWeight: FontWeight.bold),
2024-09-05 07:09:51 +00:00
),
Text(item.category)
],
)
],
),
2024-09-05 08:12:56 +00:00
const SizedBox(height: 10),
2024-09-05 08:25:18 +00:00
Row(
children: item.variants.entries.map((entry) {
return Text(entry.value.name);
}).toList()),
2024-09-04 18:05:04 +00:00
FutureBuilder(
future: API().getInventory(item.id),
builder: (context, snapshot) {
2024-09-05 08:25:18 +00:00
if (!snapshot.hasData) {
return const CircularProgressIndicator();
2024-09-04 18:05:04 +00:00
}
2024-09-05 08:25:18 +00:00
var data = snapshot.data!;
2024-09-04 18:05:04 +00:00
2024-09-05 08:25:18 +00:00
return Expanded(
child: ListView(
children: data.map((x) => TransactionCard(x)).toList()));
2024-09-04 18:05:04 +00:00
},
)
]),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => SupplyPage(item)));
},
2024-09-05 08:12:56 +00:00
child: const Icon(Icons.add)),
2024-09-04 18:05:04 +00:00
);
}
2024-09-05 08:12:56 +00:00
const ItemView({super.key, required this.item});
2024-09-04 18:05:04 +00:00
}
2024-09-05 08:25:18 +00:00
class TransactionCard extends StatelessWidget {
final Transaction t;
const TransactionCard(this.t, {super.key});
@override
Widget build(BuildContext context) {
return Card(
child: Column(
children: [
Row(
children: [Text(t.uuid), Text(t.item), Text(t.variant)],
),
],
),
);
}
}
2024-09-04 18:05:04 +00:00
class SupplyPage extends StatefulWidget {
2024-09-05 08:12:56 +00:00
final Item item;
2024-09-04 18:05:04 +00:00
2024-09-05 08:12:56 +00:00
const SupplyPage(this.item, {super.key});
2024-09-04 18:05:04 +00:00
@override
State<SupplyPage> createState() => _SupplyPageState();
}
class _SupplyPageState extends State<SupplyPage> {
2024-09-05 07:09:51 +00:00
late String variant;
final _formKey = GlobalKey<FormState>();
String _selectedOrigin = "";
String _selectedLocation = "";
String _price = "";
@override
void initState() {
super.initState();
variant = widget.item.variants.keys.first;
}
2024-09-04 18:05:04 +00:00
2024-09-05 08:12:56 +00:00
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();
}
}
2024-09-04 18:05:04 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
2024-09-05 07:09:51 +00:00
appBar: AppBar(
2024-09-05 08:12:56 +00:00
title: const Text('Add New Item'),
2024-09-05 07:09:51 +00:00
),
body: FutureBuilder(future: () async {
return (
await API().getLocations(),
2024-09-05 08:12:56 +00:00
await API().getUniqueField(widget.item.id, variant, "origin")
2024-09-05 07:09:51 +00:00
);
}(), builder: (context, snap) {
if (!snap.hasData) {
2024-09-05 08:12:56 +00:00
return const CircularProgressIndicator();
2024-09-05 07:09:51 +00:00
}
2024-09-05 08:12:56 +00:00
var (locationMap, origins) = snap.data!;
2024-09-05 07:09:51 +00:00
// todo : fix locations
var locations = [];
return Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
// Variant selection
DropdownButtonFormField<String>(
2024-09-05 08:12:56 +00:00
hint: const Text('Select Variant'),
2024-09-05 07:09:51 +00:00
value: variant,
onChanged: (value) {
setState(() {
variant = value!;
});
},
items: widget.item.variants.entries
.map<DropdownMenuItem<String>>((variant) {
return DropdownMenuItem<String>(
value: variant.key,
child: Text(variant.value.name),
);
}).toList(),
validator: (value) {
if (value == null) {
return 'Please select a variant';
}
return null;
},
onSaved: (value) {
variant = value!;
},
),
// Origin Field with Dropdown and Text Input
DropdownButtonFormField<String>(
value: _selectedOrigin,
2024-09-05 08:12:56 +00:00
hint: const Text('Select or Enter Origin'),
2024-09-05 07:09:51 +00:00
onChanged: (value) {
setState(() {
_selectedOrigin = value ?? "";
2024-09-05 08:12:56 +00:00
if (_price.isNotEmpty) {
2024-09-05 07:09:51 +00:00
// todo : update price from latest
}
});
},
items: origins
.map<DropdownMenuItem<String>>(
(origin) => DropdownMenuItem<String>(
value: origin,
child: Text(origin),
))
.toList(),
),
TextFormField(
2024-09-05 08:12:56 +00:00
decoration:
const InputDecoration(labelText: 'Enter New Origin'),
2024-09-05 07:09:51 +00:00
onChanged: (value) {
setState(() {
_selectedOrigin = ""; // Clear dropdown selection
});
},
),
// Price Field
TextFormField(
2024-09-05 08:12:56 +00:00
decoration: const InputDecoration(labelText: 'Price'),
2024-09-05 07:09:51 +00:00
keyboardType: TextInputType.number,
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!;
},
),
// Location Dropdown
2024-09-05 09:08:52 +00:00
Row(
children: [
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(),
validator: (value) {
if (value == null) {
return 'Please select a location';
}
return null;
},
onSaved: (value) {
_selectedLocation = value!;
},
),
IconButton(
onPressed: () {
QrBarCodeScannerDialog().getScannedQrBarCode(
context: context,
onCode: (code) {
setState(() {
_selectedLocation = code!;
});
});
},
icon: const Icon(Icons.qr_code))
],
2024-09-05 07:09:51 +00:00
),
2024-09-05 08:12:56 +00:00
const SizedBox(height: 20),
2024-09-05 07:09:51 +00:00
// Submit Button
ElevatedButton(
2024-09-05 08:12:56 +00:00
onPressed: _supply,
child: const Text('Add Item'),
2024-09-05 07:09:51 +00:00
),
],
),
),
);
}),
2024-09-04 18:05:04 +00:00
);
}
}