Skip to content

Instantly share code, notes, and snippets.

@gonexwind
Created August 12, 2023 02:43
Show Gist options
  • Save gonexwind/c79e9ecc1beaee2a5448c7cc810470b9 to your computer and use it in GitHub Desktop.
Save gonexwind/c79e9ecc1beaee2a5448c7cc810470b9 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class SignInPage extends StatefulWidget {
const SignInPage({super.key});
@override
State<SignInPage> createState() => _SignInPageState();
}
class _SignInPageState extends State<SignInPage> {
final _signInKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final _emailRegex = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+");
@override
void dispose() {
super.dispose();
_emailController.dispose();
_passwordController.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sign In'),
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Form(
key: _signInKey,
child: Column(
children: [
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Email can\'t be empty';
}
if (!_emailRegex.hasMatch(value)) {
return 'Please enter a valid email';
}
return null;
},
),
TextFormField(
controller: _passwordController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Password can\'t be empty';
}
if (value.length < 6) {
return 'Password at least 6 characters';
}
return null;
},
),
SizedBox(height: 8),
ElevatedButton(
child: Text('Sign In'),
onPressed: () {
if (_signInKey.currentState?.validate() ?? false) {
debugPrint('Email: ${_emailController.text}');
debugPrint('Email: ${_passwordController.text}');
}
},
),
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment