pparth602

courselist_page.dart

Apr 28th, 2021
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.44 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<List<String>> getTitles() async {
  29.     final courseItems = widget.courses.get('courseItems');
  30.     final videosUrls = courseItems.map((e) => e.toString()).toList();
  31.     final tutorialId =
  32.         videosUrls.map((e) => getIdFromUrl(e.toString())).toList();
  33.     print(' Tutorials ID : $tutorialId');
  34.     print(' Tutorials ID Type : ${tutorialId.runtimeType}');
  35.  
  36.     final tutorialTitle = await tutorialId
  37.         .map((e) async => await yt.videos.get(e).then((value) {
  38.               //print('tutorialModel to String: ${value.title}');
  39.               videoTitles.add(value.title);
  40.               print('VideoTitle: $videoTitles');
  41.  
  42.               return value.title;
  43.             }))
  44.         .toList();
  45.     print('tutorial Title Length: ${tutorialTitle.length}');
  46.     print('video Title Length: ${videoTitles.length}');
  47.    
  48.     return videoTitles;
  49.   }
  50.  
  51.   @override
  52.   Widget build(BuildContext context) {
  53.     Size size = MediaQuery.of(context).size;
  54.     return SafeArea(
  55.       child: Scaffold(
  56.         body: SingleChildScrollView(
  57.           child: FutureBuilder(
  58.             future: getTitles(),
  59.             builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
  60.               switch (snapshot.connectionState) {
  61.                 case ConnectionState.none:
  62.                   return Center(child: Text('Loading none of data'));
  63.  
  64.                 case ConnectionState.waiting:
  65.                   return Center(child: CircularProgressIndicator());
  66.                 case ConnectionState.active:
  67.                   return Center(child: CircularProgressIndicator());
  68.                   break;
  69.                 case ConnectionState.done:
  70.                   return Container(
  71.                     height: size.height * 0.8,
  72.                     child: ListView.builder(
  73.                       shrinkWrap: true,
  74.                       itemCount: videoUrls.length,
  75.                       itemBuilder: (context, index) {
  76.                         //print(('============>${widget.videoTitles[1]}'));
  77.                         print(' Snapshot Error : ${snapshot.error}');
  78.                         print(' Snapshot Data : ${snapshot.data}');
  79.  
  80.                         return videoUrls.isEmpty
  81.                             ? Center(
  82.                                 child: Text('Error: ${snapshot.error}'),
  83.                               )
  84.                             : ListTile(
  85.                                 title: Text(
  86.                                   '${videoUrls[index]}',
  87.                                   style: TextStyle(
  88.                                       color: Colors.red,
  89.                                       fontWeight: FontWeight.bold),
  90.                                 ),
  91.                                 onTap: () {
  92.                                   Navigator.push(
  93.                                     context,
  94.                                     MaterialPageRoute(builder: (builder) {
  95.                                       return PlayPage(
  96.                                         videoTitle: videoTitles[index],
  97.                                         currentvideoID: videoIds[index],
  98.                                       );
  99.                                     }),
  100.                                   );
  101.                                 },
  102.                               );
  103.                       },
  104.                     ),
  105.                   );
  106.                 default:
  107.                   if (snapshot.hasError)
  108.                     return Text('Error: ${snapshot.error}');
  109.                   else
  110.                     return Text('Result: ${snapshot.data}');
  111.               }
  112.             },
  113.           ),
  114.         ),
  115.       ),
  116.     );
  117.   }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment