cdb_ui/lib/itemview.dart
2024-09-05 09:09:51 +02:00

244 lines
7.2 KiB
Dart

import 'package:cdb_ui/api.dart';
import 'package:flutter/material.dart';
class ItemView extends StatelessWidget {
Item item;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
Row(
children: [
Align(
alignment: Alignment.centerLeft,
child: Placeholder(),
), // todo
Column(
children: [
Text(
item.name,
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(item.category)
],
)
],
),
SizedBox(height: 10),
FutureBuilder(
future: API().getInventory(item.id),
builder: (context, snapshot) {
if (snapshot.hasData) {
var data = snapshot.data!;
return Column(
children: data.map((x) {
return Row(
children: [
Text(x["uuid"]),
Text(x["origin"]),
Text(x["price"]),
Text(x["timestamp"])
],
);
}).toList());
}
return CircularProgressIndicator();
},
)
]),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => SupplyPage(item)));
},
child: Icon(Icons.add)),
);
}
ItemView({super.key, required this.item});
}
class SupplyPage extends StatefulWidget {
Item item;
SupplyPage(this.item);
@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;
}
void _supply() async {}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Add New Item'),
),
body: FutureBuilder(future: () async {
return (
await API().getLocations(),
await API().getUniqueField(widget.item.id, variant!, "origin")
);
}(), builder: (context, snap) {
if (!snap.hasData) {
return CircularProgressIndicator();
}
var (location_map, origins) = snap.data!;
// todo : fix locations
var locations = [];
return Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
// Variant selection
DropdownButtonFormField<String>(
hint: Text('Select Variant'),
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,
hint: Text('Select or Enter Origin'),
onChanged: (value) {
setState(() {
_selectedOrigin = value ?? "";
if (!_price.isEmpty) {
// todo : update price from latest
}
});
},
items: origins
.map<DropdownMenuItem<String>>(
(origin) => DropdownMenuItem<String>(
value: origin,
child: Text(origin),
))
.toList(),
),
TextFormField(
decoration: InputDecoration(labelText: 'Enter New Origin'),
onChanged: (value) {
setState(() {
_selectedOrigin = ""; // Clear dropdown selection
});
},
),
// Price Field
TextFormField(
decoration: InputDecoration(labelText: 'Price'),
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
DropdownButtonFormField<String>(
hint: 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!;
},
),
SizedBox(height: 20),
// Submit Button
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
API().supplyItem(widget.item.name, variant!, _price,
_selectedOrigin, _selectedLocation);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Item added successfully!')),
);
Navigator.of(context).pop();
}
},
child: Text('Add Item'),
),
],
),
),
);
}),
);
}
}