1
0

login_page.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // ignore_for_file: use_build_context_synchronously
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:go_router/go_router.dart';
  5. import 'package:tp5/auth/auth.dart';
  6. import 'package:tp5/core/core.dart';
  7. import 'package:tp5/core/my_theme.dart';
  8. import 'package:tp5/onboarding/onboarding.dart';
  9. class LoginPage extends ConsumerStatefulWidget {
  10. const LoginPage({super.key});
  11. @override
  12. ConsumerState<LoginPage> createState() => _LoginPageState();
  13. }
  14. class _LoginPageState extends ConsumerState<LoginPage> {
  15. final _formKey = GlobalKey<FormState>();
  16. AutovalidateMode? _autovalidateMode;
  17. bool _isSubmitting = false;
  18. final _emailCtrl = TextEditingController();
  19. final _passwordCtrl = TextEditingController();
  20. final _usernameCtrl = TextEditingController();
  21. Future<void> _login() async {
  22. try {
  23. setState(() {
  24. _isSubmitting = true;
  25. });
  26. await ref.read(authRepositoryProvider).logIn(
  27. email: _emailCtrl.text,
  28. password: _passwordCtrl.text,
  29. );
  30. if (mounted) {
  31. context.pop();
  32. }
  33. } catch (e) {
  34. setState(() {
  35. _isSubmitting = false;
  36. });
  37. context.showAlert(e.toString());
  38. }
  39. }
  40. Future<void> _createAccount() async {
  41. try {
  42. setState(() {
  43. _isSubmitting = true;
  44. });
  45. await ref.read(onboardingRepositoryProvider).signUp(
  46. email: _emailCtrl.text,
  47. password: _passwordCtrl.text,
  48. username: _usernameCtrl.text,
  49. );
  50. if (mounted) {
  51. context.push(
  52. '/verification',
  53. extra: VerificationPageParams(
  54. email: _emailCtrl.text,
  55. password: _passwordCtrl.text,
  56. username: _usernameCtrl.text,
  57. ),
  58. );
  59. }
  60. } catch (e) {
  61. context.showAlert(e.toString());
  62. }
  63. setState(() {
  64. _isSubmitting = false;
  65. });
  66. }
  67. @override
  68. Widget build(BuildContext context) {
  69. final isDarkMode = ref.watch(appThemeProvider);
  70. return Scaffold(
  71. appBar: AppBar(
  72. title: Row(
  73. children: [
  74. const Text("GeeksforGeeks"),
  75. Switch(
  76. activeColor: Colors.orange,
  77. onChanged: (value) {
  78. ref.read(appThemeProvider.notifier).state = value;
  79. },
  80. value: isDarkMode)
  81. ],
  82. ),
  83. ),
  84. body: SingleChildScrollView(
  85. child: Form(
  86. key: _formKey,
  87. autovalidateMode: _autovalidateMode,
  88. child: Column(
  89. children: <Widget>[
  90. Padding(
  91. padding: const EdgeInsets.all(20),
  92. child: Center(
  93. child: SizedBox(
  94. width: 300,
  95. child: Image.asset('assets/logo.png'),
  96. ),
  97. ),
  98. ),
  99. Padding(
  100. //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
  101. padding: const EdgeInsets.symmetric(horizontal: 15),
  102. child: TextFormField(
  103. controller: _emailCtrl,
  104. readOnly: _isSubmitting,
  105. validator: (value) {
  106. if (value == null || value.isEmpty) {
  107. return 'Please enter your email';
  108. }
  109. return null;
  110. },
  111. decoration: const InputDecoration(
  112. border: OutlineInputBorder(),
  113. labelText: 'Phone number, email or username',
  114. hintText: 'Enter valid email id as abc@gmail.com'),
  115. ),
  116. ),
  117. Padding(
  118. padding: const EdgeInsets.only(
  119. left: 15.0, right: 15.0, top: 15, bottom: 0),
  120. //padding: EdgeInsets.symmetric(horizontal: 15),
  121. child: TextFormField(
  122. controller: _passwordCtrl,
  123. readOnly: _isSubmitting,
  124. obscureText: true,
  125. validator: (value) {
  126. if (value == null || value.isEmpty) {
  127. return 'Please enter your password';
  128. }
  129. return null;
  130. },
  131. decoration: const InputDecoration(
  132. border: OutlineInputBorder(),
  133. labelText: 'Password',
  134. hintText: 'Enter secure password'),
  135. ),
  136. ),
  137. SizedBox(
  138. //height: 65,
  139. width: 360,
  140. child: Padding(
  141. padding: const EdgeInsets.only(top: 20.0),
  142. child: ElevatedButton(
  143. onPressed: _isSubmitting
  144. ? null
  145. : () {
  146. if (_formKey.currentState!.validate()) {
  147. _login();
  148. } else {
  149. setState(() {
  150. _autovalidateMode = AutovalidateMode.always;
  151. });
  152. }
  153. },
  154. child: const Padding(
  155. padding: EdgeInsets.all(8.0),
  156. child: Text(
  157. 'Log in ',
  158. style: TextStyle(color: Colors.white, fontSize: 20),
  159. ),
  160. ),
  161. ),
  162. ),
  163. ),
  164. const SizedBox(
  165. height: 20,
  166. ),
  167. Center(
  168. child: Row(
  169. children: [
  170. const Padding(
  171. padding: EdgeInsets.only(left: 62),
  172. child: Text('Forgot your login details? '),
  173. ),
  174. Padding(
  175. padding: const EdgeInsets.only(left: 1.0),
  176. child: InkWell(
  177. onTap: () {
  178. // print('hello');
  179. },
  180. child: const Text(
  181. 'Get help logging in.',
  182. style: TextStyle(fontSize: 14, color: Colors.blue),
  183. )),
  184. ),
  185. ],
  186. ),
  187. ),
  188. const SizedBox(
  189. height: 50,
  190. ),
  191. Center(
  192. child: Row(
  193. children: [
  194. const Padding(
  195. padding: EdgeInsets.only(left: 62),
  196. child: Text('don\'t have an account yet? '),
  197. ),
  198. Padding(
  199. padding: const EdgeInsets.only(left: 1.0),
  200. child: InkWell(
  201. onTap: () {
  202. _createAccount();
  203. },
  204. child: const Text(
  205. 'Create an account.',
  206. style: TextStyle(
  207. fontSize: 14,
  208. color: Colors.blue,
  209. fontWeight: FontWeight.bold),
  210. )),
  211. ),
  212. ],
  213. ),
  214. ),
  215. ],
  216. ),
  217. ),
  218. ),
  219. );
  220. }
  221. }