init
This commit is contained in:
commit
bcef1d2e48
130 changed files with 5098 additions and 0 deletions
135
lib/api.dart
Normal file
135
lib/api.dart
Normal file
|
@ -0,0 +1,135 @@
|
|||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class API {
|
||||
static String instance = "http://192.168.178.30:8080";
|
||||
|
||||
Future<String> getRequest(String url) async {
|
||||
var resp = await http.get(Uri.parse(url), headers: <String, String>{
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
});
|
||||
|
||||
return resp.body;
|
||||
}
|
||||
|
||||
Future<String> postRequest(String url, Map<String, dynamic> data) async {
|
||||
var resp = await http.post(Uri.parse(url),
|
||||
headers: <String, String>{
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: data);
|
||||
|
||||
return resp.body;
|
||||
}
|
||||
|
||||
Future<List<dynamic>> getItems() async {
|
||||
var resp = jsonDecode(await getRequest("$instance/items"));
|
||||
var lst = resp["items"];
|
||||
return lst as List<dynamic>;
|
||||
}
|
||||
|
||||
Future<Item> getItem(String item) async {
|
||||
return Item(jsonDecode(await getRequest("$instance/item/$item")));
|
||||
}
|
||||
|
||||
Future<Transaction> getTransaction(String id) async {
|
||||
return Transaction(
|
||||
jsonDecode(await getRequest("$instance/transaction/$id")));
|
||||
}
|
||||
|
||||
Future<List<Map<String, dynamic>>> getInventory(String item) async {
|
||||
return jsonDecode(await getRequest("$instance/item/$item/inventory"));
|
||||
}
|
||||
|
||||
Future<String> supplyItem(
|
||||
String item, String variant, String price, String? origin) async {
|
||||
return jsonDecode(await postRequest("$instance/supply", {
|
||||
"item": item,
|
||||
"variant": variant,
|
||||
"price": price,
|
||||
"origin": origin
|
||||
}))["uuid"];
|
||||
}
|
||||
|
||||
String getImageURL(String item) {
|
||||
return "$instance/$item/image";
|
||||
}
|
||||
}
|
||||
|
||||
class Item {
|
||||
late String id;
|
||||
late String name;
|
||||
late String category;
|
||||
late Map<String, ItemVariant> variants;
|
||||
|
||||
Item(Map<String, dynamic> json) {
|
||||
id = json["uuid"];
|
||||
name = json["name"];
|
||||
category = json["category"];
|
||||
variants = <String, ItemVariant>{};
|
||||
|
||||
json["variants"].forEach((key, value) {
|
||||
variants[key] = ItemVariant(value);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class ItemVariant {
|
||||
late String item;
|
||||
late String variant;
|
||||
late String name;
|
||||
int? min;
|
||||
int? expiry;
|
||||
|
||||
ItemVariant(Map<String, dynamic> json) {
|
||||
item = json["item"];
|
||||
variant = json["variant"];
|
||||
name = json["name"];
|
||||
min = json["min"];
|
||||
expiry = json["expiry"];
|
||||
}
|
||||
}
|
||||
|
||||
class Price {
|
||||
late double value;
|
||||
late String currency;
|
||||
|
||||
Price(Map<String, dynamic> json) {
|
||||
value = json["value"];
|
||||
currency = json["currency"];
|
||||
}
|
||||
}
|
||||
|
||||
class Transaction {
|
||||
late String uuid;
|
||||
late String item;
|
||||
late String variant;
|
||||
late Price price;
|
||||
late String? origin;
|
||||
late int timestamp;
|
||||
late ConsumeInfo consumed;
|
||||
late bool expired;
|
||||
|
||||
Transaction(Map<String, dynamic> json) {
|
||||
uuid = json["uuid"];
|
||||
item = json["item"];
|
||||
variant = json["variant"];
|
||||
price = Price(json["price"]);
|
||||
origin = json["origin"];
|
||||
timestamp = json["timestamp"];
|
||||
expired = json["expired"];
|
||||
consumed = ConsumeInfo(json["consumed"]);
|
||||
}
|
||||
}
|
||||
|
||||
class ConsumeInfo {
|
||||
late String destination;
|
||||
late Price price;
|
||||
late int timestamp;
|
||||
|
||||
ConsumeInfo(Map<String, dynamic> json) {
|
||||
destination = json["destination"];
|
||||
price = Price(json["price"]);
|
||||
timestamp = json["timestamp"];
|
||||
}
|
||||
}
|
69
lib/itemview.dart
Normal file
69
lib/itemview.dart
Normal file
|
@ -0,0 +1,69 @@
|
|||
import 'package:cdb_ui/api.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'api.dart';
|
||||
|
||||
class ItemView extends StatelessWidget {
|
||||
Item item;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Column(children: [
|
||||
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> {
|
||||
String? variant;
|
||||
|
||||
void _supply() async {}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Text("Add item ${widget.item.name}"),
|
||||
);
|
||||
}
|
||||
}
|
81
lib/main.dart
Normal file
81
lib/main.dart
Normal file
|
@ -0,0 +1,81 @@
|
|||
import 'package:cdb_ui/api.dart';
|
||||
import 'package:cdb_ui/itemview.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'CDB',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: HomePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text("Home Page")),
|
||||
body: Column(children: [
|
||||
Container(
|
||||
child: Row(children: const [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(),
|
||||
));
|
||||
}
|
||||
return CircularProgressIndicator();
|
||||
})
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ItemCard extends StatelessWidget {
|
||||
late String item;
|
||||
|
||||
ItemCard(this.item);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return InkWell(
|
||||
onTap: () async {
|
||||
var item_info = await API().getItem(item);
|
||||
|
||||
Navigator.push(context,
|
||||
MaterialPageRoute(builder: (context) => ItemView(item: item_info)));
|
||||
},
|
||||
child: Row(children: [
|
||||
Image.network(
|
||||
API().getImageURL(item),
|
||||
width: 128,
|
||||
height: 128,
|
||||
),
|
||||
Text("$item")
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue