Assignment_no_02[1]app
Assignment_no_02[1]app
Submitted By:
Abdul Rehman
Submitted To:
Ms. Rimsha Naheed
Course Title:
Mobile Application Development
Registration no:
FA22-BCS-132-C
Date:
10-April-2025
CUI(Sahiwal Campus)
Question No#1
Develop a fully functional Sign-Up and Sign-In system using Flutter,
implementing input validation and screen navigation without using a
database.
Answer:
Code:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(MyApp());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Sign In")),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
controller: _emailController,
decoration: InputDecoration(labelText: 'Email'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'\S+@\S+\.\S+').hasMatch(value)) {
return 'Please enter a valid email';
}
return null;
},
),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
if (_formKey.currentState?.validate() ?? false) {
SharedPreferences prefs = await
SharedPreferences.getInstance();
String storedEmail = prefs.getString('email') ?? '';
String storedPassword = prefs.getString('password') ??
'';
if (_emailController.text == storedEmail &&
_passwordController.text == storedPassword) {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) =>
WelcomeScreen()),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Invalid
credentials')));
}
}
},
child: Text('Sign In'),
),
TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SignUpScreen()),
);
},
child: Text('Don\'t have an account? Sign Up'),
),
],
),
),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Sign Up")),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
controller: _nameController,
decoration: InputDecoration(labelText: 'Full Name'),
validator: (value) {
if (value == null || value.isEmpty || value.length < 3) {
return 'Please enter a valid name (at least 3
characters)';
}
return null;
},
),
TextFormField(
controller: _emailController,
decoration: InputDecoration(labelText: 'Email'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'\S+@\S+\.\S+').hasMatch(value)) {
return 'Please enter a valid email';
}
return null;
},
),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty || value.length < 6 ||
!RegExp(r'\d').hasMatch(value)) {
return 'Password must be at least 6 characters and
contain a number';
}
return null;
},
),
TextFormField(
controller: _confirmPasswordController,
decoration: InputDecoration(labelText: 'Confirm Password'),
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty || value !=
_passwordController.text) {
return 'Passwords must match';
}
return null;
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
if (_formKey.currentState?.validate() ?? false) {
SharedPreferences prefs = await
SharedPreferences.getInstance();
prefs.setString('email', _emailController.text);
prefs.setString('password', _passwordController.text);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Sign Up
Successful')));
Navigator.pop(context);
}
},
child: Text('Sign Up'),
),
],
),
),
),
);
}
}
Output