Created
July 11, 2019 21:34
-
-
Save iamranchojr/2ea666ed6b8c87eff9343ac9eb3e70a5 to your computer and use it in GitHub Desktop.
Flutter Age Validator App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main(){ | |
runApp(MyApp()); | |
} | |
// Hello world with material design | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Age Validator App', | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text("Age Validator"), | |
), | |
body: Age() | |
) | |
); | |
} | |
} | |
class Age extends StatefulWidget { | |
@override | |
_AgeState createState() { | |
return _AgeState(); | |
} | |
} | |
class _AgeState extends State<Age> { | |
String _message = ""; | |
String _name; | |
int _age; | |
void nameOnSubmitted(String value) { | |
_name = value; | |
} | |
void ageOnSubmitted(String value) { | |
try { | |
_age = int.parse(value); | |
} on FormatException catch(ex) { | |
setState(() { | |
_message = "Please enter a valid age"; | |
}); | |
} | |
} | |
void enterClub() { | |
setState(() { | |
if (_age >= 18) { | |
_message = "Hello $_name, You are allowed entry"; | |
} | |
else { | |
_message = "Entry denied"; | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
padding: EdgeInsets.all(16), | |
child: Column( | |
children: <Widget>[ | |
Text(_message, style: TextStyle(fontSize: 20)), | |
TextField( | |
decoration: InputDecoration( | |
hintText: "Name" | |
), | |
onChanged: nameOnSubmitted, | |
), | |
TextField( | |
decoration: InputDecoration( | |
hintText: "Age" | |
), | |
onChanged: ageOnSubmitted, | |
), | |
RaisedButton( | |
child: Text("Enter", style: TextStyle( | |
fontSize: 20 | |
),), | |
onPressed: enterClub, | |
) | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment