Created
July 15, 2025 09:22
-
-
Save Lxxyx/0ff925d2373b3e96c4d5ca219739b90d 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: 'Flutter Demo', | |
| 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(); | |
| bool _obscureText = true; | |
| void _toggleObscureText() { | |
| setState(() { | |
| _obscureText = !_obscureText; | |
| }); | |
| } | |
| void _login() { | |
| if (_formKey.currentState!.validate()) { | |
| // add login logic here | |
| // for this example, we'll just print the values | |
| print('Email: ${_emailController.text}'); | |
| print('Password: ${_passwordController.text}'); | |
| } | |
| } | |
| @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( | |
| children: [ | |
| TextFormField( | |
| controller: _emailController, | |
| decoration: const InputDecoration( | |
| labelText: 'Email', | |
| border: OutlineInputBorder(), | |
| ), | |
| validator: (value) { | |
| if (value == null || value.isEmpty) { | |
| return 'Please enter an email'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox(height: 20), | |
| TextFormField( | |
| controller: _passwordController, | |
| decoration: InputDecoration( | |
| labelText: 'Password', | |
| border: const OutlineInputBorder(), | |
| suffixIcon: IconButton( | |
| icon: _obscureText ? const Icon(Icons.visibility_off) : const Icon(Icons.visibility), | |
| onPressed: _toggleObscureText, | |
| ), | |
| ), | |
| obscureText: _obscureText, | |
| validator: (value) { | |
| if (value == null || value.isEmpty) { | |
| return 'Please enter a password'; | |
| } | |
| if (value.length < 8) { | |
| return 'Please enter a password with at least 8 characters'; | |
| } | |
| return null; | |
| }, | |
| ), | |
| const SizedBox(height: 20), | |
| ElevatedButton( | |
| onPressed: _login, | |
| child: const Text('Login'), | |
| ), | |
| ], | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment