Advertisement
rachmadi

flutter_authentication1

Sep 12th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.24 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. class _AuthenticationState extends State<Authentication> {
  9.  
  10.   //TODO 3: buat global key
  11.   final formKey = GlobalKey<FormState>();
  12.  
  13.   String _email;
  14.   String _password;
  15.  
  16.   void validateAndSave() {
  17.     //TODO 5: buat variabel form dan diberi state dari formKey
  18.     final form = formKey.currentState;
  19.     //TODO 9: simpan form
  20.     form.save();
  21.     //TODO 6: lakukan validasi
  22.     if(form.validate()) {
  23.       //TODO 10: print nilai _email dan _password
  24.       print('Form is valid. Email: $_email, Password: $_password');
  25.     } else {
  26.       print('Form is invalid. Email: $_email, Password: $_password');
  27.     }
  28.   }
  29.  
  30.   @override
  31.   Widget build(BuildContext context) {
  32.     return Scaffold(
  33.       appBar: AppBar(
  34.         title: Text('Autentication'),
  35.       ),
  36.       body: Container(
  37.         padding: EdgeInsets.all(16.0),
  38.         child: Form(
  39.           //TODO 4: pasang key
  40.           key: formKey,
  41.  
  42.           child: Column(
  43.             crossAxisAlignment: CrossAxisAlignment.center,
  44.             children: <Widget>[
  45.               TextFormField(
  46.                 keyboardType: TextInputType.emailAddress,
  47.                 decoration: InputDecoration(labelText: 'Email'),
  48.                 //TODO 1: validasi email
  49.                 validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
  50.                 //TODO 7: simpan nilai email
  51.                 onSaved: (value) => _email = value,
  52.               ),
  53.               TextFormField(
  54.                 decoration: InputDecoration(labelText: 'Password'),
  55.                 //TODO 2: validasi password
  56.                 validator: (value) => value.length < 6 ? 'Minimum 6 characters' : null,
  57.                 obscureText: true,
  58.                 //TODO 8: simpan nilai password
  59.                 onSaved: (value) => _password = value,
  60.               ),
  61.               RaisedButton(
  62.                 child: Text('Sign Up'),
  63.                 onPressed: validateAndSave,
  64.                 color: Colors.deepOrange,
  65.                 textColor: Colors.white,
  66.               ),
  67.             ],
  68.           ),
  69.         ),
  70.       ),
  71.     );
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement