This repository has been archived on 2024-07-21. You can view files and clone it, but cannot push or open issues or pull requests.
opencanteen/lib/okna/burza.dart

149 lines
5.1 KiB
Dart
Raw Permalink Normal View History

2022-04-05 19:48:14 +02:00
import 'package:canteenlib/canteenlib.dart';
import 'package:flutter/material.dart';
2022-12-14 20:02:32 +01:00
import 'package:opencanteen/okna/login.dart';
import 'package:opencanteen/pw/platformbutton.dart';
import 'package:opencanteen/pw/platformdialog.dart';
2022-04-05 19:48:14 +02:00
import 'package:opencanteen/util.dart';
2023-01-28 15:41:17 +01:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2022-04-05 19:48:14 +02:00
2022-12-14 20:02:32 +01:00
class BurzaView extends StatefulWidget {
const BurzaView({Key? key, required this.canteen}) : super(key: key);
2022-04-05 19:48:14 +02:00
final Canteen canteen;
@override
2022-12-14 20:02:32 +01:00
State<BurzaView> createState() => _BurzaViewState();
2022-04-05 19:48:14 +02:00
}
2022-12-14 20:02:32 +01:00
class _BurzaViewState extends State<BurzaView> {
List<Widget> content = [];
double balance = 0.0;
Future<void> loadExchange(BuildContext context) async {
content = [const CircularProgressIndicator()];
var uzivatel = await widget.canteen.ziskejUzivatele().catchError((o) {
2022-04-05 19:48:14 +02:00
if (!widget.canteen.prihlasen) {
Navigator.pushReplacement(
context, platformRouter((c) => const LoginPage()));
}
return Uzivatel(kredit: 0);
});
balance = uzivatel.kredit;
var burza = await widget.canteen.ziskatBurzu();
setState(() {
content = [];
if (burza.isEmpty) {
content = [
Text(
2023-01-28 15:41:17 +01:00
AppLocalizations.of(context)!.noExchange,
style: const TextStyle(fontSize: 20),
),
2023-01-28 15:41:17 +01:00
Text(AppLocalizations.of(context)!.pullToReload)
];
} else {
for (var b in burza) {
content.add(
Padding(
padding: const EdgeInsets.only(top: 15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("${b.den.day}. ${b.den.month}."),
const SizedBox(width: 10),
Flexible(
child: Text(
b.nazev,
),
),
Text("${b.pocet}x"),
PlatformButton(
onPressed: () {
widget.canteen.objednatZBurzy(b).then(
(a) {
if (a) {
showDialog(
context: context,
builder: (context) => PlatformDialog(
2023-01-28 15:41:17 +01:00
title: AppLocalizations.of(context)!.ordered,
content:
AppLocalizations.of(context)!.orderSuccess,
actions: [
PlatformButton(
2023-01-28 15:41:17 +01:00
text: AppLocalizations.of(context)!.ok,
onPressed: () =>
Navigator.of(context).pop(),
)
],
),
);
} else {
showDialog(
context: context,
builder: (context) => PlatformDialog(
2023-01-28 15:41:17 +01:00
title:
AppLocalizations.of(context)!.cannotOrder,
content:
AppLocalizations.of(context)!.errorOrdering,
actions: [
PlatformButton(
2023-01-28 15:41:17 +01:00
text: AppLocalizations.of(context)!.ok,
onPressed: () =>
Navigator.of(context).pop(),
)
],
),
);
}
loadExchange(context);
},
);
},
2023-01-28 15:41:17 +01:00
text: AppLocalizations.of(context)!.order,
),
],
),
),
);
}
2022-04-05 19:48:14 +02:00
}
});
return;
2022-04-05 19:48:14 +02:00
}
@override
void initState() {
super.initState();
loadExchange(context);
2022-04-05 19:48:14 +02:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
2022-12-12 16:48:19 +01:00
drawer: drawerGenerator(context, widget.canteen, 3),
2022-04-05 19:48:14 +02:00
appBar: AppBar(
2023-01-28 15:41:17 +01:00
title: Text(AppLocalizations.of(context)!.exchange),
2022-04-05 19:48:14 +02:00
),
body: RefreshIndicator(
child: Center(
child: SizedBox(
width: MediaQuery.of(context).size.width - 50,
child: Column(
children: [
const SizedBox(height: 10),
2023-01-28 15:41:17 +01:00
Text("${AppLocalizations.of(context)!.balance}$balance"),
const SizedBox(height: 10),
SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: SizedBox(
height: MediaQuery.of(context).size.height / 1.3,
child: Column(children: content),
),
)
],
),
2022-04-05 19:48:14 +02:00
),
),
onRefresh: () => loadExchange(context)),
2022-04-05 19:48:14 +02:00
);
}
}