-
Create a Form with a GlobalKey.
-
Add a TextFormField with validation logic.
-
Create a button to validate and submit the form.
First Step,
Add
final _formKey = new GlobalKey<FormState>();
// it is for defining a Key that contains the state
Next add
child: new Form(
key : _formKey,
child: new ListView(
shrinkWrap: true,
children: <Widget>[
showEmailInput(),
showPasswordInput(),
showPrimaryButton(context),
],
),
));
// in this example showEmailinput() and showPasswordInput() has each TextFromField, and by becasue all of the widget is //inside a Form Widget It is connected with same formKey.
Second Step,
Inside the TextFromField add validator and onSaved.
TextFormField(
maxLines: 1,
keyboardType: TextInputType.emailAddress,
autofocus: false,
decoration: new InputDecoration(
hintText: 'ID',
icon: new Icon(
Icons.perm_identity,
color: Colors.grey,
)),
validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
onSaved: (value) => data.id = value,
),
//value represent the data wrriten inside a TextFormField.
// validator gives the validation of the input
//if the value is Empty it gives this error.
// after the validator is satisfied onSaved gives the value to the variable. In this case data.id gets the value.
Third Step,
create a button.
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
print('valid');
}
//if the condition satifies all the validation it passes the value by onSaved.
'CSEE 관련 잡다 정보' 카테고리의 다른 글
Github Copilot 사용법 (Windos + VS Code) (1) | 2024.09.04 |
---|---|
VS Code 터미널과 코드 영역 간의 단축키 이동 설정 (Ctrl + ;, Window) (0) | 2024.08.30 |
Docker를 사용하여 oracle DB 사용하기 (0) | 2020.03.10 |
oracle developer download docker .yml 파일 (0) | 2020.03.10 |