Skip to content

Commit

Permalink
Merge pull request #83 from mahdiramezanii/develop
Browse files Browse the repository at this point in the history
fix bug in login and register screan with BlocCunsumer
  • Loading branch information
mahdiramezanii authored May 11, 2024
2 parents 5f2ab34 + 7e32748 commit 06ca2e5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 59 deletions.
14 changes: 3 additions & 11 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,26 @@ import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hive_flutter/hive_flutter.dart';

GlobalKey<NavigatorState> globalKey=GlobalKey<NavigatorState>();
GlobalKey<NavigatorState> globalKey = GlobalKey<NavigatorState>();

void main() async {

WidgetsFlutterBinding();
await Hive.initFlutter();
Hive.registerAdapter(BucketAdapter());
await Hive.openBox<Bucket>("BucketBox");
await initLocator();
runApp(MyApp());

}

class MyApp extends StatelessWidget {

MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey:globalKey,
navigatorKey: globalKey,
debugShowCheckedModeBanner: false,
home: (AuthManager.isLoggin())
? BottomNavigatonScrean()
: BlocProvider(
create: (context) => AuthBloc(),
child: LoginScrean(),
),
home: (AuthManager.isLoggin()) ? BottomNavigatonScrean() : LoginScrean(),
);
}
}
62 changes: 39 additions & 23 deletions lib/screan/login_screan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ class LoginScrean extends StatelessWidget {
final TextEditingController _usernameTextController = TextEditingController();
final TextEditingController _passwordTextController = TextEditingController();

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) {
return AuthBloc();
},
child: ViewLoginWidget(
usernameTextController: _usernameTextController,
passwordTextController: _passwordTextController,
),
);
}
}

class ViewLoginWidget extends StatelessWidget {
const ViewLoginWidget({
super.key,
required TextEditingController usernameTextController,
required TextEditingController passwordTextController,
}) : _usernameTextController = usernameTextController,
_passwordTextController = passwordTextController;

final TextEditingController _usernameTextController;
final TextEditingController _passwordTextController;

@override
Widget build(BuildContext context) {
return Directionality(
Expand Down Expand Up @@ -97,11 +122,20 @@ class LoginScrean extends StatelessWidget {
const SizedBox(
height: 30,
),
BlocBuilder<AuthBloc, AuthState>(builder: (context, state) {
BlocConsumer<AuthBloc, AuthState>(listener: (context, state) {
if (state is ResponseAuthState) {
state.response.fold((l) {}, (r) {
return Navigator.of(context)
.push(MaterialPageRoute(builder: (context) {
return BottomNavigatonScrean();
}));
});
}
}, builder: (context, state) {
if (state is InitAuthState) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size(200, 50),
minimumSize: const Size(200, 50),
backgroundColor: MyColors.green,
),
onPressed: () {
Expand Down Expand Up @@ -153,27 +187,9 @@ class LoginScrean extends StatelessWidget {
GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return BlocProvider(
create: (context) {
var bloc = AuthBloc();

bloc.stream.forEach((state) {
if (state is ResponseAuthState) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) {
return BottomNavigatonScrean();
}));
}
});

return bloc;
},
child: RegisterScrean(),
);
},
),
MaterialPageRoute(builder: (context) {
return RegisterScrean();
}),
);
},
child: const Text(
Expand Down
28 changes: 5 additions & 23 deletions lib/screan/profile_screan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ class ProfileScrean extends StatelessWidget {
),
),
child: const Image(
image: AssetImage("assets/images/discount.png")),
image: AssetImage(
"assets/images/discount.png",
),
),
),
const SizedBox(
height: 5,
Expand Down Expand Up @@ -291,28 +294,7 @@ class ProfileScrean extends StatelessWidget {
AuthManager.logout();
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BlocProvider(
create: (context) {
var bloc = AuthBloc();

bloc.stream.forEach((state) {
if (state is ResponseAuthState) {
state.response.fold((l) => null, (r) {
return globalKey.currentState?.pushReplacement(
MaterialPageRoute(
builder: (context) {
return BottomNavigatonScrean();
},
),
);
});
}
});

return bloc;
},
child: LoginScrean(),
);
return LoginScrean();
}));
},
style: ElevatedButton.styleFrom(
Expand Down
41 changes: 39 additions & 2 deletions lib/screan/register_screan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:apple_shop/bloc/authentication/auth_bloc.dart';
import 'package:apple_shop/bloc/authentication/auth_event.dart';
import 'package:apple_shop/bloc/authentication/auth_state.dart';
import 'package:apple_shop/constants/colors.dart';
import 'package:apple_shop/screan/bootom_navigation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
Expand All @@ -13,7 +14,44 @@ class RegisterScrean extends StatelessWidget {
TextEditingController _usernameController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
TextEditingController _passwordConfirmController = TextEditingController();
return BlocBuilder<AuthBloc, AuthState>(
return BlocProvider(
create: (context) => AuthBloc(),
child: ViewRegisterWidget(
usernameController: _usernameController,
passwordController: _passwordController,
passwordConfirmController: _passwordConfirmController,
),
);
}
}

class ViewRegisterWidget extends StatelessWidget {
const ViewRegisterWidget({
super.key,
required TextEditingController usernameController,
required TextEditingController passwordController,
required TextEditingController passwordConfirmController,
}) : _usernameController = usernameController,
_passwordController = passwordController,
_passwordConfirmController = passwordConfirmController;

final TextEditingController _usernameController;
final TextEditingController _passwordController;
final TextEditingController _passwordConfirmController;

@override
Widget build(BuildContext context) {
return BlocConsumer<AuthBloc, AuthState>(
listener: (context, state) {
if (state is ResponseAuthState) {
state.response.fold((l) => null, (r) {
return Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BottomNavigatonScrean();
}));
});
}
},
builder: (context, state) {
return SafeArea(
child: Scaffold(
Expand Down Expand Up @@ -157,7 +195,6 @@ class RegisterScrean extends StatelessWidget {
confirmPassword: _passwordConfirmController.text,
),
);

},
child: (state is LoadingAuthState)
? const CircularProgressIndicator()
Expand Down

0 comments on commit 06ca2e5

Please sign in to comment.