Advertisement
rachmadi

flutter_authentication2

Sep 12th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.26 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. //TODO 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.   //TODO 14: inisialisasi nilai formType
  19.   FormType _formType = FormType.signIn;
  20.  
  21.   void validateAndSave() {
  22.     //Complete 5: buat variabel form dan diberi state dari formKey
  23.     final form = formKey.currentState;
  24.     //Complete 9: simpan form
  25.     form.save();
  26.     //Complete 6: lakukan validasi
  27.     if (form.validate()) {
  28.       //Complete 10: print nilai _email dan _password
  29.       print('Form is valid. Email: $_email, Password: $_password');
  30.     } else {
  31.       print('Form is invalid. Email: $_email, Password: $_password');
  32.     }
  33.   }
  34.  
  35.   //TODO 12: buat function untuk berpindah dari Sign In ke Sign Up
  36.   void moveToSignUp() {
  37.     //TODO 15: set formType menjadi signUp dan reload
  38.     setState(() {
  39.       _formType = FormType.signUp;
  40.     });
  41.   }
  42.  
  43.   void moveToSignIn() {
  44.     //TODO 16: set formType menjadi signIn dan reload
  45.     setState(() {
  46.       _formType = FormType.signIn;
  47.     });
  48.   }
  49.  
  50.   @override
  51.   Widget build(BuildContext context) {
  52.     return Scaffold(
  53.       appBar: AppBar(
  54.         title: Text('Autentication'),
  55.       ),
  56.       body: Container(
  57.         padding: EdgeInsets.all(16.0),
  58.         child: Form(
  59.           //Complete 4: pasang key
  60.           key: formKey,
  61.  
  62.           child: Column(
  63.             crossAxisAlignment: CrossAxisAlignment.center,
  64.             children:
  65.                 //TODO 21: panggil buildInputs dan buildButtons
  66.                 buildInputs() + buildButtons(),
  67.           ),
  68.         ),
  69.       ),
  70.     );
  71.   }
  72.  
  73.   //TODO 17: refactor kedua TextFormField
  74.   List<Widget> buildInputs() {
  75.     //TODO 18: pindahkan kedua TextFormField
  76.     return [
  77.       TextFormField(
  78.         keyboardType: TextInputType.emailAddress,
  79.         decoration: InputDecoration(labelText: 'Email'),
  80.         //Complete 1: validasi email
  81.         validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
  82.         //Complete 7: simpan nilai email
  83.         onSaved: (value) => _email = value,
  84.       ),
  85.       TextFormField(
  86.         decoration: InputDecoration(labelText: 'Password'),
  87.         //Complete 2: validasi password
  88.         validator: (value) => value.length < 6 ? 'Minimum 6 characters' : null,
  89.         obscureText: true,
  90.         //Complete 8: simpan nilai password
  91.         onSaved: (value) => _password = value,
  92.       ),
  93.     ];
  94.   }
  95.  
  96.   //TODO 19: refactor kedua Button
  97.   List<Widget> buildButtons() {
  98.     //TODO 20: pindahkan kedua Button
  99.     return [
  100.       RaisedButton(
  101.         //TODO 10: ganti teks dengan Sign In
  102.         child: Text('Sign In'),
  103.         onPressed: validateAndSave,
  104.         color: Colors.deepOrange,
  105.         textColor: Colors.white,
  106.       ),
  107.       //TODO 11: buat FlatButton untuk sign up
  108.       FlatButton(
  109.         child: Text(
  110.           'Create a new account',
  111.           style: TextStyle(fontSize: 16.0),
  112.         ),
  113.         onPressed: moveToSignUp,
  114.       ),
  115.     ];
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement