basic api access, state managment etc
This commit is contained in:
34
flutter/lib/pages/account/choose_auth.dart
Normal file
34
flutter/lib/pages/account/choose_auth.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AccountChoosePage extends StatelessWidget {
|
||||
final void Function()? onLogin;
|
||||
final void Function()? onCreateAccount;
|
||||
|
||||
const AccountChoosePage({super.key, this.onLogin, this.onCreateAccount});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
|
||||
onPressed: () {
|
||||
onLogin?.call();
|
||||
},
|
||||
child: const Text('Use existing account'),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
|
||||
onPressed: () {
|
||||
onCreateAccount?.call();
|
||||
},
|
||||
child: const Text('Create new account'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
85
flutter/lib/pages/account/login.dart
Normal file
85
flutter/lib/pages/account/login.dart
Normal file
@@ -0,0 +1,85 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:simplecloudnotifier/api/api_client.dart';
|
||||
|
||||
class AccountLoginPage extends StatefulWidget {
|
||||
const AccountLoginPage({super.key});
|
||||
|
||||
@override
|
||||
State<AccountLoginPage> createState() => _AccountLoginPageState();
|
||||
}
|
||||
|
||||
class _AccountLoginPageState extends State<AccountLoginPage> {
|
||||
late TextEditingController _ctrlUserID;
|
||||
late TextEditingController _ctrlToken;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_ctrlUserID = TextEditingController();
|
||||
_ctrlToken = TextEditingController();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_ctrlUserID.dispose();
|
||||
_ctrlToken.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 250,
|
||||
child: TextField(
|
||||
controller: _ctrlUserID,
|
||||
decoration: const InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: 'UserID',
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SizedBox(
|
||||
width: 250,
|
||||
child: TextField(
|
||||
controller: _ctrlToken,
|
||||
decoration: const InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: 'Token',
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
|
||||
onPressed: _login,
|
||||
child: const Text('Login'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _login() async {
|
||||
final msgr = ScaffoldMessenger.of(context);
|
||||
|
||||
final verified = await APIClient.verifyToken(_ctrlUserID.text, _ctrlToken.text);
|
||||
if (verified) {
|
||||
msgr.showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Data ok'),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
msgr.showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Failed to verify token'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
72
flutter/lib/pages/account/root.dart
Normal file
72
flutter/lib/pages/account/root.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
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;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
var prov = Provider.of<UserAccount>(context, listen: false);
|
||||
|
||||
_page = (prov.auth != null) ? _SubPage.main : _SubPage.chooseAuth;
|
||||
|
||||
prov.addListener(_onAuthStateChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
Provider.of<UserAccount>(context, listen: false).removeListener(_onAuthStateChanged);
|
||||
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:
|
||||
return const Center(
|
||||
child: Text(
|
||||
'Logged In',
|
||||
style: TextStyle(fontSize: 24),
|
||||
),
|
||||
);
|
||||
case _SubPage.chooseAuth:
|
||||
return AccountChoosePage(
|
||||
onLogin: () => setState(() {
|
||||
_page = _SubPage.login;
|
||||
}),
|
||||
onCreateAccount: () => setState(() {
|
||||
//TODO
|
||||
}),
|
||||
);
|
||||
case _SubPage.login:
|
||||
return const AccountLoginPage();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user