From fba5d1ae87d720f20f936a3be791de4863b34076 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 5 Sep 2024 10:12:56 +0200 Subject: [PATCH] refactor --- lib/itemview.dart | 71 ++++++++++++++++++++++++----------------------- lib/main.dart | 44 ++++++++++++++--------------- 2 files changed, 58 insertions(+), 57 deletions(-) diff --git a/lib/itemview.dart b/lib/itemview.dart index e96f46f..7dda510 100644 --- a/lib/itemview.dart +++ b/lib/itemview.dart @@ -2,7 +2,7 @@ import 'package:cdb_ui/api.dart'; import 'package:flutter/material.dart'; class ItemView extends StatelessWidget { - Item item; + final Item item; @override Widget build(BuildContext context) { @@ -10,7 +10,7 @@ class ItemView extends StatelessWidget { body: Column(children: [ Row( children: [ - Align( + const Align( alignment: Alignment.centerLeft, child: Placeholder(), ), // todo @@ -18,14 +18,14 @@ class ItemView extends StatelessWidget { children: [ Text( item.name, - style: TextStyle(fontWeight: FontWeight.bold), + style: const TextStyle(fontWeight: FontWeight.bold), ), Text(item.category) ], ) ], ), - SizedBox(height: 10), + const SizedBox(height: 10), FutureBuilder( future: API().getInventory(item.id), builder: (context, snapshot) { @@ -45,7 +45,7 @@ class ItemView extends StatelessWidget { }).toList()); } - return CircularProgressIndicator(); + return const CircularProgressIndicator(); }, ) ]), @@ -54,17 +54,17 @@ class ItemView extends StatelessWidget { Navigator.of(context).push( 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 { - Item item; + final Item item; - SupplyPage(this.item); + const SupplyPage(this.item, {super.key}); @override State createState() => _SupplyPageState(); @@ -85,25 +85,37 @@ class _SupplyPageState extends State { 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 Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text('Add New Item'), + title: const Text('Add New Item'), ), body: FutureBuilder(future: () async { return ( await API().getLocations(), - await API().getUniqueField(widget.item.id, variant!, "origin") + await API().getUniqueField(widget.item.id, variant, "origin") ); }(), builder: (context, snap) { if (!snap.hasData) { - return CircularProgressIndicator(); + return const CircularProgressIndicator(); } - var (location_map, origins) = snap.data!; + var (locationMap, origins) = snap.data!; // todo : fix locations var locations = []; @@ -116,7 +128,7 @@ class _SupplyPageState extends State { children: [ // Variant selection DropdownButtonFormField( - hint: Text('Select Variant'), + hint: const Text('Select Variant'), value: variant, onChanged: (value) { setState(() { @@ -144,12 +156,12 @@ class _SupplyPageState extends State { // Origin Field with Dropdown and Text Input DropdownButtonFormField( value: _selectedOrigin, - hint: Text('Select or Enter Origin'), + hint: const Text('Select or Enter Origin'), onChanged: (value) { setState(() { _selectedOrigin = value ?? ""; - if (!_price.isEmpty) { + if (_price.isNotEmpty) { // todo : update price from latest } }); @@ -163,7 +175,8 @@ class _SupplyPageState extends State { .toList(), ), TextFormField( - decoration: InputDecoration(labelText: 'Enter New Origin'), + decoration: + const InputDecoration(labelText: 'Enter New Origin'), onChanged: (value) { setState(() { _selectedOrigin = ""; // Clear dropdown selection @@ -173,7 +186,7 @@ class _SupplyPageState extends State { // Price Field TextFormField( - decoration: InputDecoration(labelText: 'Price'), + decoration: const InputDecoration(labelText: 'Price'), keyboardType: TextInputType.number, validator: (value) { if (value == null || value.isEmpty) { @@ -191,7 +204,7 @@ class _SupplyPageState extends State { // Location Dropdown DropdownButtonFormField( - hint: Text('Select Location'), + hint: const Text('Select Location'), value: _selectedLocation, onChanged: (value) { setState(() { @@ -215,24 +228,12 @@ class _SupplyPageState extends State { }, ), - SizedBox(height: 20), + const 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'), + onPressed: _supply, + child: const Text('Add Item'), ), ], ), diff --git a/lib/main.dart b/lib/main.dart index e6fbb66..6aaaff5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -18,36 +18,36 @@ class MyApp extends StatelessWidget { colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), - home: HomePage(), + home: const HomePage(), ); } } class HomePage extends StatelessWidget { + const HomePage({super.key}); + @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar(title: Text("Home Page")), + appBar: AppBar(title: const Text("Home Page")), body: Column(children: [ - Container( - child: Row(children: const [Text("Stats about everything")]), - ), + const Row(children: [Text("Stats about everything")]), FutureBuilder( future: API().getItems(), builder: (context, snapshot) { - print(snapshot); - if (snapshot.hasData) { - var items = snapshot.data!; - - return Expanded( - child: GridView.count( - crossAxisCount: 2, - children: items.map((x) { - return ItemCard(x); - }).toList(), - )); + if (!snapshot.hasData) { + return const CircularProgressIndicator(); } - 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 { - late String item; + final String item; - ItemCard(this.item); + const ItemCard(this.item, {super.key}); @override Widget build(BuildContext context) { return InkWell( onTap: () async { - var item_info = await API().getItem(item); + var itemInfo = await API().getItem(item); Navigator.push(context, - MaterialPageRoute(builder: (context) => ItemView(item: item_info))); + MaterialPageRoute(builder: (context) => ItemView(item: itemInfo))); }, child: Row(children: [ Image.network( @@ -74,7 +74,7 @@ class ItemCard extends StatelessWidget { width: 128, height: 128, ), - Text("$item") + Text(item) ]), ); }