mirror of
https://github.com/foss42/apidash.git
synced 2025-08-06 13:51:20 +08:00
Workspace selector feature
This commit is contained in:
@ -1,62 +1,133 @@
|
||||
import 'package:file_selector/file_selector.dart';
|
||||
import 'package:apidash/services/hive_services.dart';
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:file_selector/file_selector.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'field_outlined.dart';
|
||||
|
||||
class WorkspaceSelector extends StatefulWidget {
|
||||
final Future<void> Function(String)? onSelect;
|
||||
class WorkspaceSelector extends HookWidget {
|
||||
const WorkspaceSelector({
|
||||
super.key,
|
||||
required this.onSelect,
|
||||
required this.onContinue,
|
||||
this.onCancel,
|
||||
});
|
||||
|
||||
@override
|
||||
WorkspaceSelectorState createState() => WorkspaceSelectorState();
|
||||
}
|
||||
|
||||
class WorkspaceSelectorState extends State<WorkspaceSelector> {
|
||||
void selectFolder() async {
|
||||
String? selectedDirectory = await getDirectoryPath();
|
||||
if (selectedDirectory != null) {
|
||||
widget.onSelect?.call(selectedDirectory);
|
||||
}
|
||||
}
|
||||
final Future<void> Function(String)? onContinue;
|
||||
final Future<void> Function()? onCancel;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const circularLoader = MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
var selectedDirectory = useState<String?>(null);
|
||||
var workspaceName = useState<String?>(null);
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: SizedBox(
|
||||
width: 400,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Text(
|
||||
kMsgSelectWorkspace,
|
||||
style: kTextStyleButton,
|
||||
),
|
||||
kVSpacer20,
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
"CHOOSE DIRECTORY",
|
||||
style: kCodeStyle.copyWith(
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
kVSpacer5,
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
width: 1,
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
),
|
||||
borderRadius: kBorderRadius6,
|
||||
),
|
||||
padding: kP4,
|
||||
child: Text(
|
||||
style: kTextStyleButtonSmall,
|
||||
selectedDirectory.value ?? "",
|
||||
maxLines: 4,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
),
|
||||
kHSpacer10,
|
||||
FilledButton.tonalIcon(
|
||||
onPressed: () async {
|
||||
selectedDirectory.value = await getDirectoryPath();
|
||||
},
|
||||
label: const Text(kLabelSelect),
|
||||
icon: const Icon(Icons.folder_rounded),
|
||||
),
|
||||
],
|
||||
),
|
||||
kVSpacer10,
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
"WORKSPACE NAME [OPTIONAL]\n(FOLDER WILL BE CREATED IN THE SELECTED DIRECTORY)",
|
||||
style: kCodeStyle.copyWith(
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
kVSpacer5,
|
||||
OutlinedField(
|
||||
keyId: "workspace-name",
|
||||
onChanged: (value) {
|
||||
workspaceName.value = value.trim();
|
||||
},
|
||||
colorScheme: Theme.of(context).colorScheme,
|
||||
),
|
||||
kVSpacer40,
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
FilledButton(
|
||||
onPressed: selectedDirectory.value == null
|
||||
? null
|
||||
: () async {
|
||||
String finalPath = selectedDirectory.value!;
|
||||
if (workspaceName.value != null &&
|
||||
workspaceName.value!.trim().isNotEmpty) {
|
||||
finalPath =
|
||||
p.join(finalPath, workspaceName.value);
|
||||
}
|
||||
await onContinue?.call(finalPath);
|
||||
},
|
||||
child: const Text(kLabelContinue),
|
||||
),
|
||||
kHSpacer10,
|
||||
FilledButton(
|
||||
onPressed: onCancel,
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor:
|
||||
Theme.of(context).brightness == Brightness.dark
|
||||
? kColorDarkDanger
|
||||
: kColorLightDanger,
|
||||
surfaceTintColor: kColorRed,
|
||||
foregroundColor:
|
||||
Theme.of(context).colorScheme.onPrimary),
|
||||
child: const Text(kLabelCancel),
|
||||
)
|
||||
],
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return FutureBuilder<String?>(
|
||||
future: getHiveSaveFolder(),
|
||||
builder: (BuildContext context, AsyncSnapshot<String?> snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return circularLoader;
|
||||
}
|
||||
|
||||
// If there isn't hive selected folder choose it
|
||||
if (snapshot.data == null) {
|
||||
selectFolder();
|
||||
return circularLoader;
|
||||
}
|
||||
|
||||
// Once _hiveSaveFolder is set, display DashApp after hive init
|
||||
return FutureBuilder<void>(
|
||||
future: openHiveBoxes(snapshot.data!),
|
||||
builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
|
||||
// if loading show circularLoader
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return circularLoader;
|
||||
}
|
||||
// Display widget
|
||||
return widget.child;
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user