Created
September 23, 2020 12:53
-
-
Save GitLeandroHub/74fb13fba768abe0556478a1b219845b to your computer and use it in GitHub Desktop.
Dart Learning: Simple Login Screen with StatefulWidget (+StatelessWidget for static)
This file contains 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
//D:\Projetos\CursoFlutter\FlutterApps\Login\login_stateful\lib\src\app.dart | |
import 'package:flutter/material.dart'; | |
import 'screens/login_screen.dart'; | |
class App extends StatelessWidget { | |
Widget build(context) { | |
return MaterialApp( | |
title: 'Log Me In!', | |
home: Scaffold( | |
body: LoginScreen(), | |
), | |
); | |
} | |
} |
This file contains 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
//D:\Projetos\CursoFlutter\FlutterApps\Login\login_stateful\lib\main.dart | |
import 'package:flutter/material.dart'; | |
import '../mixins/validation_mixin.dart'; | |
class LoginScreen extends StatefulWidget { | |
createState() { | |
return LoginScreenState(); | |
} | |
} | |
class LoginScreenState extends State<LoginScreen> with ValidationMixin { | |
final formKey = GlobalKey<FormState>(); | |
String email = ''; | |
String password = ''; | |
Widget build(context) { | |
return Container( | |
margin: EdgeInsets.all(20.0), | |
child: Form( | |
key: formKey, | |
child: Column( | |
children: [ | |
emailField(), | |
passwordField(), | |
Container(margin: EdgeInsets.only(top: 25.00)), | |
submitButton(), | |
], | |
), | |
), | |
); | |
} | |
Widget emailField() { | |
return TextFormField( | |
keyboardType: TextInputType.emailAddress, | |
decoration: InputDecoration( | |
labelText: 'E-mail Adress', | |
hintText: 'you@example.com', | |
), | |
validator: validateEmail, | |
onSaved: (String value) { | |
email = value; | |
}, | |
); | |
} | |
Widget passwordField() { | |
return TextFormField( | |
obscureText: true, | |
decoration: InputDecoration( | |
labelText: 'Password', | |
hintText: 'Password', | |
), | |
validator: validatePassword, | |
onSaved: (String value) { | |
password = value; | |
}, | |
); | |
} | |
Widget submitButton() { | |
return RaisedButton( | |
color: Colors.blue, | |
child: Text('Sutmit'), | |
onPressed: () { | |
if (formKey.currentState.validate()) { | |
formKey.currentState.save(); | |
print('Time to post $email and $password to my API'); | |
} | |
}, | |
); | |
} | |
} |
This file contains 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
//D:\Projetos\CursoFlutter\FlutterApps\Login\login_stateful\lib\main.dart | |
import 'package:flutter/material.dart'; | |
import 'src/app.dart'; | |
void main() { | |
runApp(App()); | |
} |
This file contains 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
//D:\Projetos\CursoFlutter\FlutterApps\Login\login_stateful\lib\src\mixins\validation_mixin.dart | |
import 'package:flutter/material.dart'; | |
import 'src/app.dart'; | |
void main() { | |
runApp(App()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment