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();
- }
- //TODO 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;
- //TODO 14: inisialisasi nilai formType
- FormType _formType = FormType.signIn;
- void 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');
- } else {
- print('Form is invalid. Email: $_email, Password: $_password');
- }
- }
- //TODO 12: buat function untuk berpindah dari Sign In ke Sign Up
- void moveToSignUp() {
- //TODO 15: set formType menjadi signUp dan reload
- setState(() {
- _formType = FormType.signUp;
- });
- }
- void moveToSignIn() {
- //TODO 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:
- //TODO 21: panggil buildInputs dan buildButtons
- buildInputs() + buildButtons(),
- ),
- ),
- ),
- );
- }
- //TODO 17: refactor kedua TextFormField
- List<Widget> buildInputs() {
- //TODO 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,
- ),
- ];
- }
- //TODO 19: refactor kedua Button
- List<Widget> buildButtons() {
- //TODO 20: pindahkan kedua Button
- return [
- RaisedButton(
- //TODO 10: ganti teks dengan Sign In
- child: Text('Sign In'),
- onPressed: validateAndSave,
- color: Colors.deepOrange,
- textColor: Colors.white,
- ),
- //TODO 11: buat FlatButton untuk sign up
- FlatButton(
- child: Text(
- 'Create a new account',
- style: TextStyle(fontSize: 16.0),
- ),
- onPressed: moveToSignUp,
- ),
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement