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

View File

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

View File

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

View File

@ -1,7 +1,9 @@
import 'package:app/gapi/client.dart';
import 'package:app/pages/start_page.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/side_drawer.dart';
import 'package:flutter/material.dart';
import 'package:grpc/grpc.dart';
@ -84,8 +86,92 @@ class _LoginPageState extends State<LoginPage> {
// height: 80,
// ),
),
bottomNavigationBar: const BottomBar(),
drawer: const SideDrawer(),
bottomNavigationBar: BottomBar(
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
? Form(
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/pages/login_page.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 'dart:core';
@ -40,15 +42,194 @@ class _StartPageState extends State<StartPage> {
appBar: AppBar(
automaticallyImplyLeading: false,
),
drawer: SideDrawer(
onLogout: () {
setState(() {
widget.client?.session
.removeSession(widget.client!.session.sessionId!);
});
},
),
bottomNavigationBar: BottomBar(widget: widget),
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,
),
],
),
),
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(
padding: const EdgeInsets.all(16.0),
child: Column(
@ -74,54 +255,14 @@ class _StartPageState extends State<StartPage> {
),
),
TextButton(
onPressed: () {
final s = widget.client?.session.getSessions();
print(s);
print(widget.client?.session.accessToken);
onPressed: () async {
final s = await widget.client?.session.getSessions();
print('SESSIONS: $s');
},
child: const Text(
"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,
),
),
);
}
}