Created
December 16, 2020 02:07
-
-
Save ShaneRich5/e20a466193d87413b5b71f2687dce170 to your computer and use it in GitHub Desktop.
flutter sample
This file contains hidden or 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
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/material.dart'; | |
class RegistrationScreen extends StatefulWidget { | |
static const routeName = '/register'; | |
@override | |
State<StatefulWidget> createState() { | |
return _RegistrationState(); | |
} | |
} | |
class _RegistrationState extends State<RegistrationScreen> { | |
final _formKey = GlobalKey<FormState>(); | |
final _nameController = TextEditingController(); | |
final _emailController = TextEditingController(); | |
final _passwordController = TextEditingController(); | |
final _passwordConfirmController = TextEditingController(); | |
@override | |
void dispose() { | |
_nameController.dispose(); | |
_emailController.dispose(); | |
_passwordController.dispose(); | |
_passwordConfirmController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
leading: IconButton( | |
icon: Icon(Icons.menu), | |
tooltip: 'Navigation menu', | |
onPressed: null, | |
), | |
title: Text('Registration'), | |
actions: [ | |
IconButton( | |
icon: Icon(Icons.search), | |
tooltip: 'Search', | |
onPressed: null, | |
) | |
], | |
), | |
body: Padding( | |
padding: EdgeInsets.all(16.0), | |
child: Form( | |
key: _formKey, | |
child: Column( | |
children: <Widget>[ | |
TextFormField( | |
controller: _nameController, | |
decoration: InputDecoration( | |
labelText: 'Enter your name', | |
), | |
validator: (value) { | |
if (value.isEmpty) { | |
return 'Please enter your name'; | |
} | |
return null; | |
}, | |
), | |
TextFormField( | |
controller: _emailController, | |
decoration: InputDecoration( | |
labelText: 'Enter your email', | |
), | |
validator: (value) { | |
if (value.isEmpty) { | |
return 'Please enter your email'; | |
} | |
return null; | |
}, | |
), | |
TextFormField( | |
controller: _passwordController, | |
decoration: InputDecoration( | |
labelText: 'Enter a valid password', | |
), | |
validator: (value) { | |
if (value.isEmpty) { | |
return 'Please enter a valid password'; | |
} | |
return null; | |
}, | |
), | |
TextFormField( | |
controller: _passwordConfirmController, | |
decoration: InputDecoration( | |
labelText: 'Confirm your password', | |
), | |
validator: (value) { | |
if (value.isEmpty) { | |
return 'Please confirm your password'; | |
} | |
return null; | |
}, | |
), | |
Row( | |
children: [ | |
RaisedButton( | |
child: Text('Cancel'), | |
onPressed: () { | |
Navigator.pop(context); | |
}, | |
), | |
RaisedButton( | |
onPressed: () async { | |
if (_formKey.currentState.validate()) { | |
String email = _emailController.text; | |
String password = _passwordController.text; | |
await FirebaseAuth.instance | |
.createUserWithEmailAndPassword( | |
email: email, password: password) | |
.then((value) => showDialog( | |
context: context, | |
builder: (context) { | |
return AlertDialog( | |
content: Text(value.user.uid)); | |
}, | |
)) | |
.catchError((error) => print(error)); | |
} else { | |
Scaffold.of(context).showSnackBar( | |
SnackBar(content: Text('Error in the form'))); | |
} | |
}, | |
child: Text('Submit'), | |
) | |
], | |
) | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment