Release version 1.0.0
This commit is contained in:
186
lib/screens/signup.dart
Normal file
186
lib/screens/signup.dart
Normal file
@@ -0,0 +1,186 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../config.dart';
|
||||
import '../services/session_manager.dart';
|
||||
|
||||
class SignupScreen extends StatefulWidget {
|
||||
final VoidCallback onSwitchToLogin;
|
||||
final VoidCallback onSignupSuccess;
|
||||
|
||||
const SignupScreen({
|
||||
required this.onSwitchToLogin,
|
||||
required this.onSignupSuccess,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
State<SignupScreen> createState() => _SignupScreenState();
|
||||
}
|
||||
|
||||
class _SignupScreenState extends State<SignupScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _usernameController = TextEditingController();
|
||||
final _emailController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
|
||||
Future<void> _signup() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
final username = _usernameController.text;
|
||||
final email = _emailController.text;
|
||||
final password = _passwordController.text;
|
||||
|
||||
try {
|
||||
final signupResponse = await http.post(
|
||||
Uri.parse('$apiBaseUrl/api/v1/auth/signup'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
'username': username,
|
||||
'email': email,
|
||||
'password': password,
|
||||
}),
|
||||
);
|
||||
|
||||
if (signupResponse.statusCode == 200 || signupResponse.statusCode == 201) {
|
||||
final signinResponse = await http.post(
|
||||
Uri.parse('$apiBaseUrl/api/v1/auth/signin'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'email': email, 'password': password}),
|
||||
);
|
||||
|
||||
if (signinResponse.statusCode == 200) {
|
||||
final data = jsonDecode(signinResponse.body);
|
||||
final token = data['token'];
|
||||
final name = data['user']?['username'] ?? data['username'] ?? username;
|
||||
|
||||
await Provider.of<SessionManager>(context, listen: false).login(
|
||||
token: token,
|
||||
email: email,
|
||||
name: name,
|
||||
);
|
||||
|
||||
widget.onSignupSuccess();
|
||||
} else {
|
||||
final data = jsonDecode(signinResponse.body);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(data['message'] ?? 'Sign in failed')),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
final data = jsonDecode(signupResponse.body);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(data['message'] ?? 'Signup failed')),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Signup error: $e')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
//appBar: AppBar(title: Text('Sign Up')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
const SizedBox(height: 40),
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
child: Image.asset(
|
||||
'assets/icon/app_icon.png',
|
||||
width: 100,
|
||||
height: 100,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Create your Fuel Stats account',
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
TextFormField(
|
||||
controller: _usernameController,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Username',
|
||||
prefixIcon: Icon(Icons.person),
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty)
|
||||
return 'Please enter a username';
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _emailController,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email',
|
||||
prefixIcon: Icon(Icons.email),
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty)
|
||||
return 'Please enter an email';
|
||||
if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value))
|
||||
return 'Enter a valid email';
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _passwordController,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password',
|
||||
prefixIcon: Icon(Icons.lock),
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
obscureText: true,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty)
|
||||
return 'Please enter a password';
|
||||
if (value.length < 6)
|
||||
return 'Password must be at least 6 characters';
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _signup,
|
||||
icon: Icon(Icons.person_add),
|
||||
label: Text('Sign Up'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
padding: EdgeInsets.symmetric(horizontal: 32, vertical: 12),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextButton(
|
||||
onPressed: widget.onSwitchToLogin,
|
||||
child: Text("Already have an account? Log in"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user