mirror of https://github.com/evanferrao/mynotes
144 lines
4.2 KiB
Dart
144 lines
4.2 KiB
Dart
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:mynotes/constants/routes.dart';
|
|
import 'package:mynotes/firebase_options.dart';
|
|
import 'package:mynotes/views/login_view.dart';
|
|
import 'package:mynotes/views/register_view.dart';
|
|
import 'package:mynotes/views/verify_email_view.dart';
|
|
import 'dart:developer' as devtools show log;
|
|
|
|
void main() {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
runApp(
|
|
MaterialApp(
|
|
title: 'Flutter Demo',
|
|
theme: ThemeData(
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
|
useMaterial3: true,
|
|
),
|
|
home: const HomePage(),
|
|
routes: {
|
|
loginRoute: (context) => const LoginView(),
|
|
registerRoute: (context) => const RegisterView(),
|
|
notesRoute: (context) => const NotesView(),
|
|
verifyEmailRoute: (context) => const VerifyEmailView(),
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
class HomePage extends StatelessWidget {
|
|
const HomePage({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return FutureBuilder(
|
|
future: Firebase.initializeApp(
|
|
options: DefaultFirebaseOptions.currentPlatform,
|
|
),
|
|
builder: (context, snapshot) {
|
|
switch (snapshot.connectionState) {
|
|
case ConnectionState.done:
|
|
final user = FirebaseAuth.instance.currentUser;
|
|
if (user != null) {
|
|
if (user.emailVerified) {
|
|
return const NotesView();
|
|
} else {
|
|
return const VerifyEmailView();
|
|
}
|
|
} else {
|
|
return const LoginView();
|
|
}
|
|
|
|
default:
|
|
return const Scaffold(
|
|
body: Center(
|
|
child: CircularProgressIndicator(),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
enum MenuAction { logout }
|
|
|
|
class NotesView extends StatefulWidget {
|
|
const NotesView({super.key});
|
|
|
|
@override
|
|
State<NotesView> createState() => _NotesViewState();
|
|
}
|
|
|
|
class _NotesViewState extends State<NotesView> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Main UI', style: TextStyle(color: Colors.white)),
|
|
backgroundColor: Colors.blue,
|
|
actions: [
|
|
// Add the 'actions' named parameter here
|
|
PopupMenuButton<MenuAction>(
|
|
icon: const Icon(Icons.more_vert, color: Colors.white),
|
|
onSelected: (value) async {
|
|
switch (value) {
|
|
case MenuAction.logout:
|
|
final shouldLogout = await showLogOutDialog(context);
|
|
devtools.log('shouldLogout: $shouldLogout');
|
|
if (shouldLogout) {
|
|
await FirebaseAuth.instance.signOut();
|
|
// ignore: use_build_context_synchronously
|
|
Navigator.of(context).pushNamedAndRemoveUntil(
|
|
loginRoute,
|
|
(route) => false,
|
|
);
|
|
}
|
|
}
|
|
},
|
|
itemBuilder: (context) {
|
|
return const [
|
|
PopupMenuItem<MenuAction>(
|
|
value: MenuAction.logout,
|
|
child: Text('Logout'),
|
|
)
|
|
];
|
|
})
|
|
],
|
|
),
|
|
body: const Center(
|
|
child: Text('Welcome to the main UI'),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<bool> showLogOutDialog(BuildContext context) {
|
|
return showDialog<bool>(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
title: const Text('Sign Out'),
|
|
content: const Text('Are you sure you want to sign out?'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
// Navigator.pop(context, false); NOT recommended method
|
|
Navigator.of(context).pop(false); // recommended method
|
|
},
|
|
child: const Text('Cancel'),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop(true);
|
|
},
|
|
child: const Text('Logout'),
|
|
)
|
|
],
|
|
);
|
|
},
|
|
).then((value) => value ?? false);
|
|
}
|