pparth602

courselist_page.dart

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