| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- // ignore_for_file: use_build_context_synchronously
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:go_router/go_router.dart';
- import 'package:tp5/auth/auth.dart';
- import 'package:tp5/core/core.dart';
- import 'package:tp5/core/my_theme.dart';
- import 'package:tp5/onboarding/onboarding.dart';
- class LoginPage extends ConsumerStatefulWidget {
- const LoginPage({super.key});
- @override
- ConsumerState<LoginPage> createState() => _LoginPageState();
- }
- class _LoginPageState extends ConsumerState<LoginPage> {
- final _formKey = GlobalKey<FormState>();
- AutovalidateMode? _autovalidateMode;
- bool _isSubmitting = false;
- final _emailCtrl = TextEditingController();
- final _passwordCtrl = TextEditingController();
- final _usernameCtrl = TextEditingController();
- Future<void> _login() async {
- try {
- setState(() {
- _isSubmitting = true;
- });
- await ref.read(authRepositoryProvider).logIn(
- email: _emailCtrl.text,
- password: _passwordCtrl.text,
- );
- if (mounted) {
- context.pop();
- }
- } catch (e) {
- setState(() {
- _isSubmitting = false;
- });
- context.showAlert(e.toString());
- }
- }
- Future<void> _createAccount() async {
- try {
- setState(() {
- _isSubmitting = true;
- });
- await ref.read(onboardingRepositoryProvider).signUp(
- email: _emailCtrl.text,
- password: _passwordCtrl.text,
- username: _usernameCtrl.text,
- );
- if (mounted) {
- context.push(
- '/verification',
- extra: VerificationPageParams(
- email: _emailCtrl.text,
- password: _passwordCtrl.text,
- username: _usernameCtrl.text,
- ),
- );
- }
- } catch (e) {
- context.showAlert(e.toString());
- }
- setState(() {
- _isSubmitting = false;
- });
- }
- @override
- Widget build(BuildContext context) {
- final isDarkMode = ref.watch(appThemeProvider);
- return Scaffold(
- appBar: AppBar(
- title: Row(
- children: [
- const Text("GeeksforGeeks"),
- Switch(
- activeColor: Colors.orange,
- onChanged: (value) {
- ref.read(appThemeProvider.notifier).state = value;
- },
- value: isDarkMode)
- ],
- ),
- ),
- body: SingleChildScrollView(
- child: Form(
- key: _formKey,
- autovalidateMode: _autovalidateMode,
- child: Column(
- children: <Widget>[
- Padding(
- padding: const EdgeInsets.all(20),
- child: Center(
- child: SizedBox(
- width: 300,
- child: Image.asset('assets/logo.png'),
- ),
- ),
- ),
- Padding(
- //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
- padding: const EdgeInsets.symmetric(horizontal: 15),
- child: TextFormField(
- controller: _emailCtrl,
- readOnly: _isSubmitting,
- validator: (value) {
- if (value == null || value.isEmpty) {
- return 'Please enter your email';
- }
- return null;
- },
- decoration: const InputDecoration(
- border: OutlineInputBorder(),
- labelText: 'Phone number, email or username',
- hintText: 'Enter valid email id as abc@gmail.com'),
- ),
- ),
- Padding(
- padding: const EdgeInsets.only(
- left: 15.0, right: 15.0, top: 15, bottom: 0),
- //padding: EdgeInsets.symmetric(horizontal: 15),
- child: TextFormField(
- controller: _passwordCtrl,
- readOnly: _isSubmitting,
- obscureText: true,
- validator: (value) {
- if (value == null || value.isEmpty) {
- return 'Please enter your password';
- }
- return null;
- },
- decoration: const InputDecoration(
- border: OutlineInputBorder(),
- labelText: 'Password',
- hintText: 'Enter secure password'),
- ),
- ),
- SizedBox(
- //height: 65,
- width: 360,
- child: Padding(
- padding: const EdgeInsets.only(top: 20.0),
- child: ElevatedButton(
- onPressed: _isSubmitting
- ? null
- : () {
- if (_formKey.currentState!.validate()) {
- _login();
- } else {
- setState(() {
- _autovalidateMode = AutovalidateMode.always;
- });
- }
- },
- child: const Padding(
- padding: EdgeInsets.all(8.0),
- child: Text(
- 'Log in ',
- style: TextStyle(color: Colors.white, fontSize: 20),
- ),
- ),
- ),
- ),
- ),
- const SizedBox(
- height: 20,
- ),
- Center(
- child: Row(
- children: [
- const Padding(
- padding: EdgeInsets.only(left: 62),
- child: Text('Forgot your login details? '),
- ),
- Padding(
- padding: const EdgeInsets.only(left: 1.0),
- child: InkWell(
- onTap: () {
- // print('hello');
- },
- child: const Text(
- 'Get help logging in.',
- style: TextStyle(fontSize: 14, color: Colors.blue),
- )),
- ),
- ],
- ),
- ),
- const SizedBox(
- height: 50,
- ),
- Center(
- child: Row(
- children: [
- const Padding(
- padding: EdgeInsets.only(left: 62),
- child: Text('don\'t have an account yet? '),
- ),
- Padding(
- padding: const EdgeInsets.only(left: 1.0),
- child: InkWell(
- onTap: () {
- _createAccount();
- },
- child: const Text(
- 'Create an account.',
- style: TextStyle(
- fontSize: 14,
- color: Colors.blue,
- fontWeight: FontWeight.bold),
- )),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- );
- }
- }
|