Create management portal in Flutter #39
This commit is contained in:
parent
820617b3ff
commit
badd098fe0
@ -1,6 +0,0 @@
|
|||||||
class DuplicateMountException implements Exception {
|
|
||||||
final String _name;
|
|
||||||
const DuplicateMountException({required name}) : _name = name, super();
|
|
||||||
|
|
||||||
String get name => _name;
|
|
||||||
}
|
|
@ -73,6 +73,10 @@ class MyHomePage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
|
bool _allowAdd = true;
|
||||||
|
String _mountType = "S3";
|
||||||
|
String _mountName = "";
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@ -86,40 +90,74 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
child: MountListWidget(),
|
child: MountListWidget(),
|
||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: () {
|
onPressed:
|
||||||
showDialog(
|
_allowAdd
|
||||||
context: context,
|
? () {
|
||||||
builder: (BuildContext context) {
|
showDialog(
|
||||||
return AlertDialog(
|
context: context,
|
||||||
title: const Text('Add Mount'),
|
builder: (BuildContext context) {
|
||||||
content: Consumer<MountList>(
|
return AlertDialog(
|
||||||
builder: (_, mountList, __) {
|
title: const Text('Add Mount'),
|
||||||
return AddMountWidget(
|
content: Consumer<MountList>(
|
||||||
allowEncrypt:
|
builder: (_, mountList, __) {
|
||||||
!mountList.items.contains(
|
return AddMountWidget(
|
||||||
(item) => item.type == "encrypt",
|
allowEncrypt:
|
||||||
|
!mountList.items.contains(
|
||||||
|
(item) => item.type == "encrypt",
|
||||||
|
),
|
||||||
|
mountName: _mountName,
|
||||||
|
mountType: _mountType,
|
||||||
|
onNameChanged:
|
||||||
|
(mountName) => setState(
|
||||||
|
() => _mountName = mountName ?? "",
|
||||||
|
),
|
||||||
|
onTypeChanged:
|
||||||
|
(mountType) => setState(
|
||||||
|
() => _mountType = mountType ?? "S3",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
child: const Text('Cancel'),
|
||||||
|
onPressed: () {
|
||||||
|
setState(() {
|
||||||
|
_mountType = "S3";
|
||||||
|
_mountName = "";
|
||||||
|
});
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
),
|
),
|
||||||
);
|
TextButton(
|
||||||
},
|
child: const Text('OK'),
|
||||||
),
|
onPressed: () {
|
||||||
actions: [
|
setState(() => _allowAdd = false);
|
||||||
TextButton(
|
|
||||||
child: const Text('Cancel'),
|
Provider.of<MountList>(context, listen: false)
|
||||||
onPressed: () {
|
.add(_mountType, _mountName)
|
||||||
Navigator.of(context).pop();
|
.then((_) {
|
||||||
|
setState(() {
|
||||||
|
_allowAdd = true;
|
||||||
|
_mountType = "S3";
|
||||||
|
_mountName = "";
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catchError((_) {
|
||||||
|
setState(() {
|
||||||
|
_allowAdd = true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
},
|
},
|
||||||
),
|
);
|
||||||
TextButton(
|
}
|
||||||
child: const Text('Add'),
|
: null,
|
||||||
onPressed: () {
|
|
||||||
Navigator.of(context).pop();
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
|
||||||
tooltip: 'Add Mount',
|
tooltip: 'Add Mount',
|
||||||
child: const Icon(Icons.add),
|
child: const Icon(Icons.add),
|
||||||
),
|
),
|
||||||
|
@ -3,7 +3,6 @@ import 'dart:convert';
|
|||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:repertory/errors/duplicate_mount_exception.dart';
|
|
||||||
import 'package:repertory/types/mount_config.dart';
|
import 'package:repertory/types/mount_config.dart';
|
||||||
|
|
||||||
class MountList with ChangeNotifier {
|
class MountList with ChangeNotifier {
|
||||||
@ -47,16 +46,14 @@ class MountList with ChangeNotifier {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void add(MountConfig config) {
|
Future<void> add(String type, String name) async {
|
||||||
var item = _mountList.firstWhereOrNull((cfg) => cfg.name == config.name);
|
await http.post(
|
||||||
if (item != null) {
|
Uri.parse(
|
||||||
throw DuplicateMountException(name: config.name);
|
Uri.encodeFull('${Uri.base.origin}/api/v1/mount?name=$name&type=$type'),
|
||||||
}
|
),
|
||||||
|
);
|
||||||
|
|
||||||
_mountList.add(config);
|
return _fetch();
|
||||||
_sort(_mountList);
|
|
||||||
|
|
||||||
notifyListeners();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove(String name) {
|
void remove(String name) {
|
||||||
|
@ -1,18 +1,24 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class AddMountWidget extends StatefulWidget {
|
class AddMountWidget extends StatelessWidget {
|
||||||
final bool allowEncrypt;
|
final bool allowEncrypt;
|
||||||
const AddMountWidget({super.key, required this.allowEncrypt});
|
final String mountName;
|
||||||
|
final String mountType;
|
||||||
|
final void Function(String? newName) onNameChanged;
|
||||||
|
final void Function(String? newType) onTypeChanged;
|
||||||
|
const AddMountWidget({
|
||||||
|
super.key,
|
||||||
|
required this.allowEncrypt,
|
||||||
|
required this.mountName,
|
||||||
|
required this.mountType,
|
||||||
|
required this.onNameChanged,
|
||||||
|
required this.onTypeChanged,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
|
||||||
State<AddMountWidget> createState() => _AddMountWidgetState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _AddMountWidgetState extends State<AddMountWidget> {
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var items = ["S3", "Sia"];
|
var items = ["S3", "Sia"];
|
||||||
if (widget.allowEncrypt) {
|
if (allowEncrypt) {
|
||||||
items.insert(0, "Encrypt");
|
items.insert(0, "Encrypt");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,14 +26,17 @@ class _AddMountWidgetState extends State<AddMountWidget> {
|
|||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
DropdownButton<String>(
|
DropdownButton<String>(
|
||||||
value: "S3",
|
value: mountType,
|
||||||
onChanged: (newValue) {},
|
onChanged: onTypeChanged,
|
||||||
items:
|
items:
|
||||||
items.map<DropdownMenuItem<String>>((item) {
|
items.map<DropdownMenuItem<String>>((item) {
|
||||||
return DropdownMenuItem<String>(value: item, child: Text(item));
|
return DropdownMenuItem<String>(value: item, child: Text(item));
|
||||||
}).toList(),
|
}).toList(),
|
||||||
),
|
),
|
||||||
TextField(decoration: InputDecoration(labelText: 'Name')),
|
TextField(
|
||||||
|
decoration: InputDecoration(labelText: 'Name'),
|
||||||
|
onChanged: onNameChanged,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user