Advertisement
rachmadi

flutter_authentication3

Sep 12th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.64 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. class Authentication extends StatefulWidget {
  4.   @override
  5.   _AuthenticationState createState() => _AuthenticationState();
  6. }
  7.  
  8. //Complete 13: buat 2 nilai enum untuk FormType
  9. enum FormType { signIn, signUp }
  10.  
  11. class _AuthenticationState extends State<Authentication> {
  12.   //Complete 3: buat global key
  13.   final formKey = GlobalKey<FormState>();
  14.  
  15.   String _email;
  16.   String _password;
  17.  
  18.   //Complete 14: inisialisasi nilai formType
  19.   FormType _formType = FormType.signIn;
  20.  
  21.   //TODO 24: ubah return value jadi bool
  22.   bool validateAndSave() {
  23.     //Complete 5: buat variabel form dan diberi state dari formKey
  24.     final form = formKey.currentState;
  25.     //Complete 9: simpan form
  26.     form.save();
  27.     //Complete 6: lakukan validasi
  28.     if (form.validate()) {
  29.       //Complete 10: print nilai _email dan _password
  30.       print('Form is valid. Email: $_email, Password: $_password');
  31.       return true;
  32.     } else {
  33.       print('Form is invalid. Email: $_email, Password: $_password');
  34.       return false;
  35.     }
  36.   }
  37.  
  38.   //TODO 25: buat function validateAndSubmit
  39.   void validateAndSubmit() {
  40.     //TODO 26: cek apakah form valid
  41.     if(validateAndSave()) {
  42.       //TODO 27: gunakan try-catch untuk memeriksa error
  43.       try{
  44.         //TODO 28: kondisi signIn atau signUp
  45.         if(_formType == FormType.signIn) {
  46.           //TODO 29: buat proses signIn
  47.         } else {
  48.           //TODO 30: buat proses signUp
  49.         }
  50.       } catch (e) {
  51.         print("Error: $e");
  52.       }
  53.     }
  54.   }
  55.  
  56.   //Complete 12: buat function untuk berpindah dari Sign In ke Sign Up
  57.   void moveToSignUp() {
  58.     //Complete 15: set formType menjadi signUp dan reload
  59.     setState(() {
  60.       _formType = FormType.signUp;
  61.     });
  62.   }
  63.  
  64.   void moveToSignIn() {
  65.     //Complete 16: set formType menjadi signIn dan reload
  66.     setState(() {
  67.       _formType = FormType.signIn;
  68.     });
  69.   }
  70.  
  71.   @override
  72.   Widget build(BuildContext context) {
  73.     return Scaffold(
  74.       appBar: AppBar(
  75.         title: Text('Autentication'),
  76.       ),
  77.       body: Container(
  78.         padding: EdgeInsets.all(16.0),
  79.         child: Form(
  80.           //Complete 4: pasang key
  81.           key: formKey,
  82.  
  83.           child: Column(
  84.             crossAxisAlignment: CrossAxisAlignment.center,
  85.             children:
  86.                 //Complete 21: panggil buildInputs dan buildButtons
  87.                 buildInputs() + buildButtons(),
  88.           ),
  89.         ),
  90.       ),
  91.     );
  92.   }
  93.  
  94.   //Complete 17: refactor kedua TextFormField
  95.   List<Widget> buildInputs() {
  96.     //Complete 18: pindahkan kedua TextFormField
  97.     return [
  98.       TextFormField(
  99.         keyboardType: TextInputType.emailAddress,
  100.         decoration: InputDecoration(labelText: 'Email'),
  101.         //Complete 1: validasi email
  102.         validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
  103.         //Complete 7: simpan nilai email
  104.         onSaved: (value) => _email = value,
  105.       ),
  106.       TextFormField(
  107.         decoration: InputDecoration(labelText: 'Password'),
  108.         //Complete 2: validasi password
  109.         validator: (value) => value.length < 6 ? 'Minimum 6 characters' : null,
  110.         obscureText: true,
  111.         //Complete 8: simpan nilai password
  112.         onSaved: (value) => _password = value,
  113.       ),
  114.     ];
  115.   }
  116.  
  117.   //Complete 19: refactor kedua Button
  118.   List<Widget> buildButtons() {
  119.     //TODO 21: buat kondisi untuk signUp dan signIn
  120.     if (_formType == FormType.signIn) {
  121.       //Complete 20: pindahkan kedua Button
  122.       return [
  123.         RaisedButton(
  124.           //Complete 10: ganti teks dengan Sign In
  125.           child: Text('Sign In'),
  126.           //TODO 22: ubah function menjadi validateAndSubmit
  127.           onPressed: validateAndSubmit,
  128.           color: Colors.deepOrange,
  129.           textColor: Colors.white,
  130.         ),
  131.         //Complete 11: buat FlatButton untuk sign up
  132.         FlatButton(
  133.           child: Text(
  134.             'Create a new account',
  135.             style: TextStyle(fontSize: 16.0),
  136.           ),
  137.           onPressed: moveToSignUp,
  138.         ),
  139.       ];
  140.     } else {
  141.       return [
  142.         RaisedButton(
  143.           //Complete 10: ganti teks dengan Sign In
  144.           child: Text('Sign Up'),
  145.           //TODO 23: ubah function menjadi validateAndSubmit
  146.           onPressed: validateAndSubmit,
  147.           color: Colors.deepOrange,
  148.           textColor: Colors.white,
  149.         ),
  150.         //Complete 11: buat FlatButton untuk sign up
  151.         FlatButton(
  152.           child: Text(
  153.             'Already have an account? Sign In',
  154.             style: TextStyle(fontSize: 16.0),
  155.           ),
  156.           onPressed: moveToSignIn,
  157.         ),
  158.       ];
  159.     }
  160.   }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement