Skip to content

Commit

Permalink
modularization till homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
Abbujaa committed Mar 30, 2024
1 parent 5aa30cc commit 2b41eec
Show file tree
Hide file tree
Showing 31 changed files with 726 additions and 525 deletions.
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:guardiancare/firebase_options.dart';
import 'package:guardiancare/screens/loginpage/loginPage.dart';
import 'package:guardiancare/screens/loginpage/login_page.dart';
import 'package:guardiancare/screens/pages/pages.dart';

void main() async {
Expand All @@ -12,7 +12,7 @@ void main() async {
}

class GuardianCare extends StatefulWidget {
const GuardianCare({Key? key}) : super(key: key);
const GuardianCare({super.key});

@override
State<GuardianCare> createState() => _GuardianCareState();
Expand Down
29 changes: 29 additions & 0 deletions lib/screens/Report/email_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_email_sender/flutter_email_sender.dart';

Future<void> sendReportEmail({
required String selectedIncidentType,
required String location,
required String description,
required String recipientEmail, required BuildContext context,
}) async {
final emailBody = StringBuffer();
emailBody.write('Incident Type: $selectedIncidentType\n');
emailBody.write('Location: $location\n');
emailBody.write('Description: $description\n');

final emailRecipient = recipientEmail.isNotEmpty ? recipientEmail : '[email protected]';
final email = Email(
subject: 'Incident Report',
body: emailBody.toString(),
recipients: [emailRecipient],
);

try {
await FlutterEmailSender.send(email);
// Return a successful result
} catch (error) {
// Return an error result
rethrow;
}
}
46 changes: 46 additions & 0 deletions lib/screens/Report/incident_type_dropdown.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter/material.dart';

class IncidentTypeDropdown extends StatelessWidget {
final String selectedIncidentType;
final ValueChanged<String?> onChanged;

const IncidentTypeDropdown({super.key,
required this.selectedIncidentType,
required this.onChanged,
});

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Select Incident Type *:',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
DropdownButtonFormField<String>(
value: selectedIncidentType,
items: const [
DropdownMenuItem(
value: 'environmental_safety',
child: Text('Environmental Safety'),
),
DropdownMenuItem(
value: 'online_safety',
child: Text('Online Safety'),
),
],
onChanged: onChanged,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Select Incident Type',
),
),
],
);
}
}
155 changes: 0 additions & 155 deletions lib/screens/Report/reportPage.dart

This file was deleted.

104 changes: 104 additions & 0 deletions lib/screens/Report/report_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import 'package:flutter/material.dart';
import 'package:guardiancare/screens/Report/incident_type_dropdown.dart';
import 'package:guardiancare/screens/Report/text_input_field.dart';
import 'package:guardiancare/screens/Report/submit_button.dart';
import 'package:guardiancare/screens/Report/email_utils.dart';

class ReportPage extends StatefulWidget {
const ReportPage({super.key});

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

class _ReportPageState extends State<ReportPage> {
late String _selectedIncidentType = 'environmental_safety';
final TextEditingController _locationController = TextEditingController();
final TextEditingController _descriptionController = TextEditingController();
final TextEditingController _recipientEmailController =
TextEditingController();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Report Incident'),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
IncidentTypeDropdown(
selectedIncidentType: _selectedIncidentType,
onChanged: (value) {
setState(() {
_selectedIncidentType = value!;
});
},
),
const SizedBox(height: 16),
TextInputField(
controller: _locationController,
labelText: 'Location *',
keyboardType: TextInputType.text,
),
const SizedBox(height: 16),
TextInputField(
controller: _descriptionController,
labelText: 'Description *',
keyboardType: TextInputType.multiline,
maxLines: null,
),
const SizedBox(height: 16),
TextInputField(
controller: _recipientEmailController,
labelText: 'Recipient Email(s) (optional)',
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value!.isEmpty) {
return null;
}
final emailRegex = RegExp(r"[^@]+@[^@]+\.[^@]+");
if (!emailRegex.hasMatch(value)) {
return 'Please enter a valid email address.';
}
return null;
},
),
const SizedBox(height: 16),
SubmitButton(
onPressed: () {
_submitReport(context);
},
),
],
),
),
);
}

bool _validateInput() {
return _locationController.text.isNotEmpty &&
_descriptionController.text.isNotEmpty;
}

Future<void> _submitReport(BuildContext context) async {
if (!_validateInput()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Location and Description are required.'),
),
);
return;
}

await sendReportEmail(
selectedIncidentType: _selectedIncidentType,
location: _locationController.text,
description: _descriptionController.text,
recipientEmail: _recipientEmailController.text,
context: context,
);
}
}
15 changes: 15 additions & 0 deletions lib/screens/Report/submit_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter/material.dart';

class SubmitButton extends StatelessWidget {
final VoidCallback onPressed;

const SubmitButton({super.key, required this.onPressed});

@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
child: const Text('Submit'),
);
}
}
Loading

0 comments on commit 2b41eec

Please sign in to comment.