heart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LoginPage(),
);
}
}
class LoginPage extends StatelessWidget {
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login Page'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Login',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
// Email TextField
TextFormField(
controller: emailController,
decoration: const InputDecoration(
hintText: 'Enter your email',
labelText: 'Email',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
} else if (!RegExp(r'^[^@]+@[^@]+.[^@]+').hasMatch(value)) {
return 'Please enter a valid email address';
}
return null;
},
),
const SizedBox(height: 20),
// Password TextField
TextFormField(
controller: passwordController,
decoration: const InputDecoration(
hintText: 'Enter your password',
labelText: 'Password',
border: OutlineInputBorder(),
),
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
} else if (value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
),
const SizedBox(height: 30),
// Login Button
Center(
child: ElevatedButton(
onPressed: () {
String email = emailController.text;
String password = passwordController.text;
if (email.isEmpty || password.isEmpty) {
print('Please fill all fields');
} else {
print('Logged in with email: $email');
}
},
child: const Text('Login'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 32.0,
vertical: 12.0,
),
textStyle: const TextStyle(fontSize: 18),
),
),
),
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LoginPage(),
);
}
}
class LoginPage extends StatelessWidget {
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login Page'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Login',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
// Email TextField
TextFormField(
controller: emailController,
decoration: const InputDecoration(
hintText: 'Enter your email',
labelText: 'Email',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
} else if (!RegExp(r'^[^@]+@[^@]+.[^@]+').hasMatch(value)) {
return 'Please enter a valid email address';
}
return null;
},
),
const SizedBox(height: 20),
// Password TextField
TextFormField(
controller: passwordController,
decoration: const InputDecoration(
hintText: 'Enter your password',
labelText: 'Password',
border: OutlineInputBorder(),
),
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
} else if (value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
),
const SizedBox(height: 30),
// Login Button
Center(
child: ElevatedButton(
onPressed: () {
String email = emailController.text;
String password = passwordController.text;
if (email.isEmpty || password.isEmpty) {
print('Please fill all fields');
} else {
print('Logged in with email: $email');
}
},
child: const Text('Login'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 32.0,
vertical: 12.0,
),
textStyle: const TextStyle(fontSize: 18),
),
),
),
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'FirstScreen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FirstScreen(), // Set FirstScreen as the starting screen
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
const MyHomePage({Key? key, required this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: const Center(
child: Text(
'Welcome to MyHomePage!',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
),
);
}
}
Maindart file
import 'package:flutter/material.dart';
import 'main.dart'; // Import the main.dart file to access MyHomePage
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("First Screen")),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return const MyHomePage(title: 'Welcome');
}),
);
},
child: const Text('Click Here'),
),
),
);
}
}
prac 6
main dart
import 'package:flutter/material.dart';
import 'SecondScreen.dart'; // Import the second screen
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Passing Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FirstScreen(), // Set FirstScreen as the starting screen
);
}
}
class FirstScreen extends StatelessWidget {
FirstScreen({Key? key}) : super(key: key);
final TextEditingController nameController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('First Screen')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: nameController,
decoration: const InputDecoration(
hintText: 'Enter text here',
),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return SecondScreen(nameController.text);
}),
);
},
child: const Text('Click Here'),
),
],
),
),
);
}
}
Prac 8
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',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<StatefulWidget> createState() {
return PopUpMenu();
}
}
class PopUpMenu extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom Popup Menu'),
actions: [
PopupMenuButton(
itemBuilder: (context) => [
PopupMenuItem(
child: Row(
children: [
Icon(Icons.favorite, color: Colors.red),
SizedBox(width: 10),
Text('Favorites')
],
),
),
PopupMenuItem(
child: Row(
children: [
Icon(Icons.edit, color: Colors.blue),
SizedBox(width: 10),
Text('Edit Profile')
],
),
),
PopupMenuItem(
child: Row(
children: [
Icon(Icons.help_outline, color: Colors.orange),
SizedBox(width: 10),
Text('Help & Support')
],
),
),
PopupMenuItem(
child: Row(
children: [
Icon(Icons.logout, color: Colors.black),
SizedBox(width: 10),
Text('Logout')
],
),
),
],
child: Padding(
padding: EdgeInsets.only(right: 10),
child: Icon(Icons.more_vert, color: Colors.white),
),
),
],
),
body: Center(
child: Text(
'Tap the menu icon in the top-right corner!',
style: TextStyle(fontSize: 18),
),
),
);
}
}
Prac 8 : AlertDialog
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(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<StatefulWidget> createState() {
return _MyHomePage();
}
}
class _MyHomePage extends State<MyHomePage> {
void _showExitConfirmationDialog() {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Row(
children: [
Icon(Icons.warning, color: Colors.red),
SizedBox(width: 10),
Text('Exit Confirmation'),
],
),
content: Text('Are you sure you want to exit the app?'),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context); // Close dialog
},
child: Text('No', style: TextStyle(color: Colors.green)),
),
TextButton(
onPressed: () {
Navigator.pop(context); // Close dialog
_exitApp(); // Call exit function
},
child: Text('Yes', style: TextStyle(color: Colors.red)),
),
],
);
},
);
}
void _exitApp() {
// Perform an action like closing the app (simulated here with a message)
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Exiting App...'),
duration: Duration(seconds: 2),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Confirmation Dialog Example'),
),
body: Center(
child: ElevatedButton(
onPressed: _showExitConfirmationDialog,
child: Text('Exit App'),
),
),
);
}
}