0% found this document useful (0 votes)
2 views

Assignment_no_02[1]app

The document outlines an assignment for developing a Sign-Up and Sign-In system using Flutter without a database. It includes code for user input validation, screen navigation, and storing user credentials using SharedPreferences. The application features a Sign-In screen, a Sign-Up screen, and a Welcome screen after successful authentication.

Uploaded by

beabaigcr7eiqan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment_no_02[1]app

The document outlines an assignment for developing a Sign-Up and Sign-In system using Flutter without a database. It includes code for user input validation, screen navigation, and storing user credentials using SharedPreferences. The application features a Sign-In screen, a Sign-Up screen, and a Welcome screen after successful authentication.

Uploaded by

beabaigcr7eiqan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment no 02

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());
}

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Sign Up & Sign In',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SignInScreen(),
);
}
}

class SignInScreen extends StatefulWidget {


@override
_SignInScreenState createState() => _SignInScreenState();
}

class _SignInScreenState extends State<SignInScreen> {


final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final _formKey = GlobalKey<FormState>();

@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'),
),
],
),
),
),
);
}
}

class SignUpScreen extends StatefulWidget {


@override
_SignUpScreenState createState() => _SignUpScreenState();
}

class _SignUpScreenState extends State<SignUpScreen> {


final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final _confirmPasswordController = TextEditingController();
final _formKey = GlobalKey<FormState>();

@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'),
),
],
),
),
),
);
}
}

class WelcomeScreen extends StatelessWidget {


@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Welcome')),
body: Center(
child: Text('Welcome to the app!', style: TextStyle(fontSize: 24)),
),
);
}
}

Output

You might also like