pparth602

courselist_page.dart

Apr 27th, 2021
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.98 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:learnship/pages/play_page/play_page.dart';
  4. import 'package:youtube_explode_dart/youtube_explode_dart.dart';
  5. import 'package:youtube_parser/youtube_parser.dart';
  6.  
  7. class CourseListPage extends StatefulWidget {
  8.   static String routeName = 'courseListPage';
  9.   final DocumentSnapshot courses;
  10.  
  11.   const CourseListPage({Key key, @required this.courses}) : super(key: key);
  12.  
  13.   @override
  14.   _CourseListPageState createState() => _CourseListPageState();
  15. }
  16.  
  17. class _CourseListPageState extends State<CourseListPage> {
  18.   List<String> videoTitles = [];
  19.   List<String> videoUrls = [];
  20.   List<String> videoIds = [];
  21.   final yt = YoutubeExplode();
  22.  
  23.   @override
  24.   void initState() {
  25.     super.initState();
  26.   }
  27.  
  28.   Future getTitles() async {
  29.     final List<String> tutorialsTitles = [];
  30.  
  31.     final courseItems = widget.courses.get('courseItems');
  32.     // print('Inside courseItems: $courseItems');
  33.     // print('Inside courseItems: ${courseItems.runtimeType}'); // List<dynamic>
  34.     final videosUrls = courseItems.map((e) => e.toString()).toList();
  35.     // print('Inside videosUrls: $videosUrls');
  36.     // print(
  37.     //     'Inside videosUrls Runtime: ${videosUrls.runtimeType}'); // List<dynamic>
  38.  
  39.     final tutorialId =
  40.         videosUrls.map((e) => getIdFromUrl(e.toString())).toList();
  41.     // print('Inside the  tutorialId: $tutorialId');
  42.     // print(
  43.     //     'Inside the  tutorialId Runtime Type : ${tutorialId.runtimeType}'); //List<dynamic>
  44.  
  45.     final tutorialTitle = tutorialId
  46.         .map((e) => yt.videos.get(e).then((value) {
  47.               print('tutorialModel to String: ${value.title}');
  48.               tutorialsTitles.add(value.title);
  49.               return value.title;
  50.             }))
  51.         .toList();
  52.     print('Inside tutorialTitle ${tutorialTitle.runtimeType}');
  53.     print('Inside tutorialTitle:  $tutorialTitle');
  54.  
  55.     // for (var u in videoUrls) {
  56.     //   videoUrls.add(u.toString());
  57.     //   print('Inside for loop: $u ');
  58.     //   print('Inside for loop: ${u.runtimeType}');
  59.     // }
  60.     // print(videoUrls.runtimeType);
  61.     // videoUrls = videoUrls.toList(growable: true);
  62.     // videoIds = videoUrls.map((e) => getIdFromUrl(e));
  63.     // print(videoUrls);
  64.     // print(
  65.     //     'Inside the getTitles videoTitles RuntimeType: ${videoTitles.runtimeType}');
  66.     setState(() {});
  67.   }
  68.  
  69.   @override
  70.   Widget build(BuildContext context) {
  71.     Size size = MediaQuery.of(context).size;
  72.     return SafeArea(
  73.       child: Scaffold(
  74.         body: SingleChildScrollView(
  75.           child: FutureBuilder(
  76.             future: getTitles(),
  77.             builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
  78.               switch (snapshot.connectionState) {
  79.                 case ConnectionState.none:
  80.                   return Center(child: Text('Loading none of data'));
  81.  
  82.                 case ConnectionState.waiting:
  83.                 case ConnectionState.active:
  84.                   return Center(child: CircularProgressIndicator());
  85.                   break;
  86.                 case ConnectionState.done:
  87.                   return Container(
  88.                     height: size.height * 0.8,
  89.                     child: ListView.builder(
  90.                       shrinkWrap: true,
  91.                       itemCount: videoUrls.length,
  92.                       itemBuilder: (context, index) {
  93.                         //print(('============>${widget.videoTitles[1]}'));
  94.                         return videoTitles.isEmpty
  95.                             ? Center(
  96.                                 child: CircularProgressIndicator(),
  97.                               )
  98.                             : ListTile(
  99.                                 title: Text(
  100.                                   '${videoTitles[index]}',
  101.                                   style: TextStyle(
  102.                                       color: Colors.red,
  103.                                       fontWeight: FontWeight.bold),
  104.                                 ),
  105.                                 onTap: () {
  106.                                   Navigator.push(
  107.                                     context,
  108.                                     MaterialPageRoute(builder: (builder) {
  109.                                       return PlayPage(
  110.                                         videoTitle: videoTitles[index],
  111.                                         currentvideoID: videoIds[index],
  112.                                       );
  113.                                     }),
  114.                                   );
  115.                                 },
  116.                               );
  117.                       },
  118.                     ),
  119.                   );
  120.                 default:
  121.                   if (snapshot.hasError)
  122.                     return Text('Error: ${snapshot.error}');
  123.                   else
  124.                     return Text('Result: ${snapshot.data}');
  125.               }
  126.             },
  127.           ),
  128.         ),
  129.       ),
  130.     );
  131.   }
  132. }
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment