Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- class Authentication extends StatefulWidget {
- @override
- _AuthenticationState createState() => _AuthenticationState();
- }
- //Complete 13: buat 2 nilai enum untuk FormType
- enum FormType { signIn, signUp }
- class _AuthenticationState extends State<Authentication> {
- //Complete 3: buat global key
- final formKey = GlobalKey<FormState>();
- String _email;
- String _password;
- //Complete 14: inisialisasi nilai formType
- FormType _formType = FormType.signIn;
- //TODO 24: ubah return value jadi bool
- bool validateAndSave() {
- //Complete 5: buat variabel form dan diberi state dari formKey
- final form = formKey.currentState;
- //Complete 9: simpan form
- form.save();
- //Complete 6: lakukan validasi
- if (form.validate()) {
- //Complete 10: print nilai _email dan _password
- print('Form is valid. Email: $_email, Password: $_password');
- return true;
- } else {
- print('Form is invalid. Email: $_email, Password: $_password');
- return false;
- }
- }
- //TODO 25: buat function validateAndSubmit
- void validateAndSubmit() {
- //TODO 26: cek apakah form valid
- if(validateAndSave()) {
- //TODO 27: gunakan try-catch untuk memeriksa error
- try{
- //TODO 28: kondisi signIn atau signUp
- if(_formType == FormType.signIn) {
- //TODO 29: buat proses signIn
- } else {
- //TODO 30: buat proses signUp
- }
- } catch (e) {
- print("Error: $e");
- }
- }
- }
- //Complete 12: buat function untuk berpindah dari Sign In ke Sign Up
- void moveToSignUp() {
- //Complete 15: set formType menjadi signUp dan reload
- setState(() {
- _formType = FormType.signUp;
- });
- }
- void moveToSignIn() {
- //Complete 16: set formType menjadi signIn dan reload
- setState(() {
- _formType = FormType.signIn;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Autentication'),
- ),
- body: Container(
- padding: EdgeInsets.all(16.0),
- child: Form(
- //Complete 4: pasang key
- key: formKey,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children:
- //Complete 21: panggil buildInputs dan buildButtons
- buildInputs() + buildButtons(),
- ),
- ),
- ),
- );
- }
- //Complete 17: refactor kedua TextFormField
- List<Widget> buildInputs() {
- //Complete 18: pindahkan kedua TextFormField
- return [
- TextFormField(
- keyboardType: TextInputType.emailAddress,
- decoration: InputDecoration(labelText: 'Email'),
- //Complete 1: validasi email
- validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
- //Complete 7: simpan nilai email
- onSaved: (value) => _email = value,
- ),
- TextFormField(
- decoration: InputDecoration(labelText: 'Password'),
- //Complete 2: validasi password
- validator: (value) => value.length < 6 ? 'Minimum 6 characters' : null,
- obscureText: true,
- //Complete 8: simpan nilai password
- onSaved: (value) => _password = value,
- ),
- ];
- }
- //Complete 19: refactor kedua Button
- List<Widget> buildButtons() {
- //TODO 21: buat kondisi untuk signUp dan signIn
- if (_formType == FormType.signIn) {
- //Complete 20: pindahkan kedua Button
- return [
- RaisedButton(
- //Complete 10: ganti teks dengan Sign In
- child: Text('Sign In'),
- //TODO 22: ubah function menjadi validateAndSubmit
- onPressed: validateAndSubmit,
- color: Colors.deepOrange,
- textColor: Colors.white,
- ),
- //Complete 11: buat FlatButton untuk sign up
- FlatButton(
- child: Text(
- 'Create a new account',
- style: TextStyle(fontSize: 16.0),
- ),
- onPressed: moveToSignUp,
- ),
- ];
- } else {
- return [
- RaisedButton(
- //Complete 10: ganti teks dengan Sign In
- child: Text('Sign Up'),
- //TODO 23: ubah function menjadi validateAndSubmit
- onPressed: validateAndSubmit,
- color: Colors.deepOrange,
- textColor: Colors.white,
- ),
- //Complete 11: buat FlatButton untuk sign up
- FlatButton(
- child: Text(
- 'Already have an account? Sign In',
- style: TextStyle(fontSize: 16.0),
- ),
- onPressed: moveToSignIn,
- ),
- ];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement