auth_screen.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3. import 'package:youtube/shared/constats.dart';
  4. import '../../shared/network/cache_helper.dart';
  5. import '../../shared/shared_widgets.dart';
  6. import '../../shared/theme.dart';
  7. import '../cubit/auth/auth_cubit.dart';
  8. import '../cubit/auth/auth_state.dart';
  9. enum AuthOptions {
  10. login('Login', LoginView()),
  11. register('Register', RegisterView());
  12. final String name;
  13. final Widget view;
  14. const AuthOptions(this.name, this.view);
  15. }
  16. class AuthScreen extends StatelessWidget {
  17. const AuthScreen({Key? key}) : super(key: key);
  18. @override
  19. Widget build(BuildContext context) {
  20. return Scaffold(
  21. appBar: AppBar(),
  22. body: BlocListener<AuthCubit, AuthState>(
  23. listener: (BuildContext context, state) {
  24. if (state is SuccessLoginState) {
  25. CacheHelper.saveData(key: 'uId', value: state.uId).then((value) {
  26. Navigator.pushNamedAndRemoveUntil(
  27. context, homeScreen, (route) => false);
  28. });
  29. }
  30. if (state is SuccessRegisterState) {
  31. CacheHelper.saveData(key: 'uId', value: state.uId).then((value) {
  32. Navigator.pushNamedAndRemoveUntil(
  33. context, homeScreen, (route) => false);
  34. });
  35. }
  36. if (state is ErrorRegisterState) {
  37. showToast(
  38. msg: state.errorMessage,
  39. );
  40. }
  41. if (state is ErrorLoginState) {
  42. showToast(
  43. msg: state.errorMessage,
  44. );
  45. }
  46. if (state is ErrorGetUserDataState) {
  47. showToast(
  48. msg: state.errorMessage,
  49. );
  50. }
  51. if (state is SuccessGetUserDataState) {
  52. Navigator.pushNamedAndRemoveUntil(
  53. context, homeScreen, (route) => false);
  54. }
  55. },
  56. child: GestureDetector(
  57. onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
  58. child: Padding(
  59. padding: const EdgeInsets.all(8.0),
  60. child: DefaultTabController(
  61. length: AuthOptions.values.length,
  62. child: Column(
  63. mainAxisAlignment: MainAxisAlignment.center,
  64. children: [
  65. TabBar(
  66. isScrollable: true,
  67. unselectedLabelColor: Colors.grey[400],
  68. indicatorColor: defaultColor,
  69. labelColor: defaultColor,
  70. tabs: AuthOptions.values
  71. .map((e) => Tab(
  72. child: Text(
  73. e.name,
  74. style: const TextStyle(fontSize: 15),
  75. ),
  76. ))
  77. .toList(),
  78. ),
  79. Expanded(
  80. child: TabBarView(
  81. children: AuthOptions.values.map((e) => e.view).toList(),
  82. ),
  83. ),
  84. ],
  85. ),
  86. ),
  87. ),
  88. ),
  89. ),
  90. );
  91. }
  92. }
  93. class LoginView extends StatefulWidget {
  94. const LoginView({Key? key}) : super(key: key);
  95. @override
  96. State<LoginView> createState() => _LoginViewState();
  97. }
  98. class _LoginViewState extends State<LoginView> {
  99. final emailController = TextEditingController();
  100. final passwordController = TextEditingController();
  101. @override
  102. void dispose() {
  103. emailController.dispose();
  104. passwordController.dispose();
  105. super.dispose();
  106. }
  107. @override
  108. Widget build(BuildContext context) {
  109. return BlocBuilder<AuthCubit, AuthState>(
  110. builder: (BuildContext context, state) {
  111. var authCubit = AuthCubit.get(context);
  112. return Padding(
  113. padding: const EdgeInsets.all(8.0),
  114. child: Column(
  115. mainAxisAlignment: MainAxisAlignment.center,
  116. children: [
  117. TextField(
  118. controller: emailController,
  119. decoration: const InputDecoration(
  120. border: OutlineInputBorder(),
  121. labelText: 'Email',
  122. hintText: 'Enter your email address',
  123. ),
  124. ),
  125. const SizedBox(
  126. height: 10.0,
  127. ),
  128. TextField(
  129. controller: passwordController,
  130. decoration: const InputDecoration(
  131. border: OutlineInputBorder(),
  132. labelText: 'Password',
  133. hintText: 'Enter your password',
  134. ),
  135. ),
  136. const SizedBox(
  137. height: 20.0,
  138. ),
  139. state is! LoadingLoginState
  140. ? ElevatedButton.icon(
  141. onPressed: () {
  142. if (emailController.text.isNotEmpty &&
  143. passwordController.text.isNotEmpty) {
  144. authCubit.login(
  145. email: emailController.text,
  146. password: passwordController.text);
  147. } else {
  148. showToast(msg: 'Please fill req data');
  149. }
  150. },
  151. icon: const Icon(Icons.login),
  152. label: const Text('Login'))
  153. : const CircularProgressIndicator(),
  154. ],
  155. ),
  156. );
  157. },
  158. );
  159. }
  160. }
  161. class RegisterView extends StatefulWidget {
  162. const RegisterView({Key? key}) : super(key: key);
  163. @override
  164. State<RegisterView> createState() => _RegisterViewState();
  165. }
  166. class _RegisterViewState extends State<RegisterView> {
  167. final nameController = TextEditingController();
  168. final emailController = TextEditingController();
  169. final passwordController = TextEditingController();
  170. @override
  171. void dispose() {
  172. nameController.dispose();
  173. emailController.dispose();
  174. passwordController.dispose();
  175. super.dispose();
  176. }
  177. @override
  178. Widget build(BuildContext context) {
  179. return BlocBuilder<AuthCubit, AuthState>(
  180. builder: (BuildContext context, state) {
  181. var authCubit = AuthCubit.get(context);
  182. return Padding(
  183. padding: const EdgeInsets.all(8.0),
  184. child: Column(
  185. mainAxisAlignment: MainAxisAlignment.center,
  186. children: [
  187. TextField(
  188. controller: nameController,
  189. decoration: const InputDecoration(
  190. border: OutlineInputBorder(),
  191. labelText: 'Name',
  192. hintText: 'Enter your name',
  193. ),
  194. ),
  195. const SizedBox(
  196. height: 10.0,
  197. ),
  198. TextField(
  199. controller: emailController,
  200. decoration: const InputDecoration(
  201. border: OutlineInputBorder(),
  202. labelText: 'Email',
  203. hintText: 'Enter your email address',
  204. ),
  205. ),
  206. const SizedBox(
  207. height: 10.0,
  208. ),
  209. TextField(
  210. controller: passwordController,
  211. decoration: const InputDecoration(
  212. border: OutlineInputBorder(),
  213. labelText: 'Password',
  214. hintText: 'Enter your password',
  215. )),
  216. const SizedBox(
  217. height: 10.0,
  218. ),
  219. state is! LoadingRegisterState
  220. ? ElevatedButton.icon(
  221. onPressed: () {
  222. if (emailController.text.isNotEmpty &&
  223. passwordController.text.isNotEmpty &&
  224. nameController.text.isNotEmpty) {
  225. authCubit.register(
  226. email: emailController.text,
  227. password: passwordController.text,
  228. name: nameController.text);
  229. } else {
  230. showToast(msg: 'Please fill req data');
  231. }
  232. },
  233. icon: const Icon(Icons.login),
  234. label: const Text('Register'))
  235. : const CircularProgressIndicator(),
  236. ],
  237. ),
  238. );
  239. },
  240. );
  241. }
  242. }