SimpleCloudNotifier/flutter/lib/pages/account/root.dart

78 lines
1.9 KiB
Dart
Raw Normal View History

2024-02-11 01:08:51 +01:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/pages/account/login.dart';
import '../../state/user_account.dart';
import 'choose_auth.dart';
class AccountRootPage extends StatefulWidget {
const AccountRootPage({super.key});
@override
State<AccountRootPage> createState() => _AccountRootPageState();
}
enum _SubPage { chooseAuth, login, main }
class _AccountRootPageState extends State<AccountRootPage> {
late _SubPage _page;
2024-02-11 02:20:48 +01:00
late UserAccount userAcc;
2024-02-11 01:08:51 +01:00
@override
void initState() {
super.initState();
2024-02-11 02:20:48 +01:00
userAcc = Provider.of<UserAccount>(context, listen: false);
2024-02-11 01:08:51 +01:00
2024-02-11 02:20:48 +01:00
_page = (userAcc.auth != null) ? _SubPage.main : _SubPage.chooseAuth;
2024-02-11 01:08:51 +01:00
2024-02-11 02:20:48 +01:00
userAcc.addListener(_onAuthStateChanged);
2024-02-11 01:08:51 +01:00
}
@override
void dispose() {
2024-02-11 02:20:48 +01:00
userAcc.removeListener(_onAuthStateChanged);
2024-02-11 01:08:51 +01:00
super.dispose();
}
void _onAuthStateChanged() {
if (Provider.of<UserAccount>(context, listen: false).auth != null && _page != _SubPage.main) {
setState(() {
_page = _SubPage.main;
});
}
}
@override
Widget build(BuildContext context) {
return Consumer<UserAccount>(
builder: (context, acc, child) {
switch (_page) {
case _SubPage.main:
2024-02-11 02:20:48 +01:00
return Center(
2024-02-11 01:08:51 +01:00
child: Text(
2024-02-11 02:20:48 +01:00
'Logged In: ${acc.auth?.userId}',
style: const TextStyle(fontSize: 24),
2024-02-11 01:08:51 +01:00
),
);
case _SubPage.chooseAuth:
return AccountChoosePage(
onLogin: () => setState(() {
_page = _SubPage.login;
}),
onCreateAccount: () => setState(() {
//TODO
}),
);
case _SubPage.login:
2024-02-11 02:20:48 +01:00
return AccountLoginPage(
onLogin: () => setState(() {
_page = _SubPage.main;
}),
);
2024-02-11 01:08:51 +01:00
}
},
);
}
}