import 'package:cdb_ui/api.dart'; import 'package:flutter/material.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( child: Column( children: [ Row( children: [Text(t.uuid), Text(t.item), Text(t.variant)], ), ], ), ); } } class SupplyPage extends StatefulWidget { final Item item; const SupplyPage(this.item, {super.key}); @override State createState() => _SupplyPageState(); } class _SupplyPageState extends State { late String variant; final _formKey = GlobalKey(); String _selectedOrigin = ""; String _selectedLocation = ""; String _price = ""; @override void initState() { super.initState(); variant = widget.item.variants.keys.first; } 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: () async { return ( await API().getLocations(), await API().getUniqueField(widget.item.id, variant, "origin") ); }(), builder: (context, snap) { if (!snap.hasData) { return const CircularProgressIndicator(); } var (locationMap, 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( hint: const Text('Select Variant'), value: variant, onChanged: (value) { setState(() { variant = value!; }); }, items: widget.item.variants.entries .map>((variant) { return DropdownMenuItem( 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( value: _selectedOrigin, hint: const Text('Select or Enter Origin'), onChanged: (value) { setState(() { _selectedOrigin = value ?? ""; if (_price.isNotEmpty) { // todo : update price from latest } }); }, items: origins .map>( (origin) => DropdownMenuItem( value: origin, child: Text(origin), )) .toList(), ), TextFormField( decoration: const InputDecoration(labelText: 'Enter New Origin'), onChanged: (value) { setState(() { _selectedOrigin = ""; // Clear dropdown selection }); }, ), // Price Field TextFormField( decoration: const 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( hint: const Text('Select Location'), value: _selectedLocation, onChanged: (value) { setState(() { _selectedLocation = value!; }); }, items: locations.map>((location) { return DropdownMenuItem( value: location, child: Text(location), ); }).toList(), validator: (value) { if (value == null) { return 'Please select a location'; } return null; }, onSaved: (value) { _selectedLocation = value!; }, ), const SizedBox(height: 20), // Submit Button ElevatedButton( onPressed: _supply, child: const Text('Add Item'), ), ], ), ), ); }), ); } }