Created
November 22, 2024 12:39
-
-
Save nurrachmat-nr/d597f67ff13bab4196d7dd79c3a1f97c to your computer and use it in GitHub Desktop.
SignInScreen Completed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/gestures.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:wisata_candi/screens/sign_up_screen.dart'; | |
| class SignInScreen extends StatefulWidget { | |
| const SignInScreen({super.key}); | |
| @override | |
| State<SignInScreen> createState() => _SignInScreenState(); | |
| } | |
| class _SignInScreenState extends State<SignInScreen> { | |
| final TextEditingController _usernameController = TextEditingController(); | |
| final TextEditingController _passwordController = TextEditingController(); | |
| String _errorText = ''; | |
| bool _obscurePassword = true; | |
| void _signIn() { | |
| String username = _usernameController.text.trim(); | |
| String password = _passwordController.text.trim(); | |
| if (password.length < 8 || | |
| !password.contains(RegExp(r'[A-Z]')) || | |
| !password.contains(RegExp(r'[a-z]')) || | |
| !password.contains(RegExp(r'[0-9]')) || | |
| !password.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'))) { | |
| setState(() { | |
| _errorText = | |
| 'Minimal 8 karakter, kombinasi [A-Z], [a-z], [0-9], [!@#\\\$%^&*(),.?":{}|<>]'; | |
| }); | |
| return; | |
| } else { | |
| setState(() { | |
| _errorText = ''; | |
| }); | |
| } | |
| print('*** Sign in berhasil!'); | |
| print('Nama Pengguna: $username'); | |
| print('Password: $password'); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Sign In'), | |
| ), | |
| body: Center( | |
| child: SingleChildScrollView( | |
| child: Padding( | |
| padding: const EdgeInsets.all(16), | |
| child: Form( | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| crossAxisAlignment: CrossAxisAlignment.center, | |
| children: [ | |
| TextFormField( | |
| controller: _usernameController, | |
| decoration: const InputDecoration( | |
| labelText: "Nama Pengguna", | |
| border: OutlineInputBorder(), | |
| ), | |
| ), | |
| const SizedBox(height: 20), | |
| TextFormField( | |
| controller: _passwordController, | |
| decoration: InputDecoration( | |
| labelText: "Kata Sandi", | |
| errorText: _errorText.isNotEmpty ? _errorText : null, | |
| border: const OutlineInputBorder(), | |
| suffixIcon: IconButton( | |
| onPressed: () { | |
| setState(() { | |
| _obscurePassword = !_obscurePassword; | |
| }); | |
| }, | |
| icon: Icon( | |
| _obscurePassword | |
| ? Icons.visibility_off | |
| : Icons.visibility, | |
| ), | |
| ), | |
| ), | |
| obscureText: _obscurePassword, | |
| ), | |
| const SizedBox(height: 20), | |
| ElevatedButton( | |
| onPressed: _signIn, | |
| child: const Text('Sign In'), | |
| ), | |
| const SizedBox(height: 10), | |
| RichText( | |
| text: TextSpan( | |
| text: 'Belum punya akun? ', | |
| style: const TextStyle( | |
| fontSize: 16, | |
| color: Colors.deepPurple, | |
| ), | |
| children: <TextSpan>[ | |
| TextSpan( | |
| text: 'Daftar di sini.', | |
| style: const TextStyle( | |
| color: Colors.blue, | |
| decoration: TextDecoration.underline, | |
| fontSize: 16, | |
| ), | |
| recognizer: TapGestureRecognizer() | |
| ..onTap = () { | |
| Navigator.push( | |
| context, | |
| MaterialPageRoute( | |
| builder: (context) => const SignUpScreen(), | |
| ), | |
| ); | |
| }, | |
| ), | |
| ], | |
| ), | |
| ), | |
| ], | |
| ), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment