Skip to content

Instantly share code, notes, and snippets.

@hongsw
Last active May 17, 2023 00:14
Show Gist options
  • Save hongsw/e3c4ab57d6be22d94f84ed9cd7a02b85 to your computer and use it in GitHub Desktop.
Save hongsw/e3c4ab57d6be22d94f84ed9cd7a02b85 to your computer and use it in GitHub Desktop.
vagrant-diamond-9153

vagrant-diamond-9153

Created with <3 with dartpad.dev.

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