Skip to content

Instantly share code, notes, and snippets.

@akash-bisariya
Created December 18, 2022 14:59
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