iampawan / iampawan/30DaysOfFlutter

Form Validation Error

Open
#28 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
418
Forks
315
PR merge metrics
No merged PRs in 30d

Description

### After clicking Login button the screen is changing even if the text and password fields are empty

```dart

import 'package:first_app/utils/approutes.dart';
import 'package:flutter/material.dart';

class Login extends StatefulWidget {
@override
_LoginState createState() => _LoginState();
}

class _LoginState extends State {
String name = "";
bool changebtn = false;
final _formkey = GlobalKey();

movetohome(BuildContext context) async {
if (_formkey.currentState!.validate()) {
setState(() {
changebtn = true;
});
}
// push to move to next route

await Future.delayed(Duration(seconds: 1));
await Navigator.pushNamed(context, Approutes.homeRoute);
setState(() {
changebtn = false;
});
}

@override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: SingleChildScrollView(
child: Form(
key: _formkey,
child: Column(
children: [
Image.asset(
"assets/images/top.png",
fit: BoxFit.cover,
),
SizedBox(
height: 80.0,
),
Text(
"Welcome $name",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28),
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 30.0, horizontal: 40.0),
child: Column(
children: [
TextFormField(
onChanged: (value) {
name = value;
setState(() {});
},
style: TextStyle(fontSize: 20),
decoration: InputDecoration(
hintText: "Enter username", labelText: "username"),
validator: (value) {
if (value!.isEmpty) {
return "Username cannot be empty";
}
return null;
},
),
TextFormField(
style: TextStyle(fontSize: 20),
obscureText: true,
decoration: InputDecoration(
hintText: "Enter password", labelText: "password"),
validator: (value) {
if (value!.isEmpty) {
return "Password cannot be empty";
} else if (value.length < 6) {
return "Password length should be atleast 6";
}
return null;
},
),
SizedBox(
height: 50.0,
),
Material(
color: Colors.pinkAccent,
borderRadius: BorderRadius.circular(changebtn ? 60 : 8),
child: InkWell(
splashColor: Colors.yellow,
onTap: () => movetohome(context),
child: AnimatedContainer(
duration: Duration(seconds: 1),
width: changebtn ? 60 : 160,
height: 60,
// styling

alignment: Alignment.center,
child: changebtn
? Icon(Icons.done)
: Text(
"Login",
style: TextStyle(
fontSize: 25,
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
),
),
],
),
),
],
),
),
));
}
}

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.