Screen 4 :
(Broadcast.dart)
Screen Description :
Code :-
import ‘package:flutter/material.dart’;
import ‘package:cloud_firestore/cloud_firestore.dart’;
import ‘package:firebase_auth/firebase_auth.dart’;
class _BroadcastState extends State<Broadcast> {
FirebaseAuth _auth = FirebaseAuth.instance;
String message = ”;
bool checkbox1Value = false;
bool checkbox2Value = false;
bool checkbox3Value = false;
// Checkbox handling functions
void _handleCheckbox1(bool? value) {
if (value != null) {
setState(() {
checkbox1Value = value;
});
}
}
void _handleCheckbox2(bool? value) {
if (value != null) {
setState(() {
checkbox1Value = value;
});
}
}
void _handleCheckbox3(bool? value) {
if (value != null) {
setState(() {
checkbox1Value = value;
});
}
}
// Function to save details in Firebase Firestore
void _saveDetailsInFirebase() async {
try {
// Get the current user
User? user = _auth.currentUser;
if (user != null) {
// Save the details to Firestore
await FirebaseFirestore.instance.collection(‘test_data’).add({
‘userId’: user.uid,
‘message’: message,
‘checkbox1Value’: checkbox1Value,
‘checkbox2Value’: checkbox2Value,
‘checkbox3Value’: checkbox3Value,
});
// Show a success message
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(‘Success’),
content: Text(‘Data saved to Firestore.’),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(‘OK’),
),
],
),
);
} else {
// If the user is not logged in
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(‘Error’),
content: Text(‘User not logged in.’),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(‘OK’),
),
],
),
);
}
} catch (e) {
// Show an error message if something goes wrong
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(‘Error’),
content: Text(‘Error saving data to Firestore.’),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(‘OK’),
),
],
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘BROADCAST’),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Text input field for message
TextFormField(
onChanged: (value) {
setState(() {
message = value;
});
},
decoration: InputDecoration(
labelText: ‘Enter Message’,
),
),
SizedBox(height: 20),
// Implement logic for image attachment here (not implemented in this code)
SizedBox(height: 20),
// Checkboxes for message delivery options
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Checkbox(
value: checkbox1Value,
onChanged: _handleCheckbox1,
),
Text(‘Whatsapp’),
SizedBox(width: 20),
Row(
children: [
Checkbox(
value: checkbox2Value,
onChanged: _handleCheckbox2,
),
Text(‘SMS’),
],
),
Row(
children: [
Checkbox(
value: checkbox3Value,
onChanged: _handleCheckbox3,
),
Text(‘EMAIL’),
],
),
],
),
SizedBox(height: 20),
// Button to send the broadcast message and save data to Firestore
ElevatedButton(
onPressed: _saveDetailsInFirebase,
child: Text(‘BROADCAST’),
),
],
),
),
);
}
}
class Broadcast extends StatefulWidget {
@override
_BroadcastState createState() => _BroadcastState();
}
Code Description :
The broadcast.dart file represents a screen where users can compose and send broadcast messages with various options.
- The Broadcast class is a StatefulWidget representing the broadcast message screenThe _BroadcastState class represents the state of the broadcast message screen.
It includes the following components:
- App Bar: The app bar at the top of the screen with the title “BROADCAST.”
- Body: The body of the screen contains the following components:
- Text Input Field for Message: Users can enter the broadcast message in this text field. The entered text is stored in the message variable.
- Image Attachment: Placeholder for implementing image attachment logic (not implemented in this code).
- Checkboxes for Message Delivery Options: Users can select delivery options for the broadcast message, including WhatsApp, SMS, and Email. The selected options are stored in checkbox1Value, checkbox2Value, and checkbox3Value.
- “BROADCAST” Button: When pressed, it triggers the _saveDetailsInFirebase function to save the broadcast message details in Firebase Firestore.
- Function _saveDetailsInFirebase: This function handles the saving of broadcast message details in Firebase Firestore. It performs the following steps:
- Checks if the user is logged in using Firebase Authentication.
- If the user is logged in, it saves the message and selected delivery options in Firestore.
- Displays success or error messages in response to the Firestore operation.
Audit :