Created
April 6, 2021 13:09
-
-
Save yeoupooh/59d0ac4e5f21f145ea59bde78b457fac to your computer and use it in GitHub Desktop.
Form example
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'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final appTitle = 'Flutter Form Demo'; | |
return MaterialApp( | |
title: appTitle, | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text(appTitle), | |
), | |
body: MyCustomForm(), | |
), | |
); | |
} | |
} | |
// Create a Form widget. | |
class MyCustomForm extends StatefulWidget { | |
@override | |
MyCustomFormState createState() { | |
return MyCustomFormState(); | |
} | |
} | |
// Create a corresponding State class. This class holds data related to the form. | |
class MyCustomFormState extends State<MyCustomForm> { | |
// Create a global key that uniquely identifies the Form widget | |
// and allows validation of the form. | |
final _formKey = GlobalKey<FormState>(); | |
@override | |
Widget build(BuildContext context) { | |
// Build a Form widget using the _formKey created above. | |
return Form( | |
key: _formKey, | |
child: Padding( | |
padding: EdgeInsets.all(20), | |
child: Wrap( | |
// spacing: 20, // to apply margin in the main axis of the wrap | |
runSpacing: 20, // to apply margin in the cross axis of the wrap | |
// crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
TextFormField( | |
decoration: const InputDecoration( | |
icon: const Icon(Icons.person), | |
hintText: 'Enter your name', | |
labelText: 'Name', | |
), | |
), | |
TextFormField( | |
decoration: const InputDecoration( | |
icon: const Icon(Icons.phone), | |
hintText: 'Enter a phone number', | |
labelText: 'Phone', | |
), | |
), | |
TextFormField( | |
decoration: const InputDecoration( | |
icon: const Icon(Icons.calendar_today), | |
hintText: 'Enter your date of birth', | |
labelText: 'Dob', | |
), | |
), | |
// SizedBox(height: 20), | |
new Container( | |
// padding: const EdgeInsets.only(left: 150.0, top: 40.0), | |
child: new ElevatedButton( | |
child: const Text('Submit'), | |
onPressed: () { | |
print('submit'); | |
}, | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment