mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 16:24:07 +02:00
add settings menu and saldo settings
Co-authored-by: SimonFJ20 <simonfromjakobsen@gmail.com>
This commit is contained in:
parent
bfadc046d6
commit
1bd77c854a
@ -1,7 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mobile/pages/log_in_page.dart';
|
||||
import 'package:mobile/pages/settings_page.dart';
|
||||
import 'package:mobile/repos/user.dart';
|
||||
import 'package:mobile/utils/price.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
final User user;
|
||||
@ -17,7 +18,13 @@ class HomePage extends StatelessWidget {
|
||||
children: [
|
||||
Container(
|
||||
margin: const EdgeInsets.only(right: 10),
|
||||
child: const SettingsMenu()),
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (context) => SettingsPage()));
|
||||
},
|
||||
icon: const Icon(Icons.settings),
|
||||
)),
|
||||
],
|
||||
),
|
||||
Card(
|
||||
@ -27,8 +34,10 @@ class HomePage extends StatelessWidget {
|
||||
color: Color(0xFFFFFFFF),
|
||||
),
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: Text("Saldo: ${formatDkkCents(user.balanceInDkkCents)}",
|
||||
style: Theme.of(context).textTheme.headlineSmall),
|
||||
child: Consumer<UsersRepo>(
|
||||
builder: (context, usersRepo, _) => Text(
|
||||
"Saldo: ${formatDkkCents(user.balanceInDkkCents)}",
|
||||
style: Theme.of(context).textTheme.headlineSmall)),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
@ -47,44 +56,3 @@ class HomePage extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SettingsMenu extends StatefulWidget {
|
||||
const SettingsMenu({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => SettingsMenuState();
|
||||
}
|
||||
|
||||
class SettingsMenuState extends State<SettingsMenu> {
|
||||
final FocusNode buttonFocusNode = FocusNode(debugLabel: 'Menu Button');
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MenuAnchor(
|
||||
childFocusNode: buttonFocusNode,
|
||||
menuChildren: <Widget>[
|
||||
MenuItemButton(
|
||||
onPressed: () {
|
||||
Navigator.popUntil(context, (_) => false);
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => const LogInPage()));
|
||||
},
|
||||
child: const Text('Log ud'),
|
||||
),
|
||||
],
|
||||
builder: (_, MenuController controller, Widget? child) {
|
||||
return IconButton(
|
||||
focusNode: buttonFocusNode,
|
||||
onPressed: () {
|
||||
if (controller.isOpen) {
|
||||
controller.close();
|
||||
} else {
|
||||
controller.open();
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.settings),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
78
mobile/lib/pages/settings_page.dart
Normal file
78
mobile/lib/pages/settings_page.dart
Normal file
@ -0,0 +1,78 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mobile/pages/log_in_page.dart';
|
||||
import 'package:mobile/pages/settings_pages/saldo.dart';
|
||||
import 'package:mobile/repos/user.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class _Page {
|
||||
final IconData icon;
|
||||
final String title;
|
||||
final Function(BuildContext) action;
|
||||
|
||||
const _Page({
|
||||
required this.icon,
|
||||
required this.title,
|
||||
required this.action,
|
||||
});
|
||||
}
|
||||
|
||||
class SettingsPage extends StatelessWidget {
|
||||
SettingsPage({super.key});
|
||||
|
||||
final List<_Page> _pages = [
|
||||
_Page(
|
||||
icon: Icons.money,
|
||||
title: "Saldo",
|
||||
action: (context) {
|
||||
Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (context) => const SaldoSettingsPage()));
|
||||
}),
|
||||
_Page(
|
||||
icon: Icons.door_back_door,
|
||||
title: "Log ud",
|
||||
action: (context) {
|
||||
final users = context.read<UsersRepo>();
|
||||
users.logout();
|
||||
Navigator.popUntil(context, (_) => false);
|
||||
Navigator.of(context)
|
||||
.push(MaterialPageRoute(builder: (context) => const LogInPage()));
|
||||
}),
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
body: SafeArea(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const BackButton(),
|
||||
Text(
|
||||
"Indstillinger",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: _pages.length,
|
||||
itemBuilder: (context, i) => InkWell(
|
||||
onTap: () {
|
||||
_pages[i].action(context);
|
||||
},
|
||||
child: ListTile(
|
||||
leading: Icon(_pages[i].icon),
|
||||
title: Text(_pages[i].title),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
43
mobile/lib/pages/settings_pages/saldo.dart
Normal file
43
mobile/lib/pages/settings_pages/saldo.dart
Normal file
@ -0,0 +1,43 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mobile/repos/user.dart';
|
||||
import 'package:mobile/utils/price.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class SaldoSettingsPage extends StatelessWidget {
|
||||
const SaldoSettingsPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final usersRepo = context.watch<UsersRepo>();
|
||||
final user = usersRepo.loggedInUser()!;
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
body: SafeArea(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const BackButton(),
|
||||
Text(
|
||||
"Saldo",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
Text("Nuværende saldo: ${formatDkkCents(user.balanceInDkkCents)}",
|
||||
style: Theme.of(context).textTheme.bodyLarge),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
user.addBalanceFounds(10000);
|
||||
usersRepo.veryBadNotifyAll();
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text("Tilføj 100,00 kr"),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -5,6 +5,8 @@ class UsersRepo extends ChangeNotifier {
|
||||
int nextId = 0;
|
||||
final List<User> users = [];
|
||||
|
||||
User? _loggedInUser;
|
||||
|
||||
UsersRepo() {
|
||||
addTestUsers();
|
||||
}
|
||||
@ -44,16 +46,30 @@ class UsersRepo extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Result<User, String> login(String mail, String password) {
|
||||
User? user;
|
||||
for (var i = 0; i < users.length; i++) {
|
||||
if (users[i].mail != mail) {
|
||||
continue;
|
||||
}
|
||||
if (users[i].password == password) {
|
||||
return Ok(users[i]);
|
||||
if (users[i].mail == mail) {
|
||||
user = users[i];
|
||||
}
|
||||
}
|
||||
if (user == null) {
|
||||
return Err("User with mail $mail doesn't exist");
|
||||
}
|
||||
if (user.password != password) {
|
||||
return Err("Wrong password for user with mail $mail");
|
||||
}
|
||||
return Err("User with mail $mail doesn't exist");
|
||||
_loggedInUser = user;
|
||||
notifyListeners();
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
void logout() {
|
||||
_loggedInUser = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
User? loggedInUser() {
|
||||
return _loggedInUser;
|
||||
}
|
||||
|
||||
Result<int, String> pay(int userId, int amount) {
|
||||
@ -79,6 +95,14 @@ class UsersRepo extends ChangeNotifier {
|
||||
password: "",
|
||||
balanceInDkkCents: 100000));
|
||||
}
|
||||
|
||||
void veryBadNotifyAll() {
|
||||
// ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
|
||||
// TODO: THIS SHOULD BE FIXED
|
||||
// FIXME: DO SOMETHING ELSE PLEASE!!!!!
|
||||
// ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class User {
|
||||
@ -90,12 +114,17 @@ class User {
|
||||
// balance is in øre
|
||||
int balanceInDkkCents;
|
||||
|
||||
User(
|
||||
{required this.id,
|
||||
required this.mail,
|
||||
required this.name,
|
||||
required this.password,
|
||||
required this.balanceInDkkCents});
|
||||
User({
|
||||
required this.id,
|
||||
required this.mail,
|
||||
required this.name,
|
||||
required this.password,
|
||||
required this.balanceInDkkCents,
|
||||
});
|
||||
|
||||
void addBalanceFounds(int amount) {
|
||||
balanceInDkkCents += amount;
|
||||
}
|
||||
|
||||
Result<int, String> pay(int amount) {
|
||||
if (balanceInDkkCents < amount) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user