This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Widget get _buildAgreeToTermsField { | |
| // TODO 8: Wrap the Column with a FormField<bool> | |
| return Column( | |
| children: <Widget>[ | |
| Row( | |
| children: <Widget>[ | |
| Checkbox( | |
| value: _agree, | |
| onChanged: (bool val) => setState(() { | |
| _agree = val; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String _validatePassword(String pass1) { | |
| // 1 | |
| RegExp hasUpper = RegExp(r'[A-Z]'); | |
| RegExp hasLower = RegExp(r'[a-z]'); | |
| RegExp hasDigit = RegExp(r'\d'); | |
| RegExp hasPunct = RegExp(r'[!@#\$&*~-]'); | |
| // 2 | |
| if (!RegExp(r'.{8,}').hasMatch(pass1)) | |
| return 'Passwords must have at least 8 characters'; | |
| // 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // TODO 5: Add onSaved and validator methods | |
| // 1 | |
| onSaved: (String val) => _loginObject['email'] = val, | |
| // 2 | |
| validator: _validateEmail, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String _validateEmail(String email) { | |
| // 1 | |
| RegExp regex = RegExp(r'\w+@\w+\.\w+'); // translates to word@word.word | |
| // 2 | |
| if (email.isEmpty) | |
| return 'We need an email address'; | |
| else if (!regex.hasMatch(email)) | |
| // 3 | |
| return "That doesn't look like an email address"; | |
| else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void _doRegister() { | |
| // TODO 3: Add validation and saving here | |
| // 1 | |
| if (_key.currentState.validate()) { | |
| // Commit the field values to their variables | |
| // 2 | |
| _key.currentState.save(); | |
| // 3 | |
| print(""" | |
| The user has registered with an email address of '${_loginObject['email']}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void _doRegister() { | |
| // TODO 3: Add validation and saving here | |
| print(""" | |
| The user has registered with an email address of '${_loginObject['email']}' | |
| and a password of '${_loginObject['password']}' | |
| """); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Without a Form is simpler but... | With a Form is more complex but... | |
|---|---|---|
| You have very little control over the fields | You have lots of control over the fields | |
| Fields are unaware of each other | Field data can be evaluated as a group | |
| Validations are manual so you have to write more code | Validations are easier because they're declarative |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| body: Form( | |
| key: _key, // ← Add this line | |
| child: Container( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // TODO 2: Add a GlobalKey declaration | |
| GlobalKey<FormState> _key = GlobalKey<FormState>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // TODO 2: Add a GlobalKey | |
| @override | |
| Widget build(BuildContext context) { |