cdb_ui/lib/pages/supply.dart

257 lines
8.5 KiB
Dart
Raw Normal View History

2024-09-07 21:56:52 +00:00
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;
2024-09-15 15:49:50 +00:00
final Function refresh;
2024-09-07 21:56:52 +00:00
2024-09-15 15:49:50 +00:00
const SupplyPage(this.item, this.refresh, {super.key});
2024-09-07 21:56:52 +00:00
@override
State<SupplyPage> createState() => _SupplyPageState();
}
class _SupplyPageState extends State<SupplyPage> {
late String variant;
final _formKey = GlobalKey<FormState>();
String _selectedOrigin = "";
String _selectedLocation = "";
2024-09-20 11:10:26 +00:00
late TextEditingController _priceController;
2024-09-20 23:37:11 +00:00
late TextEditingController _noteController;
2024-09-07 21:56:52 +00:00
@override
void initState() {
super.initState();
variant = widget.item.variants.keys.first;
2024-09-20 11:10:26 +00:00
_priceController = TextEditingController(text: "");
2024-09-20 23:37:11 +00:00
_noteController = TextEditingController(text: "");
2024-09-07 21:56:52 +00:00
}
2024-09-16 07:42:55 +00:00
void _supply() {
2024-09-07 21:56:52 +00:00
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
2024-09-16 07:42:55 +00:00
API()
2024-09-21 02:15:00 +00:00
.supplyItem(widget.item.id, variant, "${_priceController.text}",
2024-09-20 23:37:11 +00:00
_selectedOrigin, _selectedLocation, _noteController.text)
2024-09-16 07:42:55 +00:00
.then((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item added successfully!')),
);
Navigator.of(context).pop();
widget.refresh();
});
2024-09-07 21:56:52 +00:00
}
}
@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());
}
2024-09-15 13:15:46 +00:00
var data = snapshot.data as Map<String, dynamic>;
var locations = data['locations']! as Map<String, Location>;
2024-09-07 21:56:52 +00:00
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
2024-09-17 14:33:55 +00:00
AutocompletedTextField(
options: origins,
getValue: () => _selectedOrigin,
onChanged: (value) {
_selectedOrigin = value;
},
onSelection: (String selection) async {
2024-09-20 11:10:26 +00:00
var price = _priceController.text.isEmpty
2024-09-17 14:33:55 +00:00
? await API()
.getLatestPrice(widget.item.id, variant,
origin: selection)
.then((x) => x.value.toStringAsFixed(2))
2024-09-20 11:10:26 +00:00
: _priceController.text;
2024-09-17 14:33:55 +00:00
setState(() {
2024-09-20 11:10:26 +00:00
_priceController.text = price;
2024-09-17 14:33:55 +00:00
_selectedOrigin = selection;
});
},
label: "Origin"),
2024-09-07 21:56:52 +00:00
const SizedBox(height: 16),
// Price Field
TextFormField(
decoration: const InputDecoration(labelText: 'Price'),
keyboardType: TextInputType.number,
2024-09-20 11:10:26 +00:00
controller: _priceController,
2024-09-07 21:56:52 +00:00
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;
},
),
const SizedBox(height: 16),
// Location Dropdown
2024-09-15 13:15:46 +00:00
Row(
children: [
Expanded(
child: DropdownButtonFormField<String>(
hint: const Text('Select Location'),
value: _selectedLocation,
onChanged: (value) {
setState(() {
_selectedLocation = value!;
});
},
items: locations.keys
.map<DropdownMenuItem<String>>((id) {
return DropdownMenuItem<String>(
value: id,
2024-09-16 07:42:55 +00:00
child:
Text(locations[id]!.fullNamePath(locations)),
2024-09-15 13:15:46 +00:00
);
}).toList(),
onSaved: (value) {
_selectedLocation = value!;
},
),
),
2024-09-15 15:49:50 +00:00
const SizedBox(
2024-09-15 13:15:46 +00:00
width: 12,
),
IconButton(
onPressed: () {
QrBarCodeScannerDialog().getScannedQrBarCode(
context: context,
onCode: (code) {
setState(() {
_selectedLocation = code!;
});
},
);
2024-09-07 21:56:52 +00:00
},
2024-09-15 13:15:46 +00:00
icon: const Icon(Icons.qr_code),
),
],
2024-09-07 21:56:52 +00:00
),
2024-09-20 23:37:11 +00:00
// Note
TextFormField(
decoration: const InputDecoration(labelText: 'Note'),
keyboardType: TextInputType.number,
controller: _noteController,
),
2024-09-07 21:56:52 +00:00
const SizedBox(height: 20),
// Submit Button
ElevatedButton(
onPressed: _supply,
child: const Text('Add Item'),
),
],
),
),
);
},
),
);
}
2024-09-15 13:15:46 +00:00
Future<Map<String, dynamic>> _fetchData() async {
var locations = await API().getLocations();
2024-09-07 21:56:52 +00:00
var origins = await API().getUniqueField(widget.item.id, variant, "origin");
origins.insert(0, "");
2024-09-15 13:15:46 +00:00
locations[""] = Location.zero();
2024-09-07 21:56:52 +00:00
return {'locations': locations, 'origins': origins};
}
}
2024-09-17 14:33:55 +00:00
// ignore: must_be_immutable
class AutocompletedTextField extends StatelessWidget {
late List<String> options;
late Function(String) onChanged;
late String Function() getValue;
late Function(String) onSelection;
late String label;
AutocompletedTextField(
{super.key,
required this.options,
required this.getValue,
required this.onChanged,
required this.onSelection,
required this.label});
@override
Widget build(BuildContext context) {
return Autocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
if (textEditingValue.text.isEmpty) {
return options;
}
return options.where((String option) {
return option
.toLowerCase()
.contains(textEditingValue.text.toLowerCase());
});
},
onSelected: onSelection,
fieldViewBuilder:
(context, textEditingController, focusNode, onFieldSubmitted) {
textEditingController.text = getValue();
return TextFormField(
onChanged: onChanged,
controller: textEditingController,
focusNode: focusNode,
decoration: InputDecoration(
labelText: label,
border: const OutlineInputBorder(),
),
);
},
);
}
}