Last active
June 28, 2019 15:16
-
-
Save leonardarnold/9c284cdc942cea25fc0e4a550115ef70 to your computer and use it in GitHub Desktop.
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()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: MyHomePage(title: 'Flutter Demo Home Page'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| MyHomePage({Key key, this.title}) : super(key: key); | |
| final String title; | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| TextEditingController textEditingController = TextEditingController(); | |
| final GlobalKey<FormState> _formKey = new GlobalKey<FormState>(); | |
| @override | |
| void initState() { | |
| textEditingController.addListener((){ | |
| print("controller text : ${textEditingController.text}"); | |
| _formKey.currentState.validate(); | |
| }); | |
| super.initState(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| body: Form( | |
| key: _formKey, | |
| child: Column( | |
| crossAxisAlignment: CrossAxisAlignment.center, | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| TextFormField( | |
| controller: textEditingController, | |
| textAlign: TextAlign.center, | |
| autofocus: true, | |
| style: Theme.of(context).textTheme.headline, | |
| validator: (value) { | |
| print("validator value : $value"); | |
| if (!value.contains("hello")) { | |
| return 'validation failed'; | |
| } | |
| }, | |
| onSaved: (String value) { | |
| //dismiss keyboard | |
| FocusScope.of(context).requestFocus(new FocusNode()); | |
| textEditingController.text = ""; | |
| }, | |
| ), | |
| SizedBox( | |
| width: 8, | |
| ), | |
| RaisedButton( | |
| child: Text("Save"), | |
| onPressed: () { | |
| if (_formKey.currentState.validate()) { | |
| _formKey.currentState.save(); | |
| } | |
| }, | |
| ) | |
| ], | |
| ), | |
| ) | |
| ); | |
| } | |
| } |
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
| 17:11:48.142 1 info flutter.tools I/flutter (30385): controller text : hel | |
| 17:11:48.150 2 info flutter.tools I/flutter (30385): validator value : he | |
| 17:11:48.423 3 info flutter.tools I/flutter (30385): controller text : hell | |
| 17:11:48.423 4 info flutter.tools I/flutter (30385): validator value : hel | |
| 17:11:49.368 5 info flutter.tools I/flutter (30385): controller text : hello | |
| 17:11:49.368 6 info flutter.tools I/flutter (30385): validator value : hell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment