Created with <3 with dartpad.dev.
Last active
May 17, 2023 00:14
-
-
Save hongsw/e3c4ab57d6be22d94f84ed9cd7a02b85 to your computer and use it in GitHub Desktop.
vagrant-diamond-9153
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 UserProfile { | |
String name; | |
String email; | |
UserProfile({required this.name, required this.email}); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Form Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Form Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
final _formKey = GlobalKey<FormState>(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
), | |
body: Form( | |
key: _formKey, | |
child: Column( | |
children: <Widget>[ | |
TextFormField( | |
decoration: const InputDecoration( | |
hintText: 'Enter your email', | |
), | |
validator: (value) { | |
if (value == null || value.isEmpty) { | |
return 'Please enter your email'; | |
} | |
return null; | |
}, | |
onSaved: (value) { | |
// Save the email value | |
}, | |
), | |
TextFormField( | |
decoration: const InputDecoration( | |
hintText: 'Enter your password', | |
), | |
validator: (value) { | |
if (value == null || value.isEmpty) { | |
return 'Please enter your password'; | |
} | |
return null; | |
}, | |
onSaved: (value) { | |
// Save the password value | |
}, | |
), | |
ElevatedButton( | |
onPressed: () { | |
if (_formKey.currentState!.validate()) { | |
_formKey.currentState!.save(); | |
// If the form is valid, display a Snackbar. | |
ScaffoldMessenger.of(context) | |
.showSnackBar(SnackBar(content: Text('Processing Data'))); | |
} | |
}, | |
child: Text('Submit'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment