Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vijayinyoutube
Created March 30, 2020 14:58
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 vijayinyoutube/8066018f94a75ffb546cc2bc1c3bdbd8 to your computer and use it in GitHub Desktop.
Save vijayinyoutube/8066018f94a75ffb546cc2bc1c3bdbd8 to your computer and use it in GitHub Desktop.
Form Validation in flutter
import 'package:flutter/material.dart';
import 'package:medium/page1.dart';
void main() => runApp(MyApp());
var _formKey = GlobalKey<FormState>();
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyPage(),
);
}
}
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Scaffold(
body: Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RichText(
text: TextSpan(
text: "Hello!",
style: TextStyle(
color: Color(0xFF2508FF),
fontWeight: FontWeight.bold,
fontSize: 35),
children: <TextSpan>[
TextSpan(
text: "Login",
style: TextStyle(
color: Color(0xFF14C115),
))
]),
),
SizedBox(height: 25),
TextFormField(
decoration: InputDecoration(
labelText: "Name",
hintStyle:
TextStyle(color: Colors.grey, fontSize: 16.0),
),
validator: (String name) {
if (name.isEmpty) {
return "Please enter your name";
} else if (name.length > 30 || name.length < 2) {
return "Please enter your name correctly";
}
},
),
SizedBox(height: 25),
TextFormField(
decoration: InputDecoration(
labelText: "Email",
hintStyle:
TextStyle(color: Colors.grey, fontSize: 16.0),
),
validator: (String email) {
if (email.isEmpty) {
return "Please enter your email";
} else if (!email.contains("@") ||
!email.endsWith(".com")) {
return "Please enter your email correctly";
}
},
),
SizedBox(height: 25),
buildButtonContainer(),
SizedBox(height: 25),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Don't have an account?",
style: TextStyle(color: Color(0xFF2508FF))),
FlatButton(
child: Text("Signup",
style: TextStyle(color: Color(0xFF14C115))),
onPressed: () {},
),
],
)
]),
))),
);
}
Widget buildButtonContainer() {
return Container(
height: 56.0,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(23.0),
gradient: LinearGradient(colors: [
Color(0xFF2508FF),
Color(0xFF2585A9),
Color(0xFF14C115),
Color(0xFF14C115),
], begin: Alignment.centerRight, end: Alignment.centerLeft),
),
child: Center(
child: Container(
height: 100.0,
width: 500,
child: FlatButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(23.0)),
child: Text(
"Sign in",
style: TextStyle(color: Colors.white, fontSize: 18.0),
),
onPressed: () {
if (_formKey.currentState.validate()) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Page1()));
}
},
),
),
),
);
}
}
import 'package:flutter/material.dart';
class Page1 extends StatefulWidget {
@override
_Page1State createState() => _Page1State();
}
class _Page1State extends State<Page1> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Text("Welcome"),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment