Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email validation and form reset after submission #10

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class _MyHomePageState extends State<MyHomePage> {
if (response == FormController.STATUS_SUCCESS) {
// Feedback is saved succesfully in Google Sheets.
_showSnackbar("Feedback Submitted");
nameController.clear();
emailController.clear();
mobileNoController.clear();
feedbackController.clear();
} else {
// Error Occurred while saving data in Google Sheets.
_showSnackbar("Error Occurred!");
Expand All @@ -73,6 +77,14 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

bool isEmail(String email) {
bool emailValid = RegExp(
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
.hasMatch(email);

return emailValid;
}

// Method to show snackbar with 'message'.
_showSnackbar(String message) {
final snackBar = SnackBar(content: Text(message));
Expand All @@ -83,7 +95,7 @@ class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
resizeToAvoidBottomPadding: false,
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text(widget.title),
),
Expand Down Expand Up @@ -111,7 +123,7 @@ class _MyHomePageState extends State<MyHomePage> {
TextFormField(
controller: emailController,
validator: (value) {
if (!value.contains("@")) {
if (!isEmail(value)) {
return 'Enter Valid Email';
}
return null;
Expand Down Expand Up @@ -146,15 +158,20 @@ class _MyHomePageState extends State<MyHomePage> {
],
),
)),
RaisedButton(
MaterialButton(
elevation: 5,
color: Colors.blue,
textColor: Colors.white,
onPressed: _submitForm,
child: Text('Submit Feedback'),
),
RaisedButton(
color: Colors.lightBlueAccent,
textColor: Colors.black,
SizedBox(
height: 5,
),
MaterialButton(
elevation: 5,
color: Colors.blue,
textColor: Colors.white,
onPressed: () {
Navigator.push(
context,
Expand Down