Skip to content

Commit

Permalink
Learn Page with Firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
uumair327 committed Mar 13, 2024
1 parent ab107bc commit f4bb420
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 127 deletions.
5 changes: 4 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ android {
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}

buildTypes {
Expand All @@ -64,4 +65,6 @@ flutter {
source '../..'
}

dependencies {}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
16 changes: 16 additions & 0 deletions android/app/google-services.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"project_info": {
"project_number": "331315801686",
"firebase_url": "https://guardiancare-a210f-default-rtdb.asia-southeast1.firebasedatabase.app",
"project_id": "guardiancare-a210f",
"storage_bucket": "guardiancare-a210f.appspot.com"
},
Expand Down Expand Up @@ -29,6 +30,14 @@
"certificate_hash": "9f253fc9da6f895b1d78051273a33370900d1118"
}
},
{
"client_id": "331315801686-dkbnrs9hrrppo95p6ovpk094utqc7ncq.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "com.guardiancare.guardiancare",
"certificate_hash": "f7dba5844f617ff67f814b06709eca1b81fb7e27"
}
},
{
"client_id": "331315801686-f57icrckmccqe3mu1airrgtpvho4lcva.apps.googleusercontent.com",
"client_type": 3
Expand All @@ -45,6 +54,13 @@
{
"client_id": "331315801686-f57icrckmccqe3mu1airrgtpvho4lcva.apps.googleusercontent.com",
"client_type": 3
},
{
"client_id": "331315801686-4h0llgi3p79eam8n5bt5vhm735plbg7h.apps.googleusercontent.com",
"client_type": 2,
"ios_info": {
"bundle_id": "com.guardiancare.guardiancare"
}
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions lib/firebase_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class DefaultFirebaseOptions {
messagingSenderId: '331315801686',
projectId: 'guardiancare-a210f',
authDomain: 'guardiancare-a210f.firebaseapp.com',
databaseURL: 'https://guardiancare-a210f-default-rtdb.asia-southeast1.firebasedatabase.app',
storageBucket: 'guardiancare-a210f.appspot.com',
measurementId: 'G-HRJ3C6VEXP',
);
Expand All @@ -58,6 +59,7 @@ class DefaultFirebaseOptions {
appId: '1:331315801686:android:28c596f9e2e592e17595f7',
messagingSenderId: '331315801686',
projectId: 'guardiancare-a210f',
databaseURL: 'https://guardiancare-a210f-default-rtdb.asia-southeast1.firebasedatabase.app',
storageBucket: 'guardiancare-a210f.appspot.com',
);

Expand All @@ -66,6 +68,7 @@ class DefaultFirebaseOptions {
appId: '1:331315801686:ios:3e6128afd380d53a7595f7',
messagingSenderId: '331315801686',
projectId: 'guardiancare-a210f',
databaseURL: 'https://guardiancare-a210f-default-rtdb.asia-southeast1.firebasedatabase.app',
storageBucket: 'guardiancare-a210f.appspot.com',
androidClientId: '331315801686-4co8knverqologscscu70sip0c3iqqd3.apps.googleusercontent.com',
iosClientId: '331315801686-4h0llgi3p79eam8n5bt5vhm735plbg7h.apps.googleusercontent.com',
Expand All @@ -77,6 +80,7 @@ class DefaultFirebaseOptions {
appId: '1:331315801686:ios:cef1e3afa0308d3d7595f7',
messagingSenderId: '331315801686',
projectId: 'guardiancare-a210f',
databaseURL: 'https://guardiancare-a210f-default-rtdb.asia-southeast1.firebasedatabase.app',
storageBucket: 'guardiancare-a210f.appspot.com',
androidClientId: '331315801686-4co8knverqologscscu70sip0c3iqqd3.apps.googleusercontent.com',
iosClientId: '331315801686-drnf2arr18matomgvs169374mbk0jgud.apps.googleusercontent.com',
Expand Down
17 changes: 17 additions & 0 deletions lib/firestore/video.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:cloud_firestore/cloud_firestore.dart';

Future<List<Map<String, dynamic>>> fetchVideos() async {
try {
FirebaseFirestore db = FirebaseFirestore.instance;
final querySnapshot = await db.collection('videos').get();
List<Map<String, dynamic>> videos = querySnapshot.docs.map((doc) {
Map<String, dynamic> data = doc.data();
data['id'] = doc.id; // Add document ID to the data map
return data;
}).toList();
return videos;
} catch (e) {
print('Error fetching videos: $e');
return [];
}
}
2 changes: 1 addition & 1 deletion lib/screens/homePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class _HomePageState extends State<HomePage> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LearnPage(),
builder: (context) => VideoPage(),
),
);
} else {
Expand Down
207 changes: 98 additions & 109 deletions lib/screens/learn.dart
Original file line number Diff line number Diff line change
@@ -1,135 +1,124 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';

class LearnPage extends StatelessWidget {
const LearnPage({super.key});
class VideoPage extends StatefulWidget {
@override
State<VideoPage> createState() => _VideoPageState();
}

class _VideoPageState extends State<VideoPage> {
final FirebaseAuth _auth = FirebaseAuth.instance;

@override
void initState() {
super.initState();
// Check if the user is authenticated
User? user = _auth.currentUser;
if (user == null) {
// Handle unauthenticated state
} else {
// User is authenticated
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Learn"),
title: const Text('Video List'),
),
body: ListView(
padding: const EdgeInsets.all(16),
children: const [
ContentField(
title: "Road Safety",
theme: "Stay Safe on the Road",
videos: [
"Video 1",
"Video 2",
"Video 3",
// Add more videos as needed
],
),
ContentField(
title: "Child Abuse",
theme: "Protecting Our Children",
videos: [
"Video 1",
"Video 2",
"Video 3",
// Add more videos as needed
],
),
ContentField(
title: "Sexual Harassment",
theme: "Creating Safe Environments",
videos: [
"Video 1",
"Video 2",
"Video 3",
// Add more videos as needed
],
),
// Add more content fields as needed
],
body: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('videos').snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
}
// Extract videos from the snapshot
List<QueryDocumentSnapshot> videos = snapshot.data!.docs;
return ListView.builder(
itemCount: videos.length,
itemBuilder: (context, index) {
var video = videos[index].data() as Map<String, dynamic>;
return ListTile(
title: Text(video['title'] ?? 'Untitled'),
onTap: () {
// Navigate to video player page passing the video URL
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VideoPlayerPage(
videoUrl: video['videoUrl'] as String? ?? '',
),
),
);
},
);
},
);
},
),
);
}
}

