2022-03-21 17:49:08 +00:00
|
|
|
import 'package:cwtch/controllers/filesharing.dart';
|
|
|
|
import 'package:cwtch/models/appstate.dart';
|
2021-12-14 21:33:30 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'dart:io';
|
2022-01-14 22:19:35 +00:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../settings.dart';
|
2021-12-14 21:33:30 +00:00
|
|
|
import 'buttontextfield.dart';
|
2024-02-02 07:48:20 +00:00
|
|
|
import 'package:path/path.dart' as path;
|
2021-12-14 21:33:30 +00:00
|
|
|
|
|
|
|
class CwtchFolderPicker extends StatefulWidget {
|
|
|
|
final String label;
|
|
|
|
final String initialValue;
|
2022-01-14 22:19:35 +00:00
|
|
|
final String tooltip;
|
|
|
|
final String description;
|
2021-12-14 21:33:30 +00:00
|
|
|
final Function(String)? onSave;
|
2022-02-05 00:37:25 +00:00
|
|
|
final Key? testKey;
|
2023-08-02 17:26:09 +00:00
|
|
|
final TextStyle? textStyle;
|
2024-02-02 07:48:20 +00:00
|
|
|
final IconData icon;
|
2024-02-03 02:08:21 +00:00
|
|
|
const CwtchFolderPicker({Key? key, this.testKey, this.textStyle, this.label = "", this.tooltip = "", this.initialValue = "", this.onSave, this.description = "", this.icon = Icons.file_download})
|
|
|
|
: super(key: key);
|
2021-12-14 21:33:30 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_CwtchFolderPickerState createState() => _CwtchFolderPickerState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CwtchFolderPickerState extends State<CwtchFolderPicker> {
|
|
|
|
final TextEditingController ctrlrVal = TextEditingController();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
ctrlrVal.text = widget.initialValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-01-14 22:19:35 +00:00
|
|
|
return ListTile(
|
2024-02-02 07:48:20 +00:00
|
|
|
leading: Icon(widget.icon, color: Provider.of<Settings>(context).theme.messageFromMeTextColor),
|
2022-01-14 22:19:35 +00:00
|
|
|
title: Text(widget.label),
|
|
|
|
subtitle: Text(widget.description),
|
|
|
|
trailing: Container(
|
2022-01-19 22:07:45 +00:00
|
|
|
width: MediaQuery.of(context).size.width / 4,
|
2022-01-14 22:19:35 +00:00
|
|
|
child: CwtchButtonTextField(
|
2022-02-05 00:37:25 +00:00
|
|
|
testKey: widget.testKey,
|
2022-01-14 22:19:35 +00:00
|
|
|
controller: ctrlrVal,
|
2023-08-02 17:26:09 +00:00
|
|
|
textStyle: widget.textStyle,
|
2022-01-14 22:19:35 +00:00
|
|
|
readonly: Platform.isAndroid,
|
2022-03-21 17:49:08 +00:00
|
|
|
onPressed: Provider.of<AppState>(context).disableFilePicker
|
|
|
|
? null
|
|
|
|
: () async {
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-14 21:33:30 +00:00
|
|
|
|
2022-03-21 17:49:08 +00:00
|
|
|
var selectedDirectory = await showSelectDirectoryPicker(context);
|
|
|
|
if (selectedDirectory != null) {
|
|
|
|
//File directory = File(selectedDirectory);
|
2024-02-03 02:08:21 +00:00
|
|
|
selectedDirectory += path.separator;
|
2022-03-21 17:49:08 +00:00
|
|
|
ctrlrVal.text = selectedDirectory;
|
|
|
|
if (widget.onSave != null) {
|
|
|
|
widget.onSave!(selectedDirectory);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// User canceled the picker
|
|
|
|
}
|
|
|
|
},
|
2022-02-05 00:37:25 +00:00
|
|
|
onChanged: widget.onSave,
|
2022-01-14 22:19:35 +00:00
|
|
|
icon: Icon(Icons.folder),
|
2022-01-17 20:24:48 +00:00
|
|
|
tooltip: widget.tooltip,
|
2022-01-14 22:19:35 +00:00
|
|
|
)));
|
2021-12-14 21:33:30 +00:00
|
|
|
}
|
|
|
|
}
|