✨ Auth guard
This commit is contained in:
101
lib/widgets/name_card.dart
Normal file
101
lib/widgets/name_card.dart
Normal file
@ -0,0 +1,101 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
import '../auth.dart';
|
||||
|
||||
class NameCard extends StatelessWidget {
|
||||
const NameCard({super.key, this.onLogin, this.onCheck});
|
||||
|
||||
final void Function()? onLogin;
|
||||
final void Function()? onCheck;
|
||||
|
||||
Future<CircleAvatar> _getAvatar() async {
|
||||
if (await AuthGuard().isAuthorized()) {
|
||||
final profiles = await AuthGuard().readProfiles();
|
||||
return CircleAvatar(backgroundImage: NetworkImage(profiles["picture"]));
|
||||
} else {
|
||||
return const CircleAvatar(child: Icon(Icons.account_circle));
|
||||
}
|
||||
}
|
||||
|
||||
Future<Column> _getDescribe() async {
|
||||
if (await AuthGuard().isAuthorized()) {
|
||||
final profiles = await AuthGuard().readProfiles();
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
profiles["nick"],
|
||||
style: const TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text(profiles["email"])
|
||||
],
|
||||
);
|
||||
} else {
|
||||
return const Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"Unauthorized",
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Text("Click here to login in.")
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: InkWell(
|
||||
splashColor: Colors.indigo.withAlpha(30),
|
||||
onTap: () async {
|
||||
if (await AuthGuard().isAuthorized() && onCheck != null) {
|
||||
onCheck!();
|
||||
} else if (onLogin != null) {
|
||||
onLogin!();
|
||||
}
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Row(
|
||||
children: [
|
||||
FutureBuilder<CircleAvatar>(
|
||||
future: _getAvatar(),
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<Widget> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return snapshot.data!;
|
||||
} else {
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 20),
|
||||
FutureBuilder<Column>(
|
||||
future: _getDescribe(),
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<Column> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return snapshot.data!;
|
||||
} else {
|
||||
return const Column();
|
||||
}
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user