/FlutterForm Secret
Created
December 18, 2022 14:59
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
import 'package:flutter/material.dart'; | |
import 'package:form/form.dart'; | |
class MyForm extends StatefulWidget { | |
@override | |
_MyFormState createState() => _MyFormState(); | |
} | |
class _MyFormState extends State<MyForm> { | |
final _formKey = GlobalKey<FormState>(); | |
String _inputText; | |
@override | |
Widget build(BuildContext context) { | |
return Form( | |
key: _formKey, | |
child: Column( | |
children: <Widget>[ | |
TextFormField( | |
decoration: InputDecoration(labelText: 'Enter your name'), | |
validator: (value) { | |
if (value.isEmpty) { | |
return 'Please enter your name'; | |
} | |
return null; | |
}, | |
onSaved: (value) => _inputText = value, | |
), | |
RaisedButton( | |
onPressed: () { | |
if (_formKey.currentState.validate()) { | |
_formKey.currentState.save(); | |
Scaffold.of(context).showSnackBar( | |
SnackBar(content: Text('Hello, $_inputText!')) | |
); | |
} | |
}, | |
child: Text('Submit'), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment