Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug in login and register screan with BlocCunsumer #83

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading