rf/extracts bottomnavigationbar & drawer

This commit is contained in:
itsscb 2023-10-30 22:48:37 +01:00
parent 0c0b0b4594
commit f41236e304
7 changed files with 346 additions and 417 deletions

View File

@ -2,7 +2,6 @@ import 'dart:async';
import 'package:fixnum/fixnum.dart'; import 'package:fixnum/fixnum.dart';
import 'package:app/pb/google/protobuf/timestamp.pb.dart'; import 'package:app/pb/google/protobuf/timestamp.pb.dart';
import 'package:flutter/services.dart';
import 'package:path/path.dart'; import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart'; import 'package:sqflite/sqflite.dart';
@ -49,7 +48,7 @@ class Session {
} }
Future<Database> _initDatabase() async { Future<Database> _initDatabase() async {
print('DB: INITIALIZING - start'); // print('DB: INITIALIZING - start');
_database = await openDatabase( _database = await openDatabase(
join(await getDatabasesPath(), 'df_database.db'), join(await getDatabasesPath(), 'df_database.db'),
onCreate: (db, version) { onCreate: (db, version) {
@ -59,7 +58,7 @@ class Session {
}, },
version: 1, version: 1,
); );
print('DB: INITIALIZING - end'); // print('DB: INITIALIZING - end');
return _database; return _database;
} }
@ -81,19 +80,25 @@ class Session {
} }
Future<void> insertSession(Session session) async { Future<void> insertSession(Session session) async {
print('INSERTING SESSION: ${session.sessionId}'); // print('INSERTING SESSION: ${session.sessionId}');
final db = _database; final db = _database;
final result = await db.insert( await db.insert(
'sessions', 'sessions',
session.toMap(), session.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace, conflictAlgorithm: ConflictAlgorithm.replace,
); );
print('INSERT RESULT: $result'); // print('INSERT RESULT: $result');
} }
Future<void> removeSession(String sessionId) async { Future<void> removeSession(String sessionId) async {
final db = _database; final db = _database;
await db.delete('sessions', where: 'sessionId = ?', whereArgs: [sessionId]); await db.delete('sessions', where: 'sessionId = ?', whereArgs: [sessionId]);
this.sessionId = null;
refreshToken = null;
accessTokenExpiresAt = null;
refreshTokenExpiresAt = null;
accountId = null;
accessToken = null;
} }
Future<List<Session>> getSessions() async { Future<List<Session>> getSessions() async {
@ -104,7 +109,7 @@ class Session {
final List<Session> sessions = List.generate( final List<Session> sessions = List.generate(
maps.length, maps.length,
(i) { (i) {
print('GOT MAP: ${maps[i]}'); // print('GOT MAP: ${maps[i]}');
return Session( return Session(
sessionId: maps[i]['sessionId'] as String, sessionId: maps[i]['sessionId'] as String,

View File

@ -8,7 +8,6 @@ import 'package:grpc/grpc.dart';
class GClient { class GClient {
GClient() { GClient() {
// session = Session.newSession();
_init(); _init();
} }
@ -16,8 +15,6 @@ class GClient {
int port = 9090; int port = 9090;
Map<String, String> metadata = {'Authorization': ''}; Map<String, String> metadata = {'Authorization': ''};
// String accessToken = '';
// Int64 accountId = Int64();
late Session session; late Session session;
@ -43,16 +40,10 @@ class GClient {
Future<void> main(List<String> args) async {} Future<void> main(List<String> args) async {}
void _init() async { void _init() async {
// print('CLIENT: INITIALIZING CLIENT - start');
session = await Session.newSession; session = await Session.newSession;
// print('CLIENT: getting sessions from database');
final sessions = await session.getSessions(); final sessions = await session.getSessions();
print('CLIENT: got sessions from database: ${sessions.toString()}');
session = sessions[0]; session = sessions[0];
// print('CLIENT: INITIALIZING CLIENT - end');
print(session.toString());
} }
Future<CreateAccountResponse> createAccount( Future<CreateAccountResponse> createAccount(
@ -60,9 +51,7 @@ class GClient {
try { try {
final response = stub.createAccount(request); final response = stub.createAccount(request);
return response; return response;
} catch (e) { } catch (e) {}
print('caught error: $e');
}
return CreateAccountResponse(); return CreateAccountResponse();
} }
@ -77,23 +66,18 @@ class GClient {
email: email, email: email,
password: password, password: password,
)); ));
print(response);
session.accessToken = response.accessToken; session.accessToken = response.accessToken;
session.accountId = response.accountId; session.accountId = response.accountId;
session.sessionId = response.sessionId; session.sessionId = response.sessionId;
session.refreshToken = response.refreshToken; session.refreshToken = response.refreshToken;
session.accessTokenExpiresAt = response.accessTokenExpiresAt; session.accessTokenExpiresAt = response.accessTokenExpiresAt;
session.refreshTokenExpiresAt = response.refreshTokenExpiresAt; session.refreshTokenExpiresAt = response.refreshTokenExpiresAt;
print('GOT: ${session.toString()}');
try { try {
session.insertSession(session); session.insertSession(session);
} catch (err) { } catch (err) {}
print('ERROR WRITING DB: $err');
}
metadata['Authorization'] = 'Bearer ${response.accessToken}'; metadata['Authorization'] = 'Bearer ${response.accessToken}';
print('auth: ${metadata['Authorization']}');
onSuccess(); onSuccess();
// return response; return response;
} on GrpcError catch (e) { } on GrpcError catch (e) {
print('caught error: ${e.message}'); print('caught error: ${e.message}');
metadata['Authorization'] = ''; metadata['Authorization'] = '';
@ -109,7 +93,6 @@ class GClient {
Future<GetAccountInfoResponse> getAccountInfo(GetAccountInfoRequest request, Future<GetAccountInfoResponse> getAccountInfo(GetAccountInfoRequest request,
{required Function onError}) async { {required Function onError}) async {
try { try {
// Map<String, String> metadata = {'Authorization': 'Bearer $token'};
final response = await stub.getAccountInfo( final response = await stub.getAccountInfo(
request, request,
options: CallOptions( options: CallOptions(

View File

@ -35,7 +35,7 @@ class _DashboardPageState extends State<DashboardPage> {
_setLoading(true); _setLoading(true);
widget.client.getAccountInfo( widget.client.getAccountInfo(
GetAccountInfoRequest( GetAccountInfoRequest(
accountId: widget.client.session?.accountId, accountId: widget.client.session.accountId,
), ),
onError: () { onError: () {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar( ScaffoldMessenger.of(context).showSnackBar(const SnackBar(

View File

@ -1,7 +1,9 @@
import 'package:app/gapi/client.dart'; import 'package:app/gapi/client.dart';
import 'package:app/pages/start_page.dart'; import 'package:app/pages/start_page.dart';
import 'package:app/widgets/background.dart'; import 'package:app/widgets/background.dart';
import 'package:app/widgets/bottom_bar.dart';
import 'package:app/widgets/loading_widget.dart'; import 'package:app/widgets/loading_widget.dart';
import 'package:app/widgets/side_drawer.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:grpc/grpc.dart'; import 'package:grpc/grpc.dart';
@ -84,8 +86,92 @@ class _LoginPageState extends State<LoginPage> {
// height: 80, // height: 80,
// ), // ),
), ),
bottomNavigationBar: const BottomBar(), bottomNavigationBar: BottomBar(
drawer: const SideDrawer(), children: [
BottomNavigationBarItem(
label: 'back',
backgroundColor: Colors.white,
icon: IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: const Icon(
Icons.arrow_back,
color: Colors.white,
),
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: 'Menu',
icon: IconButton(
onPressed: () => Scaffold.of(context).openDrawer(),
icon: const Icon(
Icons.menu,
color: Colors.white,
),
),
),
],
),
drawer: SideDrawer(children: [
const Spacer(),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'About',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.question_answer,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'Datenschutz',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.privacy_tip,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'Impressum',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.apartment,
color: Colors.white,
),
],
),
),
const SizedBox(
height: 250,
)
]),
body: !_loading body: !_loading
? Form( ? Form(
key: _formKey, key: _formKey,
@ -256,121 +342,3 @@ class _LoginPageState extends State<LoginPage> {
); );
} }
} }
class SideDrawer extends StatelessWidget {
const SideDrawer({
super.key,
});
@override
Widget build(BuildContext context) {
return Drawer(
backgroundColor: Colors.black,
child: Padding(
padding: const EdgeInsets.all(40.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Spacer(),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'About',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.question_answer,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'Datenschutz',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.privacy_tip,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'Impressum',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.apartment,
color: Colors.white,
),
],
),
),
const SizedBox(
height: 250,
)
],
),
),
);
}
}
class BottomBar extends StatelessWidget {
const BottomBar({
super.key,
});
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
items: [
BottomNavigationBarItem(
label: 'back',
backgroundColor: Colors.white,
icon: IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: const Icon(
Icons.arrow_back,
color: Colors.white,
),
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: 'Menu',
icon: IconButton(
onPressed: () => Scaffold.of(context).openDrawer(),
icon: const Icon(
Icons.menu,
color: Colors.white,
),
),
),
],
backgroundColor: Colors.black,
fixedColor: Colors.black,
// onTap: (value) => _bottomBarAction(value),
);
}
}

View File

@ -1,6 +1,8 @@
import 'package:app/gapi/client.dart'; import 'package:app/gapi/client.dart';
import 'package:app/pages/login_page.dart'; import 'package:app/pages/login_page.dart';
import 'package:app/widgets/background.dart'; import 'package:app/widgets/background.dart';
import 'package:app/widgets/bottom_bar.dart';
import 'package:app/widgets/side_drawer.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:core'; import 'dart:core';
@ -40,15 +42,194 @@ class _StartPageState extends State<StartPage> {
appBar: AppBar( appBar: AppBar(
automaticallyImplyLeading: false, automaticallyImplyLeading: false,
), ),
drawer: SideDrawer( drawer: SideDrawer(children: [
onLogout: () { const Spacer(),
setState(() { TextButton(
widget.client?.session onPressed: () {
.removeSession(widget.client!.session.sessionId!); Scaffold.of(context).closeDrawer();
}); },
}, child: const Row(
), children: [
bottomNavigationBar: BottomBar(widget: widget), Text(
'About',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.question_answer,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'Datenschutz',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.privacy_tip,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'Impressum',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.apartment,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
setState(() {
widget.client?.session.accessToken = null;
widget.client?.session
.removeSession(widget.client!.session.sessionId!);
});
},
child: const Row(
children: [
Text(
'Log out',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.logout,
color: Colors.white,
),
],
),
),
const SizedBox(
height: 250,
)
]),
bottomNavigationBar: Builder(builder: (context) {
return BottomBar(
// onTap: (value) => _bottomBarAction(value),
children: widget.client?.session.accessToken != null
? [
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: 'Personen',
icon: Column(
children: [
IconButton(
onPressed: () => Scaffold.of(context).openDrawer(),
icon: const Icon(
Icons.group,
color: Colors.white,
),
),
const Text(
'Personen',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
)
],
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: 'Menu',
icon: IconButton(
onPressed: () {
Scaffold.of(context).openDrawer();
},
icon: const Icon(
Icons.menu,
color: Colors.white,
),
),
)
]
: [
BottomNavigationBarItem(
label: 'register',
backgroundColor: Colors.white,
icon: Column(
children: [
IconButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => LoginPage()));
},
icon: const Icon(
Icons.login,
color: Colors.white,
),
),
const Text(
'Registrieren',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
)
],
),
),
BottomNavigationBarItem(
label: 'login',
backgroundColor: Colors.white,
icon: Column(
children: [
IconButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => LoginPage()));
},
icon: const Icon(
Icons.login,
color: Colors.white,
),
),
const Text(
'Login',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
)
],
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: 'Menu',
icon: IconButton(
onPressed: () => Scaffold.of(context).openDrawer(),
icon: const Icon(
Icons.menu,
color: Colors.white,
),
),
),
],
);
}),
body: Padding( body: Padding(
padding: const EdgeInsets.all(16.0), padding: const EdgeInsets.all(16.0),
child: Column( child: Column(
@ -74,54 +255,14 @@ class _StartPageState extends State<StartPage> {
), ),
), ),
TextButton( TextButton(
onPressed: () { onPressed: () async {
final s = widget.client?.session.getSessions(); final s = await widget.client?.session.getSessions();
print(s); print('SESSIONS: $s');
print(widget.client?.session.accessToken);
}, },
child: const Text( child: const Text(
"GET SESSIONS", "GET SESSIONS",
), ),
), ),
// const Spacer(),
// Row(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// crossAxisAlignment: CrossAxisAlignment.center,
// children: widget.client?.accessToken == null
// ? [
// ElevatedButton.icon(
// label: const Text('Login'),
// onPressed: () {
// // onChangePage(Pages.login);
// Navigator.of(context).push(
// MaterialPageRoute(
// builder: ((context) => LoginPage()),
// ),
// );
// },
// icon: const Icon(
// Icons.login,
// semanticLabel: 'Login',
// size: 16,
// ),
// ),
// ElevatedButton.icon(
// label: const Text('Registrieren'),
// onPressed: () {},
// icon: const Icon(
// Icons.person_add,
// semanticLabel: 'Register',
// size: 16,
// ),
// ),
// ]
// : [],
// ),
// const SizedBox(
// height: 38,
// ),
// const Text('data'),
], ],
), ),
), ),
@ -129,220 +270,3 @@ class _StartPageState extends State<StartPage> {
); );
} }
} }
class SideDrawer extends StatelessWidget {
SideDrawer({
super.key,
required this.onLogout,
});
Function() onLogout;
@override
Widget build(BuildContext context) {
return Drawer(
backgroundColor: Colors.black,
child: Padding(
padding: const EdgeInsets.all(40.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Spacer(),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'About',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.question_answer,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'Datenschutz',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.privacy_tip,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
Scaffold.of(context).closeDrawer();
},
child: const Row(
children: [
Text(
'Impressum',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.apartment,
color: Colors.white,
),
],
),
),
TextButton(
onPressed: () {
onLogout();
},
child: const Row(
children: [
Text(
'Log out',
style: TextStyle(fontSize: 20),
),
Spacer(),
Icon(
Icons.logout,
color: Colors.white,
),
],
),
),
const SizedBox(
height: 250,
)
],
),
),
);
}
}
class BottomBar extends StatelessWidget {
const BottomBar({
super.key,
required this.widget,
});
final StartPage widget;
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
// onTap: (value) => _bottomBarAction(value),
items: widget.client?.session.accessToken != null
? [
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: 'Personen',
icon: Column(
children: [
IconButton(
onPressed: () => Scaffold.of(context).openDrawer(),
icon: const Icon(
Icons.group,
color: Colors.white,
),
),
const Text(
'Personen',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
)
],
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: 'Menu',
icon: IconButton(
onPressed: () => Scaffold.of(context).openDrawer(),
icon: const Icon(
Icons.menu,
color: Colors.white,
),
),
)
]
: [
BottomNavigationBarItem(
label: 'register',
backgroundColor: Colors.white,
icon: Column(
children: [
IconButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => LoginPage()));
},
icon: const Icon(
Icons.login,
color: Colors.white,
),
),
const Text(
'Registrieren',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
)
],
),
),
BottomNavigationBarItem(
label: 'login',
backgroundColor: Colors.white,
icon: Column(
children: [
IconButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => LoginPage()));
},
icon: const Icon(
Icons.login,
color: Colors.white,
),
),
const Text(
'Login',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
)
],
),
),
BottomNavigationBarItem(
backgroundColor: Colors.white,
label: 'Menu',
icon: IconButton(
onPressed: () => Scaffold.of(context).openDrawer(),
icon: const Icon(
Icons.menu,
color: Colors.white,
),
),
),
],
backgroundColor: Colors.black,
fixedColor: Colors.black,
);
}
}

View File

@ -0,0 +1,20 @@
import 'package:flutter/material.dart';
class BottomBar extends StatelessWidget {
const BottomBar({
super.key,
required this.children,
});
final List<BottomNavigationBarItem> children;
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
items: children,
backgroundColor: Colors.black,
fixedColor: Colors.black,
// onTap: (value) => _bottomBarAction(value),
);
}
}

View File

@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
class SideDrawer extends StatefulWidget {
const SideDrawer({
super.key,
required this.children,
});
final List<Widget> children;
@override
State<SideDrawer> createState() => _SideDrawerState();
}
class _SideDrawerState extends State<SideDrawer> {
@override
Widget build(BuildContext context) {
return Drawer(
backgroundColor: Colors.black,
child: Padding(
padding: const EdgeInsets.all(40.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: widget.children,
),
),
);
}
}