Repository for exercise E6. Course IDATA2503 Mobile applications at NTNU, campus Aalesund.
The intention is to practice creating forms with user input and do some basic form validation and display of error messages.
Create a clone of the template repository within GitHub Classroom. Use the invitation link you got from Blackboard! Get the automated tests passing.
To pass the assignment approved you need to get at least 180 points.
As always, you can run flutter test
inside the project folder to run all the tests. As an
alternative, use the testing features in your IDE - right-click on the necessary places, etc. You
know the drill ;)
P.S. Remember how you can run tests in a single test-file? flutter test test/step_1_1_test.dart
etc.
Your task is to implement a form where the user can input username, email and password, and press on a "Submit" button. If an input contains an incorrect value, you must mark that input field and display an appropriate error message.
The implementation consists of two parts:
- Creating the UI layout
- Setting up validation logic
Create the elements for the input form according to the following specification (which will be checked in the tests):
- Add an input field for the username: a
TextField
widget (note: later we will talk aboutTextFormField
widget as well, but don't use it for now as this may conflict with unit tests). Implement the following properties for this TextField:- Set the
key
property for it tousername_input
. We have not used thekey
property before. In general - it is like anid
used for web elements - an identifier that uniquely identifies an element in the UI (an instance of a widget). Hint: in the constructor you can set the key as follows:key: Key("username_input")
- Set the label to
Username
.
- Set the
- Add an input field for the email: a
TextField
widget with the following properties:- The
key
property having valueemail_input
. - Label
Email
- Make it an email-input field. I.e., when the user will enter the value here, she should see the keyboard layout made for entering email addresses.
- The
- Add an input field for the password with the following properties:
key
having valuepassword_input
- Label
Password
- Make it a password-input-field: the typed characters must not be visible!
- Add a "Submit" button with the following features:
key
with valuesubmit_button
- Text in it saying
Submit form
When you are done with the UI, it should look something along these lines:
Note: there will be no comparison with a golden image in this exercise, therefore specific fonts, paddings and color's don't matter in this case.
Now implement the following logic for validation:
- There should be no validation before the "Submit form" button is pressed. I.e., the user can enter any values and no error messages are shown first. Only after the "Submit form" button is pressed, the validation is enabled.
- When the input field validation is enabled, the following rules apply:
- The username field is considered invalid if it does not satisfy the following mandatory
requirements:
- It must contain only lowercase or uppercase letters from the English alphabet [a-z, A-Z], no other symbols are allowed (Norwegian letters øåæ are also not allowed).
- It must contain at least 3 characters
- It must not be longer than 12 characters
- Examples of valid usernames:
chuck
,Chuck
,cucumber
,PewDiePie
,cat
- Examples of invalid usernames:
chuck12
,nun-chuck
,Pære
,me
, empty string.
- Conditions for the email field to be considered valid:
- It must not be empty
- It must be a "valid email address". There are several rules (and different regular expressions available online). Find a solution for checking whether a string is a valid email address!
- Examples of valid email addresses:
microsoft@chuck.com
,the.big_chuck@big.mac.com
,chuck+ducktape@apple.com
- Examples of invalid email addresses:
@thechuck
,@thechuck.com
,duck@
- Password is considered valid if it contains the following:
- at least 6 characters
- no more than 20 characters (in total)
- at least one uppercase letter
- at least one lowercase letter
- at least one digit
- If any of the error mentioned above are detected (any of the fields contains an invalid value), the "Submit form" button is disabled
- The "Submit form" button is enabled again when all errors in the form are fixed and all input fields contain a valid value
- If the username is invalid, the
TextField
is marked with red and message "invalid username" is displayed below the field. (Hint: useerrorText
for theTextField
, don't create a new widget for the error). See an example below. - In a similar manner - if the email is invalid, show an error "invalid email format"
- If the password is invalid, show error "6-20 chars, uppercase, lowercase, digits".
- The username field is considered invalid if it does not satisfy the following mandatory
requirements:
- If the "Submit form" button is pressed and all the fields are valid:
- Hide the "Submit form" button
- Disable all input fields - the user should not be able to change any text within them
- Display a
CircularProgressIndicator
in the place where the button was located
Here is an example of a form showing error messages for the fields:
P.S. This may sound like a lot of logic, but all the code you write should be re-usable in your project ;)
P.P.S. For now you don't need to worry about separating the logic from the UI widgets , we will do that a bit later.