Skip to content

Instantly share code, notes, and snippets.

@chummypixels
Last active December 26, 2021 09:52
Show Gist options
  • Save chummypixels/b2c7ea81462d0ac406fdb6ea8290536b to your computer and use it in GitHub Desktop.
Save chummypixels/b2c7ea81462d0ac406fdb6ea8290536b to your computer and use it in GitHub Desktop.
//1
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../profile/profile_screen.dart';
import '../services/auth.dart';
class RegisterScreen extends StatefulWidget {
const RegisterScreen({Key? key}) : super(key: key);
@override
State<RegisterScreen> createState() => _RegisterScreenState();
}
class _RegisterScreenState extends State<RegisterScreen> {
//2
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
//3
final _formKey = GlobalKey<FormState>();
//4
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
//5
final auth = Provider.of<Auth>(context);
return Scaffold(
appBar: AppBar(
title: const Text('Register'),
),
body: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
// 6
child: TextFormField(
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Enter your email'),
keyboardType: TextInputType.emailAddress,
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Email is required';
}
return null;
},
controller: _emailController,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
//7
child: TextFormField(
obscureText: true,
controller: _passwordController,
decoration:
const InputDecoration(labelText: 'Enter your password'),
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Password is required';
} else if (value.length < 6) {
return 'Password should be at least 6 characters';
}
return null;
},
),
),
ElevatedButton(
onPressed: () async {
final isValid = _formKey.currentState!.validate();
//8
await auth
.handleSignUp(
_emailController.text, _passwordController.text)
.then((value) {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const ProfileScreen()));
}).catchError((e) => print(e));
},
child: const Text('Register'))
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment