| import 'package:flutter/material.dart'; |
| import 'package:firebase_auth/firebase_auth.dart'; |
| |
| final FirebaseAuth _auth = FirebaseAuth.instance; |
| |
| class RegisterPage extends StatefulWidget { |
| final String title = 'Registration'; |
| @override |
| State<StatefulWidget> createState() => RegisterPageState(); |
| } |
| |
| class RegisterPageState extends State<RegisterPage> { |
| final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); |
| final TextEditingController _emailController = TextEditingController(); |
| final TextEditingController _passwordController = TextEditingController(); |
| bool _success; |
| String _userEmail; |
| @override |
| Widget build(BuildContext context) { |
| return Scaffold( |
| appBar: AppBar( |
| title: Text(widget.title), |
| ), |
| body: Form( |
| key: _formKey, |
| child: Column( |
| crossAxisAlignment: CrossAxisAlignment.start, |
| children: <Widget>[ |
| TextFormField( |
| controller: _emailController, |
| decoration: InputDecoration(labelText: 'Email'), |
| validator: (String value) { |
| if (value.isEmpty) { |
| return 'Please enter some text'; |
| } |
| return null; |
| }, |
| ), |
| TextFormField( |
| controller: _passwordController, |
| decoration: InputDecoration(labelText: 'Password'), |
| validator: (String value) { |
| if (value.isEmpty) { |
| return 'Please enter some text'; |
| } |
| return null; |
| }, |
| ), |
| Container( |
| padding: const EdgeInsets.symmetric(vertical: 16.0), |
| alignment: Alignment.center, |
| child: RaisedButton( |
| onPressed: () async { |
| if (_formKey.currentState.validate()) { |
| _register(); |
| } |
| }, |
| child: const Text('Submit'), |
| ), |
| ), |
| Container( |
| alignment: Alignment.center, |
| child: Text(_success == null |
| ? '' |
| : (_success |
| ? 'Successfully registered ' + _userEmail |
| : 'Registration failed')), |
| ) |
| ], |
| ), |
| ), |
| ); |
| } |
| |
| @override |
| void dispose() { |
| // Clean up the controller when the Widget is disposed |
| _emailController.dispose(); |
| _passwordController.dispose(); |
| super.dispose(); |
| } |
| |
| // Example code for registration. |
| void _register() async { |
| final FirebaseUser user = await _auth.createUserWithEmailAndPassword( |
| email: _emailController.text, |
| password: _passwordController.text, |
| ); |
| if (user != null) { |
| setState(() { |
| _success = true; |
| _userEmail = user.email; |
| }); |
| } else { |
| _success = false; |
| } |
| } |
| } |