-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAudioPicker.dart
109 lines (100 loc) · 3.59 KB
/
AudioPicker.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// The purpose of this file is to provide a audio picker for the user to select an audio file to play along side the book they are reading
import 'package:flutter/material.dart';
import 'package:isar/isar.dart';
import 'package:jellybook/models/entry.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:jellybook/widgets/roundedImageWithShadow.dart';
class AudioPicker extends StatefulWidget {
AudioPicker();
_AudioPickerState createState() => _AudioPickerState();
}
class _AudioPickerState extends State<AudioPicker> {
final isar = Isar.getInstance();
String path = '';
// get all downloaded audio files
Future<List<Entry>> getAudioFiles() async {
List<Entry> entries = await isar!.entrys
.filter()
.typeEqualTo(EntryType.audiobook)
.and()
.downloadedEqualTo(true)
.findAll();
return entries;
}
@override
void initState() {
super.initState();
getAudioFiles();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context)?.selectAudioFile ??
'Select Audio File'),
),
body: FutureBuilder(
future: getAudioFiles(),
builder: (BuildContext context, AsyncSnapshot<List<Entry>> snapshot) {
if (snapshot.hasData) {
debugPrint('snapshot has data');
if (snapshot.data.toString() == "[]") {
// have a popup saying that you must download audiobooks first
return AlertDialog(
title: Text(
AppLocalizations.of(context)?.noAudiobooksDownloaded ??
'No Audiobooks Downloaded'),
content: Text(AppLocalizations.of(context)
?.pleaseDownloadAudiobookFirst ??
'Please download an audiobook first'),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(AppLocalizations.of(context)?.ok ?? 'Ok'),
),
],
);
}
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
// create image on left side of list tile
leading: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
boxShadow: [
BoxShadow(
color: Theme.of(context).brightness == Brightness.dark
? Colors.black.withOpacity(0.5)
: Colors.grey.withOpacity(0.5),
spreadRadius: 1,
blurRadius: 3,
offset: const Offset(2, 3),
),
],
),
child: RoundedImageWithShadow(
imageUrl: snapshot.data![index].imagePath,
ratio: 1,
),
),
title: Text(snapshot.data![index].title),
onTap: () {
Navigator.pop(context, snapshot.data![index].filePath);
},
);
},
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
}