ft/adds automatic download of data in initState

and more
This commit is contained in:
itsscb 2023-10-26 23:29:22 +02:00
parent 35d8ed0221
commit a8377110bb
5 changed files with 138 additions and 64 deletions

View File

@ -1,3 +1,4 @@
import 'package:fixnum/fixnum.dart';
import 'package:app/pb/rpc_create_account.pb.dart';
import 'package:app/pb/rpc_get_account_info.pb.dart';
import 'package:app/pb/rpc_login.pb.dart';
@ -8,8 +9,9 @@ class Client {
String baseUrl = 'localhost';
int port = 9090;
Map<String, dynamic> metadata = {'Authorization': ''};
Map<String, String> metadata = {'Authorization': ''};
String accessToken = '';
Int64 accountId = Int64();
dfClient stub = dfClient(
ClientChannel('10.0.2.2',
@ -25,7 +27,6 @@ class Client {
Future<CreateAccountResponse> createAccount(
CreateAccountRequest request) async {
// CreateAccountResponse response;
try {
final response = stub.createAccount(request);
return response;
@ -47,6 +48,7 @@ class Client {
password: password,
));
accessToken = response.accessToken;
accountId = response.accountId;
metadata['Authorization'] = 'Bearer ${response.accessToken}';
print('auth: ${metadata['Authorization']}');
onSuccess();
@ -64,9 +66,9 @@ class Client {
}
Future<GetAccountInfoResponse> getAccountInfo(GetAccountInfoRequest request,
{required String token, required Function onError}) async {
{required Function onError}) async {
try {
Map<String, String> metadata = {'Authorization': 'Bearer $token'};
// Map<String, String> metadata = {'Authorization': 'Bearer $token'};
final response = await stub.getAccountInfo(
request,
options: CallOptions(

View File

@ -8,6 +8,19 @@ void main() async {
runApp(
MaterialApp(
theme: ThemeData().copyWith(
colorScheme: const ColorScheme(
brightness: Brightness.dark,
primary: Colors.white,
onPrimary: Colors.black,
secondary: Colors.black,
onSecondary: Colors.white,
error: Color.fromARGB(170, 255, 0, 0),
onError: Color.fromARGB(170, 255, 0, 0),
background: Colors.transparent,
onBackground: Colors.white,
surface: Colors.black,
onSurface: Colors.white,
),
textTheme: const TextTheme().copyWith(
titleLarge: const TextStyle(
color: Colors.white,
@ -21,7 +34,7 @@ void main() async {
),
inputDecorationTheme: const InputDecorationTheme(
labelStyle: TextStyle(
color: Colors.white,
color: Colors.grey,
),
),
scaffoldBackgroundColor: Colors.transparent,

View File

@ -1,5 +1,8 @@
import 'package:app/gapi/client.dart';
import 'package:app/pb/account_info.pb.dart';
import 'package:app/pb/rpc_get_account_info.pb.dart';
import 'package:app/widgets/background.dart';
import 'package:app/widgets/loading_widget.dart';
import 'package:flutter/material.dart';
class DashboardPage extends StatefulWidget {
@ -13,6 +16,7 @@ class DashboardPage extends StatefulWidget {
class _DashboardPageState extends State<DashboardPage> {
bool _loading = false;
late AccountInfo accountInfo;
void _setLoading(bool loading) {
setState(() {
@ -25,16 +29,68 @@ class _DashboardPageState extends State<DashboardPage> {
super.initState();
if (widget.client.accessToken == '') {
Navigator.of(context).pop();
return;
}
_setLoading(true);
widget.client.getAccountInfo(
GetAccountInfoRequest(
accountId: widget.client.accountId,
),
onError: () {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('AccountInfo konnte nicht geladen werden'),
));
},
).then((value) {
accountInfo = value.accountInfo;
_setLoading(false);
});
}
@override
Widget build(BuildContext context) {
print(widget.client.accessToken);
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
items: const [
BottomNavigationBarItem(
label: '',
backgroundColor: Colors.white,
icon: Icon(
Icons.arrow_back,
color: Colors.white,
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: '',
icon: Icon(
Icons.person,
color: Colors.white,
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: '',
icon: Icon(
Icons.group,
color: Colors.white,
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: '',
icon: Icon(
Icons.file_copy,
color: Colors.white,
),
),
],
backgroundColor: Colors.transparent,
),
appBar: AppBar(
automaticallyImplyLeading: false,
// backgroundColor: Colors.black,
flexibleSpace: Image.asset(
'lib/assets/logo_300x200.png',
height: 80,
@ -52,18 +108,25 @@ class _DashboardPageState extends State<DashboardPage> {
// ),
// ],
),
body: Background(
child: Column(
children: [
Text(
widget.client.accessToken,
style: const TextStyle(
color: Colors.white,
body: !_loading
? Background(
child: Center(
child: Column(
children: [
const SizedBox(
height: 48,
),
Text(
'Willkommen ${accountInfo.firstname} ${accountInfo.lastname}!',
style: const TextStyle(
fontSize: 24,
),
),
],
),
),
),
],
),
),
)
: const LoadingWidget(),
);
}
}

View File

@ -1,6 +1,6 @@
import 'package:app/gapi/client.dart';
import 'package:app/pages/dashboard_page.dart';
import 'package:app/pb/rpc_login.pb.dart';
import 'package:app/widgets/loading_widget.dart';
import 'package:flutter/material.dart';
class LoginPage extends StatefulWidget {
@ -25,28 +25,6 @@ class _LoginPageState extends State<LoginPage> {
final mailController = TextEditingController();
final passwordController = TextEditingController();
// @override
// void initState() {
// super.initState();
// }
// Future<String> _login(
// {required BuildContext context,
// required String email,
// required String password,
// required Function onSuccess,
// required Function onError}) async {
// LoginResponse r = await client.login(
// LoginRequest(
// email: email,
// password: password,
// ),
// onError: onError,
// onSuccess: onSuccess,
// );
// return r.accessToken;
// }
@override
Widget build(BuildContext context) {
return Scaffold(
@ -56,18 +34,27 @@ class _LoginPageState extends State<LoginPage> {
'lib/assets/logo_300x200.png',
height: 80,
),
// actions: [
// IconButton(
// onPressed: () {},
// icon: const Icon(Icons.menu),
// tooltip: 'Menu',
// ),
// IconButton(
// onPressed: () {},
// icon: const Icon(Icons.login_sharp),
// tooltip: 'Login',
// ),
// ],
),
bottomNavigationBar: BottomNavigationBar(
items: const [
BottomNavigationBarItem(
label: 'Zurueck',
backgroundColor: Colors.white,
icon: Icon(
Icons.arrow_back,
color: Colors.white,
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: 'AccountInf',
icon: Icon(
Icons.person,
color: Colors.white,
),
),
],
backgroundColor: Colors.transparent,
),
body: !_loading
? Form(
@ -77,9 +64,9 @@ class _LoginPageState extends State<LoginPage> {
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextFormField(
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
// style: TextStyle(
// color: Theme.of(context).colorScheme.primary,
// ),
controller: mailController,
decoration: const InputDecoration(
fillColor: Color.fromARGB(30, 255, 255, 255),
@ -164,15 +151,7 @@ class _LoginPageState extends State<LoginPage> {
],
),
)
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'lib/assets/logo_300x200.png',
height: 300,
),
],
),
: const LoadingWidget(),
);
}
}

View File

@ -0,0 +1,17 @@
import 'package:flutter/material.dart';
class LoadingWidget extends StatelessWidget {
const LoadingWidget({
super.key,
});
@override
Widget build(BuildContext context) {
return Center(
child: Image.asset(
'lib/assets/logo_300x200.png',
height: 300,
),
);
}
}