Skip to content

Instantly share code, notes, and snippets.

View rapPayne's full-sized avatar
:octocat:
Working from home

Rap Payne rapPayne

:octocat:
Working from home
View GitHub Profile
@rapPayne
rapPayne / Register.dart
Last active October 21, 2020 22:33
Validating a checkbox -- before
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;
@rapPayne
rapPayne / Register.dart
Created October 21, 2020 21:44
Creating the _validatePassword method
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
@rapPayne
rapPayne / Register.dart
Created October 21, 2020 21:35
_buildEmailField validation
// TODO 5: Add onSaved and validator methods
// 1
onSaved: (String val) => _loginObject['email'] = val,
// 2
validator: _validateEmail,
@rapPayne
rapPayne / Register.dart
Created October 21, 2020 21:25
Validating a Simple TextFormField
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
@rapPayne
rapPayne / Register.dart
Created October 21, 2020 21:16
_doRegister after saving
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']}'
@rapPayne
rapPayne / Register.dart
Created October 21, 2020 19:15
_doRegister before saving
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']}'
""");
}
@rapPayne
rapPayne / WhyUseAForm.csv
Last active October 21, 2020 18:48
Why use a form?
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
@rapPayne
rapPayne / Register.dart
Created September 24, 2020 18:02
Adding the key to the Form widget
body: Form(
key: _key, // ← Add this line
child: Container(
@rapPayne
rapPayne / Register.dart
Last active September 24, 2020 17:58
Adding a GlobalKey for the Form
// TODO 2: Add a GlobalKey declaration
GlobalKey<FormState> _key = GlobalKey<FormState>();
@rapPayne
rapPayne / Register.dart
Created September 24, 2020 17:54
Before adding a GlobalKey
// TODO 2: Add a GlobalKey
@override
Widget build(BuildContext context) {