Skip to content

Instantly share code, notes, and snippets.

@GitLeandroHub
Created September 23, 2020 12:53
Show Gist options
  • Save GitLeandroHub/74fb13fba768abe0556478a1b219845b to your computer and use it in GitHub Desktop.
Save GitLeandroHub/74fb13fba768abe0556478a1b219845b to your computer and use it in GitHub Desktop.
Dart Learning: Simple Login Screen with StatefulWidget (+StatelessWidget for static)
//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(),
),
);
}
}
//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');
}
},
);
}
}
//D:\Projetos\CursoFlutter\FlutterApps\Login\login_stateful\lib\main.dart
import 'package:flutter/material.dart';
import 'src/app.dart';
void main() {
runApp(App());
}
//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