pparth602

Untitled

Sep 27th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 10.24 KB | None | 0 0
  1. import 'package:assets_audio_player/assets_audio_player.dart';
  2. import 'package:audioplayers/audioplayers.dart';
  3. import 'package:flutter/material.dart';
  4.  
  5. import 'package:flutter/services.dart';
  6. import 'package:file_picker/file_picker.dart';
  7.  
  8. class FilePickerDemo extends StatefulWidget {
  9.   @override
  10.   _FilePickerDemoState createState() => _FilePickerDemoState();
  11. }
  12.  
  13. class _FilePickerDemoState extends State<FilePickerDemo> {
  14.   final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  15.   String _fileName;
  16.   List<PlatformFile> _paths;
  17.   String _directoryPath;
  18.   String _extension;
  19.   bool _loadingPath = false;
  20.   bool _multiPick = false;
  21.   FileType _pickingType = FileType.any;
  22.   TextEditingController _controller = TextEditingController();
  23.  
  24.   @override
  25.   void initState() {
  26.     super.initState();
  27.     _controller.addListener(() => _extension = _controller.text);
  28.   }
  29.  
  30.   void _openFileExplorer() async {
  31.     setState(() => _loadingPath = true);
  32.     try {
  33.       _directoryPath = null;
  34.       _paths = (await FilePicker.platform.pickFiles(
  35.         type: _pickingType,
  36.         allowMultiple: _multiPick,
  37.         allowedExtensions: (_extension?.isNotEmpty ?? false)
  38.             ? _extension?.replaceAll(' ', '')?.split(',')
  39.             : null,
  40.       ))
  41.           ?.files;
  42.     } on PlatformException catch (e) {
  43.       print("Unsupported operation" + e.toString());
  44.     } catch (ex) {
  45.       print(ex);
  46.     }
  47.     if (!mounted) return;
  48.     setState(() {
  49.       _loadingPath = false;
  50.       _fileName = _paths != null ? _paths.map((e) => e.name).toString() : '...';
  51.     });
  52.   }
  53.  
  54.   void _clearCachedFiles() {
  55.     FilePicker.platform.clearTemporaryFiles().then((result) {
  56.       _scaffoldKey.currentState.showSnackBar(
  57.         SnackBar(
  58.           backgroundColor: result ? Colors.green : Colors.red,
  59.           content: Text((result
  60.               ? 'Temporary files removed with success.'
  61.               : 'Failed to clean temporary files')),
  62.         ),
  63.       );
  64.     });
  65.   }
  66.  
  67.   void _selectFolder() {
  68.     FilePicker.platform.getDirectoryPath().then((value) {
  69.       setState(() => _directoryPath = value);
  70.     });
  71.   }
  72.  
  73.   @override
  74.   Widget build(BuildContext context) {
  75.     //final assetsAudioPlayer = AssetsAudioPlayer();
  76.     AudioPlayer audioPlayer = AudioPlayer();
  77.     return MaterialApp(
  78.       home: Scaffold(
  79.         key: _scaffoldKey,
  80.         appBar: AppBar(
  81.           title: const Text('File Picker example app'),
  82.         ),
  83.         body: Center(
  84.           child: Padding(
  85.             padding: const EdgeInsets.only(left: 10.0, right: 10.0),
  86.             child: SingleChildScrollView(
  87.               child: Column(
  88.                 mainAxisAlignment: MainAxisAlignment.center,
  89.                 children: <Widget>[
  90.                   Padding(
  91.                     padding: const EdgeInsets.only(top: 20.0),
  92.                     child: DropdownButton(
  93.                         hint: Text('LOAD PATH FROM'),
  94.                         value: _pickingType,
  95.                         items: <DropdownMenuItem>[
  96.                           DropdownMenuItem(
  97.                             child: Text('FROM AUDIO'),
  98.                             value: FileType.audio,
  99.                           ),
  100.                           DropdownMenuItem(
  101.                             child: Text('FROM IMAGE'),
  102.                             value: FileType.image,
  103.                           ),
  104.                           DropdownMenuItem(
  105.                             child: Text('FROM VIDEO'),
  106.                             value: FileType.video,
  107.                           ),
  108.                           DropdownMenuItem(
  109.                             child: Text('FROM MEDIA'),
  110.                             value: FileType.media,
  111.                           ),
  112.                           DropdownMenuItem(
  113.                             child: Text('FROM ANY'),
  114.                             value: FileType.any,
  115.                           ),
  116.                           DropdownMenuItem(
  117.                             child: Text('CUSTOM FORMAT'),
  118.                             value: FileType.custom,
  119.                           ),
  120.                         ],
  121.                         onChanged: (value) => setState(() {
  122.                               _pickingType = value;
  123.                               if (_pickingType != FileType.custom) {
  124.                                 _controller.text = _extension = '';
  125.                               }
  126.                             })),
  127.                   ),
  128.                   ConstrainedBox(
  129.                     constraints: const BoxConstraints.tightFor(width: 100.0),
  130.                     child: _pickingType == FileType.custom
  131.                         ? TextFormField(
  132.                             maxLength: 15,
  133.                             autovalidate: true,
  134.                             controller: _controller,
  135.                             decoration:
  136.                                 InputDecoration(labelText: 'File extension'),
  137.                             keyboardType: TextInputType.text,
  138.                             textCapitalization: TextCapitalization.none,
  139.                           )
  140.                         : const SizedBox(),
  141.                   ),
  142.                   ConstrainedBox(
  143.                     constraints: const BoxConstraints.tightFor(width: 200.0),
  144.                     child: SwitchListTile.adaptive(
  145.                       title: Text('Pick multiple files',
  146.                           textAlign: TextAlign.right),
  147.                       onChanged: (bool value) =>
  148.                           setState(() => _multiPick = value),
  149.                       value: _multiPick,
  150.                     ),
  151.                   ),
  152.                   Padding(
  153.                     padding: const EdgeInsets.only(top: 50.0, bottom: 20.0),
  154.                     child: Column(
  155.                       children: <Widget>[
  156.                         RaisedButton(
  157.                           onPressed: () => _openFileExplorer(),
  158.                           child: Text("Open file picker"),
  159.                         ),
  160.                         RaisedButton(
  161.                           onPressed: () => _selectFolder(),
  162.                           child: Text("Pick folder"),
  163.                         ),
  164.                         RaisedButton(
  165.                           onPressed: () => _clearCachedFiles(),
  166.                           child: Text("Clear temporary files"),
  167.                         ),
  168.                       ],
  169.                     ),
  170.                   ),
  171.                   Builder(
  172.                     builder: (BuildContext context) => _loadingPath
  173.                         ? Padding(
  174.                             padding: const EdgeInsets.only(bottom: 10.0),
  175.                             child: const CircularProgressIndicator(),
  176.                           )
  177.                         : _directoryPath != null
  178.                             ? ListTile(
  179.                                 title: Text('Directory path'),
  180.                                 subtitle: Text(_directoryPath),
  181.                               )
  182.                             : _paths != null
  183.                                 ? Container(
  184.                                     padding:
  185.                                         const EdgeInsets.only(bottom: 30.0),
  186.                                     height: MediaQuery.of(context).size.height *
  187.                                         0.50,
  188.                                     child: Scrollbar(
  189.                                       child: ListView.separated(
  190.                                         itemCount:
  191.                                             _paths != null && _paths.isNotEmpty
  192.                                                 ? _paths.length
  193.                                                 : 1,
  194.                                         itemBuilder:
  195.                                             (BuildContext context, int index) {
  196.                                           final bool isMultiPath =
  197.                                               _paths != null &&
  198.                                                   _paths.isNotEmpty;
  199.                                           final String name = 'File $index: ' +
  200.                                               (isMultiPath
  201.                                                   ? _paths
  202.                                                       .map((e) => e.name)
  203.                                                       .toList()[index]
  204.                                                   : _fileName ?? '...');
  205.                                           final path = _paths
  206.                                               .map((e) => e.path)
  207.                                               .toList()[index]
  208.                                               .toString();
  209.  
  210.                                           return ListTile(
  211.                                             title: Text(
  212.                                               name,
  213.                                             ),
  214.                                             subtitle: Text(path),
  215.                                             leading:
  216.                                                 Icon(Icons.play_circle_filled),
  217.                                             onTap: () {
  218.                                               // assetsAudioPlayer.open(
  219.                                               //   Audio.file(path[0]),
  220.                                               // );
  221.                                               playLocal() async {
  222.                                                 int result = await audioPlayer
  223.                                                     .play(path, isLocal: true);
  224.                                               }
  225.                                             },
  226.                                           );
  227.                                         },
  228.                                         separatorBuilder:
  229.                                             (BuildContext context, int index) =>
  230.                                                 const Divider(),
  231.                                       ),
  232.                                     ),
  233.                                   )
  234.                                 : const SizedBox(),
  235.                   ),
  236.                 ],
  237.               ),
  238.             ),
  239.           ),
  240.         ),
  241.       ),
  242.     );
  243.   }
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment