This commit is contained in:
JMARyA 2024-09-05 10:12:56 +02:00
parent 3da7878555
commit fba5d1ae87
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 58 additions and 57 deletions

View file

@ -2,7 +2,7 @@ import 'package:cdb_ui/api.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class ItemView extends StatelessWidget { class ItemView extends StatelessWidget {
Item item; final Item item;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -10,7 +10,7 @@ class ItemView extends StatelessWidget {
body: Column(children: [ body: Column(children: [
Row( Row(
children: [ children: [
Align( const Align(
alignment: Alignment.centerLeft, alignment: Alignment.centerLeft,
child: Placeholder(), child: Placeholder(),
), // todo ), // todo
@ -18,14 +18,14 @@ class ItemView extends StatelessWidget {
children: [ children: [
Text( Text(
item.name, item.name,
style: TextStyle(fontWeight: FontWeight.bold), style: const TextStyle(fontWeight: FontWeight.bold),
), ),
Text(item.category) Text(item.category)
], ],
) )
], ],
), ),
SizedBox(height: 10), const SizedBox(height: 10),
FutureBuilder( FutureBuilder(
future: API().getInventory(item.id), future: API().getInventory(item.id),
builder: (context, snapshot) { builder: (context, snapshot) {
@ -45,7 +45,7 @@ class ItemView extends StatelessWidget {
}).toList()); }).toList());
} }
return CircularProgressIndicator(); return const CircularProgressIndicator();
}, },
) )
]), ]),
@ -54,17 +54,17 @@ class ItemView extends StatelessWidget {
Navigator.of(context).push( Navigator.of(context).push(
MaterialPageRoute(builder: (context) => SupplyPage(item))); MaterialPageRoute(builder: (context) => SupplyPage(item)));
}, },
child: Icon(Icons.add)), child: const Icon(Icons.add)),
); );
} }
ItemView({super.key, required this.item}); const ItemView({super.key, required this.item});
} }
class SupplyPage extends StatefulWidget { class SupplyPage extends StatefulWidget {
Item item; final Item item;
SupplyPage(this.item); const SupplyPage(this.item, {super.key});
@override @override
State<SupplyPage> createState() => _SupplyPageState(); State<SupplyPage> createState() => _SupplyPageState();
@ -85,25 +85,37 @@ class _SupplyPageState extends State<SupplyPage> {
variant = widget.item.variants.keys.first; variant = widget.item.variants.keys.first;
} }
void _supply() async {} 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text('Add New Item'), title: const Text('Add New Item'),
), ),
body: FutureBuilder(future: () async { body: FutureBuilder(future: () async {
return ( return (
await API().getLocations(), await API().getLocations(),
await API().getUniqueField(widget.item.id, variant!, "origin") await API().getUniqueField(widget.item.id, variant, "origin")
); );
}(), builder: (context, snap) { }(), builder: (context, snap) {
if (!snap.hasData) { if (!snap.hasData) {
return CircularProgressIndicator(); return const CircularProgressIndicator();
} }
var (location_map, origins) = snap.data!; var (locationMap, origins) = snap.data!;
// todo : fix locations // todo : fix locations
var locations = []; var locations = [];
@ -116,7 +128,7 @@ class _SupplyPageState extends State<SupplyPage> {
children: [ children: [
// Variant selection // Variant selection
DropdownButtonFormField<String>( DropdownButtonFormField<String>(
hint: Text('Select Variant'), hint: const Text('Select Variant'),
value: variant, value: variant,
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
@ -144,12 +156,12 @@ class _SupplyPageState extends State<SupplyPage> {
// Origin Field with Dropdown and Text Input // Origin Field with Dropdown and Text Input
DropdownButtonFormField<String>( DropdownButtonFormField<String>(
value: _selectedOrigin, value: _selectedOrigin,
hint: Text('Select or Enter Origin'), hint: const Text('Select or Enter Origin'),
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
_selectedOrigin = value ?? ""; _selectedOrigin = value ?? "";
if (!_price.isEmpty) { if (_price.isNotEmpty) {
// todo : update price from latest // todo : update price from latest
} }
}); });
@ -163,7 +175,8 @@ class _SupplyPageState extends State<SupplyPage> {
.toList(), .toList(),
), ),
TextFormField( TextFormField(
decoration: InputDecoration(labelText: 'Enter New Origin'), decoration:
const InputDecoration(labelText: 'Enter New Origin'),
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
_selectedOrigin = ""; // Clear dropdown selection _selectedOrigin = ""; // Clear dropdown selection
@ -173,7 +186,7 @@ class _SupplyPageState extends State<SupplyPage> {
// Price Field // Price Field
TextFormField( TextFormField(
decoration: InputDecoration(labelText: 'Price'), decoration: const InputDecoration(labelText: 'Price'),
keyboardType: TextInputType.number, keyboardType: TextInputType.number,
validator: (value) { validator: (value) {
if (value == null || value.isEmpty) { if (value == null || value.isEmpty) {
@ -191,7 +204,7 @@ class _SupplyPageState extends State<SupplyPage> {
// Location Dropdown // Location Dropdown
DropdownButtonFormField<String>( DropdownButtonFormField<String>(
hint: Text('Select Location'), hint: const Text('Select Location'),
value: _selectedLocation, value: _selectedLocation,
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
@ -215,24 +228,12 @@ class _SupplyPageState extends State<SupplyPage> {
}, },
), ),
SizedBox(height: 20), const SizedBox(height: 20),
// Submit Button // Submit Button
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: _supply,
if (_formKey.currentState!.validate()) { child: const Text('Add Item'),
_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'),
), ),
], ],
), ),

View file

@ -18,36 +18,36 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true, useMaterial3: true,
), ),
home: HomePage(), home: const HomePage(),
); );
} }
} }
class HomePage extends StatelessWidget { class HomePage extends StatelessWidget {
const HomePage({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar(title: Text("Home Page")), appBar: AppBar(title: const Text("Home Page")),
body: Column(children: [ body: Column(children: [
Container( const Row(children: [Text("Stats about everything")]),
child: Row(children: const [Text("Stats about everything")]),
),
FutureBuilder( FutureBuilder(
future: API().getItems(), future: API().getItems(),
builder: (context, snapshot) { builder: (context, snapshot) {
print(snapshot); if (!snapshot.hasData) {
if (snapshot.hasData) { return const CircularProgressIndicator();
var items = snapshot.data!;
return Expanded(
child: GridView.count(
crossAxisCount: 2,
children: items.map((x) {
return ItemCard(x);
}).toList(),
));
} }
return CircularProgressIndicator();
var items = snapshot.data!;
return Expanded(
child: GridView.count(
crossAxisCount: 2,
children: items.map((x) {
return ItemCard(x);
}).toList(),
));
}) })
]), ]),
); );
@ -55,18 +55,18 @@ class HomePage extends StatelessWidget {
} }
class ItemCard extends StatelessWidget { class ItemCard extends StatelessWidget {
late String item; final String item;
ItemCard(this.item); const ItemCard(this.item, {super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return InkWell( return InkWell(
onTap: () async { onTap: () async {
var item_info = await API().getItem(item); var itemInfo = await API().getItem(item);
Navigator.push(context, Navigator.push(context,
MaterialPageRoute(builder: (context) => ItemView(item: item_info))); MaterialPageRoute(builder: (context) => ItemView(item: itemInfo)));
}, },
child: Row(children: [ child: Row(children: [
Image.network( Image.network(
@ -74,7 +74,7 @@ class ItemCard extends StatelessWidget {
width: 128, width: 128,
height: 128, height: 128,
), ),
Text("$item") Text(item)
]), ]),
); );
} }