class ContentField extends StatelessWidget {
final String title;
final String theme;
final List<String> videos;
class VideoPlayerPage extends StatelessWidget {
final String videoUrl;

const ContentField({
super.key,
required this.title,
required this.theme,
required this.videos,
});
const VideoPlayerPage({Key? key, required this.videoUrl}) : super(key: key);

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VideoPage(videos: videos),
),
);
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.category, color: Colors.blue),
const SizedBox(width: 8),
Text(
title,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
],
),
const SizedBox(height: 4),
Text(
theme,
style: const TextStyle(
color: Colors.grey,
fontStyle: FontStyle.italic,
),
),
const SizedBox(height: 12),
],
),
),
const Divider(), // Add a divider between content fields
],
);
}
}
// Extract video ID from the YouTube URL
String? videoId = YoutubePlayer.convertUrlToId(videoUrl);

class VideoPage extends StatelessWidget {
final List<String> videos;
// Check if the video ID is null
if (videoId == null) {
// Handle the case where the video ID is null
return Scaffold(
appBar: AppBar(
title: const Text('Video Player'),
),
body: const Center(
child: Text('Invalid YouTube URL'),
),
);
}

const VideoPage({Key? key, required this.videos}) : super(key: key);
// Create a YoutubePlayerController
YoutubePlayerController _controller = YoutubePlayerController(
initialVideoId: videoId,
flags: const YoutubePlayerFlags(
autoPlay: true,
mute: false,
),
);

@override
Widget build(BuildContext context) {
// Implement video player here
return Scaffold(
appBar: AppBar(
title: const Text('Video List'),
title: const Text('Video Player'),
),
body: ListView.builder(
itemCount: videos.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(videos[index]),
onTap: () {
// Add functionality to play the video
},
);
},
body: SingleChildScrollView(
child: Column(
children: [
Center(
child: YoutubePlayer(
controller: _controller,
showVideoProgressIndicator: true,
progressIndicatorColor: Colors.blueAccent,
progressColors: const ProgressBarColors(
playedColor: Colors.blue,
handleColor: Colors.blueAccent,
),
),
),
],
),
),
);
}
Expand Down
4 changes: 4 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import FlutterMacOS
import Foundation

import cloud_firestore
import firebase_auth
import firebase_core
import flutter_inappwebview_macos
import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTFirebaseFirestorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseFirestorePlugin"))
FLTFirebaseAuthPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAuthPlugin"))
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
InAppWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "InAppWebViewFlutterPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}
Loading

0 comments on commit f4bb420

Please sign in to comment.