Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5e2e402

Browse files
implemented form validation
1 parent a2eca29 commit 5e2e402

File tree

3 files changed

+215
-170
lines changed

3 files changed

+215
-170
lines changed

‎day_07_05_ui_simple_login_screen/lib/login_screen.dart‎

Lines changed: 96 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ class LoginScreen extends StatefulWidget {
88

99
class _LoginScreenState extends State<LoginScreen> {
1010
BuildContext _scaffoldContext;
11-
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
11+
final _scaffoldKey = GlobalKey<ScaffoldState>();
12+
final _formKey = GlobalKey<FormState>();
1213
double _nHeightBetweenWidgets = 15.0;
1314
double _nMaterialButtonMinWidth = 190.0;
1415

16+
String _inputEmail, _inputPassword;
1517
var _textfieldControllerEmail = TextEditingController();
1618
var _textfieldControllerPassword = TextEditingController();
1719
final FocusNode _focusNodeEmail = FocusNode();
@@ -62,84 +64,102 @@ class _LoginScreenState extends State<LoginScreen> {
6264
_scaffoldContext = context;
6365
return SingleChildScrollView(
6466
padding: EdgeInsets.all(16.0),
65-
child: Column(
66-
mainAxisAlignment: MainAxisAlignment.center,
67-
crossAxisAlignment: CrossAxisAlignment.center,
68-
children: <Widget>[
69-
FlutterLogo(
70-
size: 140,
71-
),
72-
SizedBox(
73-
height: _nHeightBetweenWidgets,
74-
),
75-
TextFormField(
76-
controller: _textfieldControllerEmail,
77-
keyboardType: TextInputType.emailAddress,
78-
focusNode: _focusNodeEmail,
79-
textInputAction: TextInputAction.next,
80-
onFieldSubmitted: (term) {
81-
_textfieldChangeFocus(
82-
context, _focusNodeEmail, _focusNodePassword);
83-
},
84-
decoration: Constants.inputDecorationEmail,
85-
),
86-
SizedBox(
87-
height: _nHeightBetweenWidgets,
88-
),
89-
TextFormField(
90-
controller: _textfieldControllerPassword,
91-
focusNode: _focusNodePassword,
92-
textInputAction: TextInputAction.done,
93-
onFieldSubmitted: (term) {
94-
_focusNodePassword.unfocus();
95-
},
96-
obscureText: true,
97-
decoration: Constants.inputDecorationPassword,
98-
),
99-
SizedBox(
100-
height: _nHeightBetweenWidgets,
101-
),
102-
MaterialButton(
103-
height: 50,
104-
color: Colors.blue,
105-
minWidth: _nMaterialButtonMinWidth,
106-
child: Text(
107-
'Login',
108-
style: Constants.textStyleLoginButton,
67+
child: Form(
68+
key: _formKey,
69+
//autovalidate:true, // realtime validate textField inputs as you type along
70+
child: Column(
71+
mainAxisAlignment: MainAxisAlignment.center,
72+
crossAxisAlignment: CrossAxisAlignment.center,
73+
children: <Widget>[
74+
FlutterLogo(
75+
size: 140,
10976
),
110-
onPressed: () {
111-
if (_validateInputsFields()) {
112-
print('proceed for login');
113-
}
114-
},
115-
),
116-
SizedBox(
117-
height: 50,
118-
),
119-
MaterialButton(
120-
color: Colors.red,
121-
height: 40,
122-
minWidth: _nMaterialButtonMinWidth,
123-
child: Text(
124-
'Google',
125-
style: Constants.textStyleLoginButton,
77+
SizedBox(
78+
height: _nHeightBetweenWidgets,
12679
),
127-
onPressed: () {},
128-
),
129-
SizedBox(
130-
height: 7,
131-
),
132-
MaterialButton(
133-
color: Colors.blue[900],
134-
height: 40,
135-
minWidth: _nMaterialButtonMinWidth,
136-
child: Text(
137-
'Facebook',
138-
style: Constants.textStyleLoginButton,
80+
TextFormField(
81+
controller: _textfieldControllerEmail,
82+
keyboardType: TextInputType.emailAddress,
83+
focusNode: _focusNodeEmail,
84+
validator: (typedText) =>
85+
Constants.validateEmailMethod(typedText),
86+
onSaved: (typedText) => _inputEmail = typedText,
87+
textInputAction: TextInputAction.next,
88+
onFieldSubmitted: (term) {
89+
_textfieldChangeFocus(
90+
context, _focusNodeEmail, _focusNodePassword);
91+
},
92+
decoration: Constants.inputDecorationEmail,
13993
),
140-
onPressed: () {},
141-
),
142-
],
94+
SizedBox(
95+
height: _nHeightBetweenWidgets,
96+
),
97+
TextFormField(
98+
controller: _textfieldControllerPassword,
99+
focusNode: _focusNodePassword,
100+
validator: (typedText) =>
101+
Constants.validatePasswordMethod(typedText),
102+
onSaved: (typedText) => _inputPassword = typedText,
103+
textInputAction: TextInputAction.done,
104+
onFieldSubmitted: (term) {
105+
_focusNodePassword.unfocus();
106+
},
107+
obscureText: true,
108+
decoration: Constants.inputDecorationPassword,
109+
),
110+
SizedBox(
111+
height: _nHeightBetweenWidgets,
112+
),
113+
MaterialButton(
114+
height: 50,
115+
color: Colors.blue,
116+
minWidth: _nMaterialButtonMinWidth,
117+
child: Text(
118+
'Login',
119+
style: Constants.textStyleLoginButton,
120+
),
121+
onPressed: () {
122+
// if (_validateInputsFields()) {
123+
// print('proceed for login');
124+
// }
125+
126+
if (_formKey.currentState.validate()) {
127+
print('save textField text to your string variable');
128+
_formKey.currentState.save();
129+
print('proceed for login');
130+
print('email = $_inputEmail');
131+
print('password = $_inputPassword');
132+
}
133+
},
134+
),
135+
SizedBox(
136+
height: 50,
137+
),
138+
MaterialButton(
139+
color: Colors.red,
140+
height: 40,
141+
minWidth: _nMaterialButtonMinWidth,
142+
child: Text(
143+
'Google',
144+
style: Constants.textStyleLoginButton,
145+
),
146+
onPressed: () {},
147+
),
148+
SizedBox(
149+
height: 7,
150+
),
151+
MaterialButton(
152+
color: Colors.blue[900],
153+
height: 40,
154+
minWidth: _nMaterialButtonMinWidth,
155+
child: Text(
156+
'Facebook',
157+
style: Constants.textStyleLoginButton,
158+
),
159+
onPressed: () {},
160+
),
161+
],
162+
),
143163
),
144164
);
145165
}),

‎day_07_05_ui_simple_login_screen/lib/main.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class MyApp extends StatelessWidget {
1212
return MaterialApp(
1313
debugShowCheckedModeBanner: false,
1414
theme: ThemeData(primarySwatch: Colors.blue),
15-
//home: LoginScreen(),
16-
home: SignUpScreen(),
15+
home: LoginScreen(),
16+
//home: SignUpScreen(),
1717
);
1818
}
1919
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /