pparth602

home_page.dart

Mar 14th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.45 KB | None | 0 0
  1. import 'package:audiobook_player/presentation/library_page/library_page.dart';
  2. import 'package:file_picker/file_picker.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:just_audio/just_audio.dart';
  6.  
  7. class HomePage extends StatefulWidget {
  8.   @override
  9.   _HomePageState createState() => _HomePageState();
  10. }
  11.  
  12. class _HomePageState extends State<HomePage> {
  13.   String _fileName;
  14.   List<PlatformFile> _paths = [];
  15.   List<String> _listOfFileName = <String>[];
  16.   String _directoryPath;
  17.   String _extension;
  18.   bool _loadingPath = false;
  19.   bool _multiPick = true;
  20.   List<String> _listOfPaths;
  21.   FileType _pickingType = FileType.audio;
  22.   bool isPlay = false;
  23.   final player = AudioPlayer();
  24.  
  25.   void _openFileExplorer() async {
  26.     setState(() => _loadingPath = true);
  27.     try {
  28.       _directoryPath = null;
  29.       _paths = (await FilePicker.platform.pickFiles(
  30.         type: FileType.audio,
  31.         allowMultiple: true,
  32.         allowedExtensions: (_extension?.isNotEmpty ?? false)
  33.             ? _extension?.replaceAll(' ', '')?.split(',')
  34.             : null,
  35.       ))
  36.           ?.files;
  37.     } on PlatformException catch (e) {
  38.       print("Unsupported operation" + e.toString());
  39.     } catch (ex) {
  40.       print(ex);
  41.     }
  42.     if (!mounted) return;
  43.     setState(() {
  44.       _loadingPath = false;
  45.       _fileName = _paths != null ? _paths.map((e) => e.name).toString() : '...';
  46.       _listOfFileName.add(_fileName);
  47.     });
  48.   }
  49.  
  50.   @override
  51.   Widget build(BuildContext context) {
  52.     Size size = MediaQuery.of(context).size;
  53.     return Scaffold(
  54.       appBar: AppBar(
  55.         title: Text(
  56.           "Open Audiobook Player",
  57.           style: TextStyle(color: Color(0xFF747474)),
  58.         ),
  59.         backgroundColor: Colors.white,
  60.         leading: Icon(
  61.           Icons.arrow_back_outlined,
  62.           color: Color(0xFF757575),
  63.         ),
  64.         actions: <Widget>[
  65.           InkWell(
  66.             child: Icon(
  67.               Icons.local_library_rounded,
  68.               color: Color(0xFF757575),
  69.             ),
  70.             onTap: () {
  71.               Navigator.push(
  72.                 context,
  73.                 MaterialPageRoute(
  74.                   builder: (context) => LibraryPage(
  75.                     paths: _paths,
  76.                     player01: player,
  77.                   ),
  78.                 ),
  79.               );
  80.             },
  81.           ),
  82.           InkWell(
  83.             focusColor: Color(0xFF757575),
  84.             onTap: () async {
  85.               setState(() async {
  86.                 await _openFileExplorer();
  87.                 Navigator.push(
  88.                   context,
  89.                   MaterialPageRoute(
  90.                     builder: (context) => LibraryPage(
  91.                       //listOfFileName: _listOfFileName,
  92.                       paths: _paths,
  93.                       isPlay: false,
  94.                     ),
  95.                   ),
  96.                 );
  97.               });
  98.             },
  99.             child: Padding(
  100.               padding: const EdgeInsets.all(15.0),
  101.               child: Icon(Icons.add_circle_outline_rounded,
  102.                   color: Color(0xFF757575)),
  103.             ),
  104.           ),
  105.         ],
  106.       ),
  107.       body: Column(
  108.         crossAxisAlignment: CrossAxisAlignment.center,
  109.         children: [
  110.           SizedBox(
  111.             height: 15,
  112.           ),
  113.           Row(
  114.             children: [
  115.               SizedBox(width: size.width * 0.05),
  116.               Expanded(
  117.                   child: IconButton(
  118.                       icon: Icon(Icons.alarm_on_rounded), onPressed: () {})),
  119.               //SizedBox(width: size.width * 0.5),
  120.               Expanded(
  121.                 child: IconButton(
  122.                   icon: Icon(Icons.volume_up),
  123.                   onPressed: () {},
  124.                 ),
  125.               ),
  126.               Expanded(
  127.                   child: IconButton(
  128.                 icon: Icon(Icons.equalizer_rounded),
  129.                 onPressed: () {},
  130.               )),
  131.               Expanded(
  132.                 child: IconButton(
  133.                   icon: Icon(Icons.bookmark),
  134.                   onPressed: () {},
  135.                 ),
  136.               ),
  137.               Expanded(
  138.                 child: IconButton(
  139.                   icon: Icon(Icons.lock_open),
  140.                   onPressed: () {},
  141.                 ),
  142.               ),
  143.               SizedBox(width: 15),
  144.             ],
  145.           ),
  146.           Expanded(
  147.             child: Align(
  148.               alignment: Alignment.center,
  149.               child: Row(
  150.                 crossAxisAlignment: CrossAxisAlignment.center,
  151.                 mainAxisAlignment: MainAxisAlignment.center,
  152.                 children: [
  153.                   Expanded(
  154.                     child: Container(
  155.                       alignment: Alignment.center,
  156.                       child: IconButton(
  157.                         iconSize: size.width * 0.22,
  158.                         icon: Icon(
  159.                           Icons.skip_previous_outlined,
  160.                         ),
  161.                         onPressed: () {},
  162.                       ),
  163.                     ),
  164.                   ),
  165.                   Expanded(
  166.                     child: Container(
  167.                       alignment: Alignment.center,
  168.                       child: IconButton(
  169.                           iconSize: size.width * 0.22,
  170.                           icon: isPlay
  171.                               ? Icon(Icons.pause_sharp)
  172.                               : Icon(Icons.play_arrow_sharp),
  173.                           onPressed: () async {
  174.                             setState(() {
  175.                               if (isPlay == true) {
  176.                                 player.pause();
  177.                                 isPlay = false;
  178.                               } else {
  179.                                 player.play();
  180.                                 isPlay = true;
  181.                               }
  182.                             });
  183.                           }),
  184.                     ),
  185.                   ),
  186.                   Expanded(
  187.                     child: Container(
  188.                       alignment: Alignment.center,
  189.                       child: IconButton(
  190.                         iconSize: size.width * 0.22,
  191.                         icon: Icon(Icons.skip_next_outlined),
  192.                         onPressed: () {},
  193.                       ),
  194.                     ),
  195.                   ),
  196.                 ],
  197.               ),
  198.             ),
  199.           ),
  200.         ],
  201.       ),
  202.     );
  203.   }
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment