0% found this document useful (0 votes)
17 views8 pages

Untitled Document

The document outlines the development of a Flutter frontend application that integrates with a Node.js backend, featuring user authentication, a blood request system, and SOS emergency alerts. It includes detailed instructions for setting up the file structure, installing dependencies, and creating essential screens for user registration, login, and home navigation. The app is designed to be mobile-friendly and supports JWT authentication for secure user interactions.

Uploaded by

apurva.kondekar6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

Untitled Document

The document outlines the development of a Flutter frontend application that integrates with a Node.js backend, featuring user authentication, a blood request system, and SOS emergency alerts. It includes detailed instructions for setting up the file structure, installing dependencies, and creating essential screens for user registration, login, and home navigation. The app is designed to be mobile-friendly and supports JWT authentication for secure user interactions.

Uploaded by

apurva.kondekar6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Since you are using Flutter for the frontend and Node.

js + Express for the backend, I’ll


provide you with a Flutter frontend that works with your backend.


This Flutter app will include:​


User Authentication (Register & Login)​


Blood Request System (Request & Track Blood Requests)​


SOS Emergency Alerts (Send Emergency Notifications)​
Donor Availability Status

📂 File Structure for the Flutter App


plaintext
CopyEdit
blood_donation_app/
│── lib/
│ ├── main.dart
│ ├── screens/
│ │ ├── login_screen.dart
│ │ ├── register_screen.dart
│ │ ├── home_screen.dart
│ │ ├── request_blood_screen.dart
│ │ ├── sos_screen.dart
│ ├── services/
│ │ ├── api_service.dart
│── pubspec.yaml

1️⃣ Install Required Dependencies


In your Flutter project, edit pubspec.yaml and add:

yaml
CopyEdit
dependencies:
flutter:
sdk: flutter
http: ^0.13.5
provider: ^6.0.5

Then run:
sh
CopyEdit
flutter pub get

2️⃣ Create api_service.dart for Backend API Calls


📂 lib/services/api_service.dart
dart
CopyEdit
import 'dart:convert';
import 'package:http/http.dart' as http;

class ApiService {
static const String baseUrl = "http://localhost:5000/api";

// User Registration
static Future<String?> registerUser(String name, String email,
String password) async {
final url = Uri.parse("$baseUrl/users/register");

final response = await http.post(


url,
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"name": name,
"email": email,
"password": password,
}),
);

if (response.statusCode == 201) {
return "User registered successfully!";
} else {
return jsonDecode(response.body)["message"];
}
}

// User Login
static Future<String?> loginUser(String email, String password)
async {
final url = Uri.parse("$baseUrl/users/login");

final response = await http.post(


url,
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"email": email,
"password": password,
}),
);

if (response.statusCode == 200) {
final data = jsonDecode(response.body);
return data["token"]; // Return JWT token
} else {
return null;
}
}

// Blood Request
static Future<String?> requestBlood(String token, String
bloodType, String location, String urgency) async {
final url = Uri.parse("$baseUrl/requests/request");

final response = await http.post(


url,
headers: {
"Authorization": "Bearer $token",
"Content-Type": "application/json",
},
body: jsonEncode({
"bloodType": bloodType,
"location": location,
"urgency": urgency,
}),
);

if (response.statusCode == 201) {
return "Blood request sent successfully!";
} else {
return "Failed to request blood.";
}
}
}

3️⃣ Create register_screen.dart


📂 lib/screens/register_screen.dart
dart
CopyEdit
import 'package:flutter/material.dart';
import '../services/api_service.dart';

class RegisterScreen extends StatefulWidget {


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

class _RegisterScreenState extends State<RegisterScreen> {


final TextEditingController nameController =
TextEditingController();
final TextEditingController emailController =
TextEditingController();
final TextEditingController passwordController =
TextEditingController();

void registerUser() async {


String? message = await ApiService.registerUser(
nameController.text,
emailController.text,
passwordController.text,
);

ScaffoldMessenger.of(context).showSnackBar(SnackBar(content:
Text(message ?? "Error")));
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Register")),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
TextField(controller: nameController, decoration:
InputDecoration(labelText: "Name")),
TextField(controller: emailController, decoration:
InputDecoration(labelText: "Email")),
TextField(controller: passwordController, decoration:
InputDecoration(labelText: "Password"), obscureText: true),
SizedBox(height: 20),
ElevatedButton(onPressed: registerUser, child:
Text("Register")),
],
),
),
);
}
}

4️⃣ Create login_screen.dart


📂 lib/screens/login_screen.dart
dart
CopyEdit
import 'package:flutter/material.dart';
import '../services/api_service.dart';
import 'home_screen.dart';

class LoginScreen extends StatefulWidget {


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

class _LoginScreenState extends State<LoginScreen> {


final TextEditingController emailController =
TextEditingController();
final TextEditingController passwordController =
TextEditingController();

void loginUser() async {


String? token = await ApiService.loginUser(
emailController.text,
passwordController.text,
);

if (token != null) {
Navigator.pushReplacement(context, MaterialPageRoute(builder:
(context) => HomeScreen(token: token)));
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content:
Text("Login failed")));
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Login")),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
TextField(controller: emailController, decoration:
InputDecoration(labelText: "Email")),
TextField(controller: passwordController, decoration:
InputDecoration(labelText: "Password"), obscureText: true),
SizedBox(height: 20),
ElevatedButton(onPressed: loginUser, child:
Text("Login")),
],
),
),
);
}
}
5️⃣ Create home_screen.dart
📂 lib/screens/home_screen.dart
dart
CopyEdit
import 'package:flutter/material.dart';
import 'request_blood_screen.dart';
import 'sos_screen.dart';

class HomeScreen extends StatelessWidget {


final String token;

HomeScreen({required this.token});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Home")),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder:
(context) => RequestBloodScreen(token: token)));
},
child: Text("Request Blood"),
),
ElevatedButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder:
(context) => SOSScreen(token: token)));
},
child: Text("Emergency SOS"),
),
],
),
),
);
}
}

6️⃣ Run Your Flutter App


1.​ Start your backend (npm start).
2.​ Start Flutter emulator or connect a device.

Run the Flutter app:​


sh​
CopyEdit​
flutter run

3.​

✅ Summary
✔ Flutter frontend fully integrated with your Node.js backend​
✔ User Registration & Login (JWT authentication)​
✔ Blood Requests & Emergency SOS Support​
✔ Mobile-friendly UI

🚀🔥​
🎯
This provides a working frontend, and you can extend it further!
Let me know if you need any changes!

4o

You might also like