359 lines
11 KiB
Dart
359 lines
11 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:bahasajepang/service/API_config.dart';
|
|
import 'package:bahasajepang/theme.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class SignUpPage extends StatefulWidget {
|
|
const SignUpPage({super.key});
|
|
|
|
@override
|
|
State<SignUpPage> createState() => _SignUpPage();
|
|
}
|
|
|
|
class _SignUpPage extends State<SignUpPage> {
|
|
final TextEditingController _fullnameController = TextEditingController();
|
|
final TextEditingController _usernameController = TextEditingController();
|
|
final TextEditingController _emailController = TextEditingController();
|
|
final TextEditingController _passwordController = TextEditingController();
|
|
bool _obscureText = true;
|
|
@override
|
|
Future<void> functionRegister() async {
|
|
String fullname = _fullnameController.text;
|
|
String username = _usernameController.text;
|
|
String email = _emailController.text;
|
|
String password = _passwordController.text;
|
|
|
|
try {
|
|
const endpoint = "/register";
|
|
var response = await http.post(
|
|
Uri.parse(ApiConfig.baseUrl + endpoint),
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode({
|
|
'fullname': fullname,
|
|
'username': username,
|
|
'email': email,
|
|
'password': password
|
|
}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
var jsonResponse = jsonDecode(response.body);
|
|
if (jsonResponse['status'] == 'success') {
|
|
print('${jsonResponse['message']}');
|
|
Navigator.pushNamed(context, '/sign-in');
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print('Error: $e');
|
|
}
|
|
}
|
|
|
|
Widget build(BuildContext context) {
|
|
Widget header() {
|
|
return Container(
|
|
margin: EdgeInsets.only(top: 30),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Sign Up',
|
|
style:
|
|
primaryTextStyle.copyWith(fontSize: 24, fontWeight: semiBold),
|
|
),
|
|
SizedBox(height: 2),
|
|
Text(
|
|
'Register and Learn',
|
|
style: secondaryTextStyle.copyWith(
|
|
fontSize: 14, fontWeight: regular),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget nameInput() {
|
|
return Container(
|
|
margin: EdgeInsets.only(top: 50),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Full Name',
|
|
style:
|
|
primaryTextStyle.copyWith(fontSize: 16, fontWeight: medium),
|
|
),
|
|
SizedBox(height: 12),
|
|
Container(
|
|
height: 45,
|
|
padding: EdgeInsets.symmetric(horizontal: 16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white, borderRadius: BorderRadius.circular(12)),
|
|
child: Center(
|
|
child: Row(
|
|
children: [
|
|
Image.asset('assets/profile.png', width: 17),
|
|
SizedBox(width: 16),
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller: _fullnameController,
|
|
style: primaryTextStyle.copyWith(
|
|
fontSize: 14,
|
|
fontWeight: medium,
|
|
),
|
|
decoration: InputDecoration.collapsed(
|
|
hintText: 'Your Full Name',
|
|
hintStyle: primaryTextStyle.copyWith(
|
|
fontSize: 14,
|
|
fontWeight: medium,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget usernameInput() {
|
|
return Container(
|
|
margin: EdgeInsets.only(top: 20),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Username',
|
|
style:
|
|
primaryTextStyle.copyWith(fontSize: 16, fontWeight: medium),
|
|
),
|
|
SizedBox(height: 12),
|
|
Container(
|
|
height: 45,
|
|
padding: EdgeInsets.symmetric(horizontal: 16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white, borderRadius: BorderRadius.circular(12)),
|
|
child: Center(
|
|
child: Row(
|
|
children: [
|
|
Image.asset('assets/user.png', width: 17),
|
|
SizedBox(width: 16),
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller: _usernameController,
|
|
style: primaryTextStyle.copyWith(
|
|
fontSize: 14,
|
|
fontWeight: medium,
|
|
),
|
|
decoration: InputDecoration.collapsed(
|
|
hintText: 'Your Username',
|
|
hintStyle: primaryTextStyle.copyWith(
|
|
fontSize: 14,
|
|
fontWeight: medium,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget emailInput() {
|
|
return Container(
|
|
margin: EdgeInsets.only(top: 20),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Email Address',
|
|
style:
|
|
primaryTextStyle.copyWith(fontSize: 16, fontWeight: medium),
|
|
),
|
|
SizedBox(height: 12),
|
|
Container(
|
|
height: 45,
|
|
padding: EdgeInsets.symmetric(horizontal: 16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white, borderRadius: BorderRadius.circular(12)),
|
|
child: Center(
|
|
child: Row(
|
|
children: [
|
|
Image.asset('assets/email.png', width: 17),
|
|
SizedBox(width: 16),
|
|
Expanded(
|
|
child: TextFormField(
|
|
keyboardType: TextInputType.emailAddress,
|
|
controller: _emailController,
|
|
style: primaryTextStyle.copyWith(
|
|
fontSize: 14,
|
|
fontWeight: medium,
|
|
),
|
|
decoration: InputDecoration.collapsed(
|
|
hintText: 'Your Email Address',
|
|
hintStyle: primaryTextStyle.copyWith(
|
|
fontSize: 14,
|
|
fontWeight: medium,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget passwordInput() {
|
|
return Container(
|
|
margin: EdgeInsets.only(top: 20),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Password',
|
|
style:
|
|
primaryTextStyle.copyWith(fontSize: 16, fontWeight: medium),
|
|
),
|
|
SizedBox(height: 12),
|
|
Container(
|
|
height: 45,
|
|
padding: EdgeInsets.symmetric(horizontal: 16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white, borderRadius: BorderRadius.circular(12)),
|
|
child: Center(
|
|
child: Row(
|
|
children: [
|
|
Image.asset('assets/padlock.png', width: 17),
|
|
SizedBox(width: 16),
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller: _passwordController,
|
|
style: primaryTextStyle.copyWith(
|
|
fontSize: 14,
|
|
fontWeight: medium,
|
|
),
|
|
obscureText: _obscureText,
|
|
decoration: InputDecoration(
|
|
border: InputBorder.none,
|
|
hintText: 'Your Password',
|
|
hintStyle: primaryTextStyle.copyWith(
|
|
fontSize: 14,
|
|
fontWeight: medium,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
GestureDetector(
|
|
onTap: () {
|
|
setState(() {
|
|
_obscureText = !_obscureText;
|
|
});
|
|
},
|
|
child: Icon(
|
|
_obscureText ? Icons.visibility_off : Icons.visibility,
|
|
color: Colors.grey,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget signUpButton() {
|
|
return Container(
|
|
height: 50,
|
|
width: double.infinity,
|
|
margin: EdgeInsets.only(top: 30),
|
|
child: TextButton(
|
|
onPressed: () {
|
|
functionRegister();
|
|
// Navigator.pushNamed(context, '/home');
|
|
},
|
|
style: TextButton.styleFrom(
|
|
backgroundColor: primaryColor,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12))),
|
|
child: Text(
|
|
'Sign Up',
|
|
style: primaryTextStyle.copyWith(
|
|
fontSize: 16,
|
|
fontWeight: medium,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget footer() {
|
|
return Container(
|
|
margin: EdgeInsets.only(bottom: 30),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
'Already have an account?',
|
|
style: primaryTextStyle.copyWith(
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
SizedBox(width: 5),
|
|
GestureDetector(
|
|
onTap: () {
|
|
Navigator.pop(context);
|
|
},
|
|
child: Text(
|
|
'Sign In',
|
|
style: purpleTextStyle.copyWith(
|
|
fontSize: 12,
|
|
fontWeight: medium,
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
return Scaffold(
|
|
backgroundColor: bgColor1,
|
|
body: SafeArea(
|
|
child: SingleChildScrollView(
|
|
physics: BouncingScrollPhysics(),
|
|
child: Container(
|
|
margin: EdgeInsets.symmetric(horizontal: defaultMargin),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
header(),
|
|
nameInput(),
|
|
usernameInput(),
|
|
emailInput(),
|
|
passwordInput(),
|
|
signUpButton(),
|
|
SizedBox(height: 30), // Ganti Spacer() agar tetap ada jarak
|
|
footer(),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|