pparth602

Untitled

Mar 13th, 2021
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.33 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.  
  6. class HomePage extends StatefulWidget {
  7.   @override
  8.   _HomePageState createState() => _HomePageState();
  9. }
  10.  
  11. class _HomePageState extends State<HomePage> {
  12.   String _fileName;
  13.   List<PlatformFile> _paths = [];
  14.   List<String> _listOfFileName = <String>[];
  15.   String _directoryPath;
  16.   String _extension;
  17.   bool _loadingPath = false;
  18.   bool _multiPick = true;
  19.   List<String> _listOfPaths;
  20.   FileType _pickingType = FileType.audio;
  21.  
  22.   void _openFileExplorer() async {
  23.     setState(() => _loadingPath = true);
  24.     try {
  25.       _directoryPath = null;
  26.       _paths = (await FilePicker.platform.pickFiles(
  27.         type: FileType.audio,
  28.         allowMultiple: true,
  29.         allowedExtensions: (_extension?.isNotEmpty ?? false)
  30.             ? _extension?.replaceAll(' ', '')?.split(',')
  31.             : null,
  32.       ))
  33.           ?.files;
  34.     } on PlatformException catch (e) {
  35.       print("Unsupported operation" + e.toString());
  36.     } catch (ex) {
  37.       print(ex);
  38.     }
  39.     if (!mounted) return;
  40.     setState(() {
  41.       _loadingPath = false;
  42.       _fileName = _paths != null ? _paths.map((e) => e.name).toString() : '...';
  43.       // _listOfFileName.add(_fileName);
  44.     });
  45.   }
  46.  
  47.   @override
  48.   Widget build(BuildContext context) {
  49.     return Scaffold(
  50.       appBar: AppBar(
  51.         title: Text("Open Audiobook"),
  52.         actions: <Widget>[
  53.           InkWell(
  54.             onTap: () async {
  55.               setState(() {
  56.                 _openFileExplorer();
  57.               });
  58.             },
  59.             child: Padding(
  60.               padding: const EdgeInsets.all(15.0),
  61.               child: Icon(Icons.add_circle_outline_rounded),
  62.             ),
  63.           ),
  64.         ],
  65.       ),
  66.       body: Center(
  67.         child: ElevatedButton(
  68.           child: Text('Go to Library Page'),
  69.           onPressed: () {
  70.             Navigator.push(
  71.               context,
  72.               MaterialPageRoute(
  73.                 builder: (context) => LibraryPage(
  74.                   listOfFileName: _listOfFileName,
  75.                   paths: _paths,
  76.                 ),
  77.               ),
  78.             );
  79.           },
  80.         ),
  81.       ),
  82.     );
  83.   }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment