Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Created January 9, 2023 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelbourneDeveloper/2d6f00b02d73428a0289d16342df5ae6 to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/2d6f00b02d73428a0289d16342df5ae6 to your computer and use it in GitHub Desktop.
Regsistration Screen Built By ChatGPT
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RegisterScreen()),
);
},
child: Text('Sign Up'),
),
),
);
}
}
class RegisterScreen extends StatefulWidget {
@override
_RegisterScreenState createState() => _RegisterScreenState();
}
class _RegisterScreenState extends State<RegisterScreen> {
final _formKey = GlobalKey<FormState>();
String _email = '';
String _password = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Create an Account'),
),
body: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
decoration: InputDecoration(labelText: 'Email'),
validator: (input) => (input == null || !input.contains('@'))
? 'Please enter a valid email'
: null,
onSaved: (input) => _email = input ?? '',
),
TextFormField(
decoration: InputDecoration(labelText: 'Password'),
validator: (input) => input == null || input.length < 8
? 'Must be at least 8 characters'
: null,
onSaved: (input) => _password = input ?? '',
obscureText: true,
),
TextButton(
onPressed: _submit,
child: Text('Sign Up'),
),
],
),
),
);
}
void _submit() {
final form = _formKey.currentState;
if (form == null) {
return;
}
if (form.validate()) {
form.save();
// Send the data to the API
print(_email);
print(_password);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment