frosty/lib/screens/settings/widgets/profile_card.dart
2022-01-09 12:53:40 -05:00

106 lines
3.3 KiB
Dart

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:frosty/core/auth/auth_store.dart';
import 'package:intl/intl.dart';
class ProfileCard extends StatelessWidget {
final AuthStore authStore;
const ProfileCard({Key? key, required this.authStore}) : super(key: key);
Future<void> _showLoginDialog(BuildContext context) {
return showDialog(
context: context,
builder: (context) => SimpleDialog(
title: const Text('Log In With'),
children: [
SimpleDialogOption(
child: const Text('Twitch Account'),
onPressed: () {
authStore.login();
Navigator.pop(context);
},
),
const Padding(
padding: EdgeInsets.all(10.0),
child: Center(child: Text('Or')),
),
SimpleDialogOption(
child: TextField(
decoration: const InputDecoration(
hintText: 'Token',
),
onSubmitted: (token) {
authStore.login(customToken: token);
Navigator.pop(context);
},
),
)
],
),
);
}
Future<void> _showLogoutDialog(BuildContext context) {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Log Out'),
content: const Text('Are you sure you want to log out?'),
actions: [
TextButton(
onPressed: Navigator.of(context).pop,
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
authStore.logout();
Navigator.pop(context);
},
child: const Text('Yes'),
style: TextButton.styleFrom(primary: Colors.red),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Center(
child: Observer(
builder: (context) {
if (authStore.isLoggedIn && authStore.user.details != null) {
return ListTile(
leading: CircleAvatar(
backgroundColor: const Color(0xFFFFFFFF),
foregroundImage: CachedNetworkImageProvider(authStore.user.details!.profileImageUrl),
),
title: Text(authStore.user.details!.displayName),
subtitle: Text('Joined on ${DateFormat.yMMMd().format(DateTime.parse(authStore.user.details!.createdAt))}'),
trailing: OutlinedButton(
onPressed: () => _showLogoutDialog(context),
child: const Text('Log Out'),
style: OutlinedButton.styleFrom(primary: Colors.red),
),
);
}
return ListTile(
isThreeLine: true,
leading: const Icon(
Icons.account_circle_outlined,
size: 40,
),
title: const Text('Anonymous User'),
subtitle: const Text('Log in to view your followed streams, send chat messages, and more.'),
trailing: OutlinedButton(
onPressed: () => _showLoginDialog(context),
child: const Text('Log In'),
),
);
},
),
);
}
}