| 1 | import 'package:flutter/services.dart'; |
| 2 | import 'package:flutter_secure_storage/flutter_secure_storage.dart'; |
| 3 | import 'package:openapi/openapi.dart'; |
| 4 | import 'package:flutter/material.dart'; |
| 5 | import 'package:go_router/go_router.dart'; |
| 6 | import 'package:aero/constants/main.dart'; |
| 7 | import 'package:aero/notifiers/user.dart'; |
| 8 | import 'package:aero/screens/auth/service.dart'; |
| 9 | import 'package:aero/utils/error.dart'; |
| 10 | import 'package:aero/widgets/input.dart'; |
| 11 | import 'package:provider/provider.dart'; |
| 12 | |
| 13 | class LoginScreen extends StatefulWidget |
| 14 | const LoginScreen({super |
| 15 | |
| 16 | @override |
| 17 | State<LoginScreen> createState |
| 18 | } |
| 19 | |
| 20 | class _LoginScreenState extends State |
| 21 | final _formKey = GlobalKey< |
| 22 | final _nameController = TextEditingController() |
| 23 | final _emailController = TextEditingController() |
| 24 | final _passwordController = TextEditingController() |
| 25 | bool _loading = false; |
| 26 | bool _isPasswordVisible = false; |
| 27 | |
| 28 | Future<void> _login |
| 29 | if (!_formKey.currentState!.validate |
| 30 | setState(() { |
| 31 | _loading = true; |
| 32 | }); |
| 33 | |
| 34 | final data = await authService. |
| 35 | LoginDTO( |
| 36 | (b) { |
| 37 | b |
| 38 | ..email = _emailController.text.trim |
| 39 | ..password = _passwordController.text.trim |
| 40 | ..build(); |
| 41 | }, |
| 42 | ), |
| 43 | ); |
| 44 | setState(() { |
| 45 | _loading = false; |
| 46 | }); |
| 47 | if (data.hasError) { |
| 48 | showErrorToast(data.error); |
| 49 | return; |
| 50 | } |
| 51 | final body = data.value! |
| 52 | TextInput.finishAutofillContext(); |
| 53 | await const FlutterSecureStorage() |
| 54 | key: AUTH_TOKEN_KEY, |
| 55 | value: body!.token, |
| 56 | ); |
| 57 | if (mounted) { |
| 58 | final ref = Provider. |
| 59 | ref.login(body.user.id |
| 60 | } |
| 61 | showSuccessToast( |
| 62 | description: "Welcome back ${body.user |
| 63 | ); |
| 64 | if (mounted) { |
| 65 | GoRouter.of(context).go |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @override |
| 70 | Widget build(BuildContext |
| 71 | return Scaffold( |
| 72 | backgroundColor: Colors.white, |
| 73 | body: SafeArea( |
| 74 | child: SingleChildScrollView( |
| 75 | padding: const EdgeInsets. |
| 76 | child: Form( |
| 77 | key: _formKey, |
| 78 | child: AutofillGroup( |
| 79 | child: Column( |
| 80 | mainAxisAlignment: MainAxisAlignment.center, |
| 81 | crossAxisAlignment: CrossAxisAlignment.stretch, |
| 82 | children: [ |
| 83 | const SizedBox(height: |
| 84 | // App Logo |
| 85 | Center( |
| 86 | child: Image.asset( |
| 87 | 'assets/logos/0-1.png', |
| 88 | width: 100, |
| 89 | height: 100, |
| 90 | fit: BoxFit.contain, |
| 91 | ), |
| 92 | ), |
| 93 | const SizedBox(height: |
| 94 | // Welcome Text |
| 95 | const Text( |
| 96 | 'Welcome Back', |
| 97 | style: TextStyle( |
| 98 | fontSize: 32, |
| 99 | fontWeight: FontWeight.bold, |
| 100 | fontFamily: "CalSans", |
| 101 | color: Colors.black, |
| 102 | letterSpacing: -1, |
| 103 | ), |
| 104 | textAlign: TextAlign.center, |
| 105 | ), |
| 106 | const SizedBox(height: |
| 107 | Text( |
| 108 | 'Sign in to continue tracking your flights', |
| 109 | style: TextStyle( |
| 110 | fontSize: 15, |
| 111 | color: Colors.grey.shade700 |
| 112 | fontFamily: "DM Sans", |
| 113 | height: 1.4, |
| 114 | ), |
| 115 | textAlign: TextAlign.center, |
| 116 | ), |
| 117 | const SizedBox(height: |
| 118 | // Email Field |
| 119 | InputField( |
| 120 | hintText: "Email", |
| 121 | keyboardType: TextInputType.emailAddress, |
| 122 | autofillHints: [ |
| 123 | AutofillHints.email, |
| 124 | ], |
| 125 | fillColor: Colors.white, |
| 126 | validator: (value) => value?.isEmpty ?? |
| 127 | ? 'Please enter your email' |
| 128 | : null, |
| 129 | controller: _emailController, |
| 130 | prefixIcon: Icon( |
| 131 | Icons.email_outlined, |
| 132 | color: Colors.black, |
| 133 | size: 20, |
| 134 | ), |
| 135 | enabledBorder: OutlineInputBorder( |
| 136 | borderRadius: BorderRadius.circular( |
| 137 | borderSide: BorderSide( |
| 138 | color: Colors.grey.shade300 |
| 139 | width: 1, |
| 140 | ), |
| 141 | ), |
| 142 | border: OutlineInputBorder( |
| 143 | borderRadius: BorderRadius.circular( |
| 144 | borderSide: const BorderSide( |
| 145 | color: Colors.black, |
| 146 | width: 1.5, |
| 147 | ), |
| 148 | ), |
| 149 | ), |
| 150 | const SizedBox(height: |
| 151 | // Password Field |
| 152 | InputField( |
| 153 | hintText: "Password", |
| 154 | keyboardType: TextInputType.visiblePassword, |
| 155 | autofillHints: [ |
| 156 | AutofillHints.password, |
| 157 | ], |
| 158 | fillColor: Colors.white, |
| 159 | validator: (value) => (value?.length ?? |
| 160 | ? 'Password must be at least 8 characters' |
| 161 | : null, |
| 162 | obscureText: !_isPasswordVisible, |
| 163 | controller: _passwordController, |
| 164 | prefixIcon: Icon( |
| 165 | Icons.lock_outline, |
| 166 | color: Colors.black, |
| 167 | size: 20, |
| 168 | ), |
| 169 | suffixIcon: IconButton( |
| 170 | icon: Icon( |
| 171 | _isPasswordVisible |
| 172 | ? Icons.visibility_off |
| 173 | : Icons.visibility, |
| 174 | color: Colors.grey.shade600 |
| 175 | size: 20, |
| 176 | ), |
| 177 | onPressed: () { |
| 178 | setState(() { |
| 179 | _isPasswordVisible = !_isPasswordVisible; |
| 180 | }); |
| 181 | }, |
| 182 | ), |
| 183 | enabledBorder: OutlineInputBorder( |
| 184 | borderRadius: BorderRadius.circular( |
| 185 | borderSide: BorderSide( |
| 186 | color: Colors.grey.shade300 |
| 187 | width: 1, |
| 188 | ), |
| 189 | ), |
| 190 | border: OutlineInputBorder( |
| 191 | borderRadius: BorderRadius.circular( |
| 192 | borderSide: const BorderSide( |
| 193 | color: Colors.black, |
| 194 | width: 1.5, |
| 195 | ), |
| 196 | ), |
| 197 | ), |
| 198 | const SizedBox(height: |
| 199 | // Login Button |
| 200 | Consumer( |
| 201 | builder: (context, ref, child) { |
| 202 | return SizedBox( |
| 203 | height: 52, |
| 204 | child: ElevatedButton( |
| 205 | onPressed: _loading ? null : _login, |
| 206 | style: ElevatedButton.styleFrom( |
| 207 | backgroundColor: Colors.black, |
| 208 | foregroundColor: Colors.white, |
| 209 | shape: RoundedRectangleBorder( |
| 210 | borderRadius: BorderRadius.circular( |
| 211 | ), |
| 212 | elevation: 0, |
| 213 | ), |
| 214 | child: _loading |
| 215 | ? const SizedBox( |
| 216 | height: 20, |
| 217 | width: 20, |
| 218 | child: CircularProgressIndicator( |
| 219 | strokeWidth: 2, |
| 220 | valueColor: AlwaysStoppedAnimation<Color>( |
| 221 | Colors.white, |
| 222 | ), |
| 223 | ), |
| 224 | ) |
| 225 | : const Text( |
| 226 | "Login", |
| 227 | style: TextStyle( |
| 228 | fontSize: 16, |
| 229 | fontWeight: FontWeight.w600, |
| 230 | fontFamily: "CalSans", |
| 231 | letterSpacing: 0.5, |
| 232 | ), |
| 233 | ), |
| 234 | ), |
| 235 | ); |
| 236 | }, |
| 237 | ), |
| 238 | const SizedBox(height: |
| 239 | // Register Link |
| 240 | Row( |
| 241 | mainAxisAlignment: MainAxisAlignment.center, |
| 242 | children: [ |
| 243 | Text( |
| 244 | "Don't have an account? ", |
| 245 | style: TextStyle( |
| 246 | color: Colors.grey.shade600 |
| 247 | fontSize: 14, |
| 248 | fontFamily: "DM Sans", |
| 249 | ), |
| 250 | ), |
| 251 | TextButton( |
| 252 | onPressed: () { |
| 253 | context.go("/auth/register"); |
| 254 | }, |
| 255 | style: TextButton.styleFrom( |
| 256 | padding: const EdgeInsets. |
| 257 | minimumSize: Size.zero, |
| 258 | tapTargetSize: MaterialTapTargetSize.shrinkWrap, |
| 259 | ), |
| 260 | child: const Text( |
| 261 | "Register", |
| 262 | style: TextStyle( |
| 263 | fontWeight: FontWeight.w600, |
| 264 | color: Colors.black, |
| 265 | fontSize: 14, |
| 266 | fontFamily: "CalSans", |
| 267 | decoration: TextDecoration.underline, |
| 268 | decorationThickness: 1.5, |
| 269 | ), |
| 270 | ), |
| 271 | ), |
| 272 | ], |
| 273 | ), |
| 274 | ], |
| 275 | ), |
| 276 | ), |
| 277 | ), |
| 278 | ), |
| 279 | ), |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | @override |
| 284 | void dispose() { |
| 285 | _nameController.dispose(); |
| 286 | _emailController.dispose(); |
| 287 | _passwordController.dispose(); |
| 288 | super.dispose(); |
| 289 | } |
| 290 | } |