pparth602

home_page.dart

Apr 25th, 2021
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 7.88 KB | None | 0 0
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
  4. import 'package:learnship/pages/courselist_page/courselist_page.dart';
  5. import 'package:learnship/pages/home_page/search_page.dart';
  6. import 'package:youtube_api/youtube_api.dart';
  7.  
  8. class HomePage extends StatefulWidget {
  9.   HomePage({Key key}) : super(key: key);
  10.  
  11.   @override
  12.   _HomePageState createState() => _HomePageState();
  13. }
  14.  
  15. class _HomePageState extends State<HomePage> {
  16.   static String key = "AIzaSyB5Spz0ROPoQ0J1HsorKOldqIsEeshMaSk";
  17.   TextEditingController searchController = TextEditingController();
  18.  
  19.   YoutubeAPI ytApi = YoutubeAPI(key);
  20.   List<YT_API> ytResult = [];
  21.  
  22.   callAPI() async {
  23.     ytResult = await ytApi.search(searchController.text);
  24.     // ytResult = await ytApi.nextPage();
  25.     // ytResult = await ytApi.nextPage();
  26.     setState(() {});
  27.   }
  28.  
  29.   @override
  30.   void initState() {
  31.     super.initState();
  32.     //callAPI();
  33.     print('hello');
  34.   }
  35.  
  36.   List<String> imageList = [
  37.     'assets/course_image/Data Structures.jpg',
  38.     'assets/course_image/database_1.jpg',
  39.     'assets/course_image/Database.jpg',
  40.     'assets/course_image/flutter.jpg',
  41.     'assets/course_image/ML.jpg'
  42.   ];
  43.  
  44.   final List<dynamic> _videoUrls = [];
  45.  
  46.   @override
  47.   Widget build(BuildContext context) {
  48.     Size size = MediaQuery.of(context).size;
  49.     return Scaffold(
  50.       backgroundColor: Color(0xFFF8F8F8),
  51.       body: Column(
  52.         children: [
  53.           Row(
  54.             mainAxisAlignment: MainAxisAlignment.spaceBetween,
  55.             children: [
  56.               Spacer(
  57.                 flex: 1,
  58.               ),
  59.               Text(
  60.                 'Hi, Parth!',
  61.                 style: TextStyle(
  62.                   fontSize: size.height * 0.04,
  63.                   fontWeight: FontWeight.bold,
  64.                 ),
  65.               ),
  66.               Spacer(
  67.                 flex: 9,
  68.               ),
  69.               CircleAvatar(
  70.                 radius: size.height * 0.04,
  71.                 backgroundImage: AssetImage('assets/course_image/Parth.jpg'),
  72.               ),
  73.               Spacer(
  74.                 flex: 2,
  75.               ),
  76.             ],
  77.           ), // Header
  78.           Container(
  79.             padding: EdgeInsets.all(10),
  80.             //  padding: EdgeInsets.only(top: 8, bottom: 8),
  81.             margin: EdgeInsets.only(left: 8, right: 8),
  82.             child: Material(
  83.               elevation: 10.0,
  84.               borderRadius: BorderRadius.all(Radius.circular(30.0)),
  85.               child: TextField(
  86.                 controller: searchController,
  87.                 style: TextStyle(color: Colors.black, fontSize: 22.0),
  88.                 cursorColor: Theme.of(context).primaryColor,
  89.                 decoration: InputDecoration(
  90.                     contentPadding:
  91.                         EdgeInsets.symmetric(horizontal: 32.0, vertical: 14.0),
  92.                     prefixIcon: InkWell(
  93.                       onTap: () {
  94.                         Navigator.push(
  95.                           context,
  96.                           MaterialPageRoute(
  97.                             builder: (context) => SearchPage(
  98.                               searchQuery: searchController.text,
  99.                               ytApi: ytApi,
  100.                             ),
  101.                           ),
  102.                         );
  103.                       },
  104.                       child: Icon(
  105.                         Icons.search,
  106.                         color: Colors.black,
  107.                       ),
  108.                     ),
  109.                     border: InputBorder.none,
  110.                     hintText: "Search"),
  111.               ),
  112.             ),
  113.           ), //Search Bar
  114.           _space,
  115.           Expanded(
  116.             child: Container(
  117.               margin: EdgeInsets.all(12),
  118.               child: StreamBuilder(
  119.                   stream: FirebaseFirestore.instance
  120.                       .collection("courses")
  121.                       .snapshots(),
  122.                   builder: (BuildContext context,
  123.                       AsyncSnapshot<QuerySnapshot> snapshot) {
  124.                     return !snapshot.hasData
  125.                         ? Center(
  126.                             child: CircularProgressIndicator(),
  127.                           )
  128.                         : StaggeredGridView.countBuilder(
  129.                             crossAxisCount: 2,
  130.                             shrinkWrap: true,
  131.                             crossAxisSpacing: 12,
  132.                             mainAxisSpacing: 12,
  133.                             itemCount: snapshot.data.docs.length,
  134.                             itemBuilder: (context, index) {
  135.                               DocumentSnapshot courses =
  136.                                   snapshot.data.docs[index];
  137.                               List<dynamic> videoUrls = _videoUrls
  138.                                   .map<String>((e) => e.toString())
  139.                                   .toList();
  140.  
  141.                               return Column(
  142.                                 children: [
  143.                                   Expanded(
  144.                                     child: GestureDetector(
  145.                                       onTap: () {
  146.                                         Navigator.push(
  147.                                           context,
  148.                                           MaterialPageRoute(
  149.                                             builder: (context) =>
  150.                                                 CourseListPage(
  151.                                               courses: courses,
  152.                                               videoUrls: videoUrls,
  153.                                             ),
  154.                                           ),
  155.                                         );
  156.                                       },
  157.                                       child: ClipRRect(
  158.                                         borderRadius: BorderRadius.all(
  159.                                             Radius.circular(12)),
  160.                                         child: Image.network(courses['imgUrl'],
  161.                                             fit: BoxFit.cover,
  162.                                             height: size.height * 0.3),
  163.                                       ),
  164.                                     ),
  165.                                   ),
  166.                                   Row(
  167.                                     mainAxisSize: MainAxisSize.max,
  168.                                     mainAxisAlignment:
  169.                                         MainAxisAlignment.spaceBetween,
  170.                                     children: [
  171.                                       SizedBox(
  172.                                         width: 3,
  173.                                       ),
  174.                                       Expanded(
  175.                                         child: Text(
  176.                                           courses['courseTitle'],
  177.                                           style: TextStyle(
  178.                                               fontWeight: FontWeight.bold),
  179.                                           softWrap: true,
  180.                                           maxLines: 3,
  181.                                           overflow: TextOverflow.ellipsis,
  182.                                         ),
  183.                                       ),
  184.                                     ],
  185.                                   ),
  186.                                 ],
  187.                               );
  188.                             },
  189.                             staggeredTileBuilder: (index) {
  190.                               return new StaggeredTile.count(
  191.                                   1, index.isEven ? 1.2 : 1.8);
  192.                             },
  193.                           );
  194.                   }),
  195.             ),
  196.           ),
  197.         ],
  198.       ),
  199.     );
  200.   }
  201.  
  202.   Widget get _space => const SizedBox(height: 10);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment