Untitled Document
Untitled Document
✅
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
yaml
CopyEdit
dependencies:
flutter:
sdk: flutter
http: ^0.13.5
provider: ^6.0.5
Then run:
sh
CopyEdit
flutter pub get
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");
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");
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");
if (response.statusCode == 201) {
return "Blood request sent successfully!";
} else {
return "Failed to request blood.";
}
}
}
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")),
],
),
),
);
}
}
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';
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"),
),
],
),
),
);
}
}
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