Created
August 29, 2024 23:03
-
-
Save Lxxyx/c634f1b4d5f68557128bd722400111f2 to your computer and use it in GitHub Desktop.
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(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Login Page', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
colorSchemeSeed: Colors.blue, | |
), | |
home: const LoginPage(), | |
); | |
} | |
} | |
class LoginPage extends StatefulWidget { | |
const LoginPage({super.key}); | |
@override | |
State<LoginPage> createState() => _LoginPageState(); | |
} | |
class _LoginPageState extends State<LoginPage> { | |
final _formKey = GlobalKey<FormState>(); | |
final _emailController = TextEditingController(); | |
final _passwordController = TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Login Page'), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(20.0), | |
child: Form( | |
key: _formKey, | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
TextFormField( | |
controller: _emailController, | |
decoration: const InputDecoration( | |
labelText: 'Email', | |
border: OutlineInputBorder(), | |
hintText: 'Enter your email', | |
), | |
validator: (value) { | |
if (value == null || value.isEmpty) { | |
return 'Please enter your email'; | |
} | |
return null; | |
}, | |
), | |
const SizedBox(height: 20), | |
TextFormField( | |
controller: _passwordController, | |
obscureText: true, | |
decoration: const InputDecoration( | |
labelText: 'Password', | |
border: OutlineInputBorder(), | |
hintText: 'Enter your password', | |
), | |
validator: (value) { | |
if (value == null || value.isEmpty) { | |
return 'Please enter your password'; | |
} | |
return null; | |
}, | |
), | |
const SizedBox(height: 20), | |
ElevatedButton( | |
onPressed: () { | |
if (_formKey.currentState!.validate()) { | |
// If the form is valid, then login | |
// You can call your login API or function here | |
// For demo, I'll just print the email and password | |
print('Email: ${_emailController.text}'); | |
print('Password: ${_passwordController.text}'); | |
} else { | |
print('Invalid form'); | |
} | |
}, | |
child: const Text('Login'), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment