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