Skip to content

Commit

Permalink
auth1
Browse files Browse the repository at this point in the history
  • Loading branch information
mo7amedaliEbaid committed May 27, 2024
1 parent 22202e8 commit 76e1684
Show file tree
Hide file tree
Showing 12 changed files with 697 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/core/router/routers_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:recipes/features/drinks/infrastructure/dto/drink_details/drink_d
import 'package:recipes/features/home/presentation/screens/home.dart';
import 'package:recipes/features/drinks/presentation/screens/drinks_by_category.dart';
import 'package:recipes/features/on_boarding/presentation/screens/on_boarding_screen.dart';
import 'package:recipes/features/register/presentation/screens/register.dart';

import '../../features/drinks/presentation/screens/drinkDetails.dart';
import '../../features/favourites/presentation/screens/favourites.dart';
Expand Down
8 changes: 7 additions & 1 deletion lib/features/home/presentation/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:recipes/features/categories/presentation/widgets/category_item.d
import 'package:recipes/features/drinks/infrastructure/dto/drink_details/drink_details_model.dart';
import 'package:recipes/features/home/presentation/widgets/common_row.dart';
import 'package:recipes/features/home/presentation/widgets/custom_app_bar.dart';
import 'package:recipes/features/home/presentation/widgets/custom_drawer.dart';
import 'package:recipes/features/home/presentation/widgets/custom_textfield.dart';
import 'package:recipes/features/search/presentation/provider/search_value_provider.dart';
import '../../../drinks/presentation/riverpod/drink_details/selected_drink_provider.dart';
Expand All @@ -29,6 +30,7 @@ class HomeScreen extends ConsumerStatefulWidget {

class HomeScreenState extends ConsumerState<HomeScreen> {
final TextEditingController controller = TextEditingController();
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
Expand All @@ -50,8 +52,12 @@ class HomeScreenState extends ConsumerState<HomeScreen> {
final searchState = ref.watch(searchProvider);
log(AppDimensions.normalize(39.38).toString());
return Scaffold(
key: scaffoldKey,
backgroundColor: Colors.white,
appBar: customAppBar(),
appBar: customAppBar(
scaffoldKey: scaffoldKey,
),
drawer: const CustomDrawer(),
body: SingleChildScrollView(
child: Column(
children: [
Expand Down
22 changes: 16 additions & 6 deletions lib/features/home/presentation/widgets/custom_app_bar.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:recipes/configs/configs.dart';
import 'package:recipes/core/core.dart';

PreferredSizeWidget customAppBar(){
PreferredSizeWidget customAppBar({
required GlobalKey<ScaffoldState> scaffoldKey,
}) {
return PreferredSize(
preferredSize: Size(
double.infinity,
AppDimensions.normalize(35),
),
child: Padding(
padding: Space.all(1.5,2.2),
padding: Space.all(1.5, 2.2),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image.asset(
AppAssets.menu,
width: AppDimensions.normalize(10),
GestureDetector(
onTap: () {
log("drawer tapped");
scaffoldKey.currentState?.openDrawer();
},
child: Image.asset(
AppAssets.menu,
width: AppDimensions.normalize(10),
),
),
Image.asset(
AppAssets.appBar,
Expand All @@ -30,4 +40,4 @@ PreferredSizeWidget customAppBar(){
),
),
);
}
}
97 changes: 97 additions & 0 deletions lib/features/home/presentation/widgets/custom_drawer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:recipes/core/core.dart';

import '../../../login/presentation/screens/login.dart';



class CustomDrawer extends ConsumerWidget {
const CustomDrawer({ super.key});


@override
Widget build(BuildContext context, WidgetRef ref) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: AppColors.deepBlue,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CircleAvatar(
radius: 30,
backgroundImage: AssetImage(AppAssets.profile),
),
SizedBox(height: 10),
Text(
'John Doe',
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
Text(
'johndoe@example.com',
style: TextStyle(
color: Colors.white70,
fontSize: 14,
),
),
],
),
),
ListTile(
leading: const Icon(Icons.home),
title: const Text('Home'),
onTap: () {
Navigator.pop(context); // Close the drawer
context.goNamed(Routes.home.name);
},
),
ListTile(
leading: const Icon(Icons.favorite),
title: const Text('Favorites'),
onTap: () {
Navigator.pop(context); // Close the drawer
context.goNamed(Routes.favorites.name);
},
),
ListTile(
leading: const Icon(Icons.settings),
title: const Text('Settings'),
onTap: () {
Navigator.pop(context); // Close the drawer
// context.goNamed(Routes.settings.name);
},
),
ListTile(
leading: const Icon(Icons.person),
title: const Text('Profile'),
onTap: () {
Navigator.pop(context); // Close the drawer
//context.goNamed(Routes.profile.name);
},
),
const Divider(),
ListTile(
leading: const Icon(Icons.logout),
title: const Text('Logout'),
onTap: () {
/* Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const LoginScreen()),
);*/
// Handle logout
},
),
],
),
);
}
}
100 changes: 100 additions & 0 deletions lib/features/login/presentation/screens/login.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive/hive.dart';
import 'package:recipes/configs/configs.dart';
import 'package:collection/collection.dart';
import '../../../home/presentation/screens/home.dart';
import '../../../register/infrastructure/dto/user_model.dart';

class LoginScreen extends ConsumerStatefulWidget {
const LoginScreen({Key? key}) : super(key: key);

@override
_LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends ConsumerState<LoginScreen> {
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

Future<void> _login() async {
if (_formKey.currentState?.validate() ?? false) {
final Box<User> userBox = Hive.box<User>('users');
final String email = _emailController.text;
final String password = _passwordController.text;

final User? user = userBox.values.firstWhereOrNull(
(user) => user.email == email && user.password == password,
//orElse: () => null,
);

if (user != null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Login Successful')),
);
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Invalid email or password')),
);
}
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
),
body: Padding(
padding: EdgeInsets.all(AppDimensions.normalize(4)),
child: Form(
key: _formKey,
child: ListView(
children: [
TextFormField(
controller: _emailController,
decoration: const InputDecoration(
labelText: 'Email',
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) {
return 'Please enter a valid email';
}
return null;
},
),
Space.yf(1.0),
TextFormField(
controller: _passwordController,
decoration: const InputDecoration(
labelText: 'Password',
),
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
),
Space.yf(2.0),
ElevatedButton(
onPressed: _login,
child: const Text('Login'),
),
],
),
),
),
);
}
}
41 changes: 41 additions & 0 deletions lib/features/register/infrastructure/dto/user_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:hive/hive.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'user_model.freezed.dart';
part 'user_model.g.dart';

@freezed
class User with _$User {
@HiveType(typeId: 1)
const factory User({
@HiveField(0) required String id,
@HiveField(1) required String name,
@HiveField(2) required String email,
@HiveField(3) required String password,
}) = _User;

factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}

class UserAdapter extends TypeAdapter<User> {
@override
final int typeId = 1;

@override
User read(BinaryReader reader) {
return User(
id: reader.readString(),
name: reader.readString(),
email: reader.readString(),
password: reader.readString(),
);
}

@override
void write(BinaryWriter writer, User obj) {
writer.writeString(obj.id);
writer.writeString(obj.name);
writer.writeString(obj.email);
writer.writeString(obj.password);
}
}
Loading

0 comments on commit 76e1684

Please sign in to comment.