Created
June 21, 2020 03:59
-
-
Save DevKhalyd/16b0e9a0e0576b1ed13d398ae7880d16 to your computer and use it in GitHub Desktop.
Validator Text Form Flutter
This file contains 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
class MyHomePage extends StatefulWidget { | |
@override | |
MyHomePageState createState() { | |
return new MyHomePageState(); | |
} | |
} | |
class MyHomePageState extends State<MyHomePage> { | |
final _text = TextEditingController(); | |
bool _validate = false; | |
@override | |
void dispose() { | |
_text.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('TextField Demo'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('Error Showed if Field is Empty on Submit button Pressed'), | |
TextField( | |
controller: _text, | |
decoration: InputDecoration( | |
labelText: 'Enter the Value', | |
errorText: _validate ? 'Value Can\'t Be Empty' : null, | |
), | |
), | |
RaisedButton( | |
onPressed: () { | |
setState(() { | |
_text.text.isEmpty ? _validate = true : _validate = false; | |
}); | |
}, | |
child: Text('Submit'), | |
textColor: Colors.white, | |
color: Colors.blueAccent, | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment