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();
- }
- class _AuthenticationState extends State<Authentication> {
- //TODO 3: buat global key
- final formKey = GlobalKey<FormState>();
- String _email;
- String _password;
- void validateAndSave() {
- //TODO 5: buat variabel form dan diberi state dari formKey
- final form = formKey.currentState;
- //TODO 9: simpan form
- form.save();
- //TODO 6: lakukan validasi
- if(form.validate()) {
- //TODO 10: print nilai _email dan _password
- print('Form is valid. Email: $_email, Password: $_password');
- } else {
- print('Form is invalid. Email: $_email, Password: $_password');
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Autentication'),
- ),
- body: Container(
- padding: EdgeInsets.all(16.0),
- child: Form(
- //TODO 4: pasang key
- key: formKey,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- TextFormField(
- keyboardType: TextInputType.emailAddress,
- decoration: InputDecoration(labelText: 'Email'),
- //TODO 1: validasi email
- validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
- //TODO 7: simpan nilai email
- onSaved: (value) => _email = value,
- ),
- TextFormField(
- decoration: InputDecoration(labelText: 'Password'),
- //TODO 2: validasi password
- validator: (value) => value.length < 6 ? 'Minimum 6 characters' : null,
- obscureText: true,
- //TODO 8: simpan nilai password
- onSaved: (value) => _password = value,
- ),
- RaisedButton(
- child: Text('Sign Up'),
- onPressed: validateAndSave,
- color: Colors.deepOrange,
- textColor: Colors.white,
- ),
- ],
- ),
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement