first commit
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../../models/finamp_models.dart';
|
||||
|
||||
class AppDirectoryLocationForm extends StatefulWidget {
|
||||
const AppDirectoryLocationForm({Key? key, required this.formKey})
|
||||
: super(key: key);
|
||||
|
||||
final Key formKey;
|
||||
|
||||
@override
|
||||
State<AppDirectoryLocationForm> createState() =>
|
||||
_AppDirectoryLocationFormState();
|
||||
}
|
||||
|
||||
class _AppDirectoryLocationFormState extends State<AppDirectoryLocationForm> {
|
||||
Directory? selectedDirectory;
|
||||
late Future<List<Directory>?> externalStorageListFuture;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
externalStorageListFuture = getExternalStorageDirectories();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Form(
|
||||
key: widget.formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
FutureBuilder<List<Directory>?>(
|
||||
future: externalStorageListFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
if (snapshot.data!.isEmpty) {
|
||||
return const Text("No external directories.");
|
||||
}
|
||||
List<DropdownMenuItem<Directory>> dropdownButtonItems =
|
||||
snapshot.data!
|
||||
.map((e) => DropdownMenuItem(
|
||||
value: e,
|
||||
child: Text(
|
||||
e.path,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
))
|
||||
.toList();
|
||||
return DropdownButtonFormField<Directory>(
|
||||
items: dropdownButtonItems,
|
||||
hint: const Text("Location"),
|
||||
isExpanded: true,
|
||||
value: selectedDirectory,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
selectedDirectory = value;
|
||||
});
|
||||
},
|
||||
validator: (value) {
|
||||
if (value == null) {
|
||||
return "Required";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (newValue) {
|
||||
if (newValue != null) {
|
||||
context.read<NewDownloadLocation>().path = newValue.path;
|
||||
}
|
||||
},
|
||||
);
|
||||
} else if (snapshot.hasError) {
|
||||
return Text(snapshot.error.toString());
|
||||
} else {
|
||||
return const CircularProgressIndicator.adaptive();
|
||||
}
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: const InputDecoration(labelText: "Name (required)"),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return "Required";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (newValue) {
|
||||
if (newValue != null) {
|
||||
context.read<NewDownloadLocation>().name = newValue;
|
||||
}
|
||||
},
|
||||
),
|
||||
const Padding(padding: EdgeInsets.all(8.0)),
|
||||
Text(
|
||||
"If the path doesn't contain \"emulated\", it is proably external storage.",
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../models/finamp_models.dart';
|
||||
|
||||
class CustomDownloadLocationForm extends StatefulWidget {
|
||||
const CustomDownloadLocationForm({Key? key, required this.formKey})
|
||||
: super(key: key);
|
||||
|
||||
final Key formKey;
|
||||
|
||||
@override
|
||||
State<CustomDownloadLocationForm> createState() =>
|
||||
_CustomDownloadLocationFormState();
|
||||
}
|
||||
|
||||
class _CustomDownloadLocationFormState
|
||||
extends State<CustomDownloadLocationForm> {
|
||||
Directory? selectedDirectory;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Form(
|
||||
key: widget.formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
FormField<Directory>(
|
||||
builder: (field) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ClipRRect(
|
||||
borderRadius: const BorderRadius.all(Radius.circular(4)),
|
||||
child: Material(
|
||||
color: Theme.of(context).colorScheme.secondaryContainer,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
selectedDirectory == null
|
||||
? AppLocalizations.of(context)!
|
||||
.selectDirectory
|
||||
: selectedDirectory!.path.replaceFirst(
|
||||
"${selectedDirectory!.parent.path}/",
|
||||
""),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
maxLines: 1,
|
||||
style: selectedDirectory == null
|
||||
? Theme.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(
|
||||
color: Theme.of(context).hintColor,
|
||||
)
|
||||
: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.folder),
|
||||
onPressed: () async {
|
||||
String? newPath = await FilePicker.platform
|
||||
.getDirectoryPath();
|
||||
|
||||
if (newPath != null) {
|
||||
setState(() {
|
||||
selectedDirectory = Directory(newPath);
|
||||
});
|
||||
}
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (field.hasError)
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(0, 4, 0, 0),
|
||||
child: Text(
|
||||
field.errorText ??
|
||||
AppLocalizations.of(context)!.unknownError,
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.bodySmall
|
||||
?.copyWith(color: Theme.of(context).colorScheme.error),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
validator: (_) {
|
||||
if (selectedDirectory == null) {
|
||||
return AppLocalizations.of(context)!.required;
|
||||
}
|
||||
|
||||
// There are a load of null checks here, but since we've already
|
||||
// checked if selectedDirectory is null we should be fine.
|
||||
|
||||
if (selectedDirectory!.path == "/") {
|
||||
return AppLocalizations.of(context)!
|
||||
.pathReturnSlashErrorMessage;
|
||||
}
|
||||
|
||||
// This checks if the chosen directory is empty
|
||||
if (selectedDirectory!
|
||||
.listSync()
|
||||
.where((event) => !event.path
|
||||
.replaceFirst(selectedDirectory!.path, "")
|
||||
.contains("."))
|
||||
.isNotEmpty) {
|
||||
return AppLocalizations.of(context)!.directoryMustBeEmpty;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (_) {
|
||||
if (selectedDirectory != null) {
|
||||
context.read<NewDownloadLocation>().path =
|
||||
selectedDirectory!.path;
|
||||
}
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration:
|
||||
InputDecoration(labelText: AppLocalizations.of(context)!.name),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return AppLocalizations.of(context)!.required;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSaved: (newValue) {
|
||||
if (newValue != null) {
|
||||
context.read<NewDownloadLocation>().name = newValue;
|
||||
}
|
||||
},
|
||||
),
|
||||
const Padding(padding: EdgeInsets.all(8.0)),
|
||||
Text(AppLocalizations.of(context)!.customLocationsBuggy,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(color: Colors.red)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user