]> git.proxmox.com Git - flutter/pve_flutter_frontend.git/commitdiff
add guest os selector widget
authorTim Marx <t.marx@proxmox.com>
Fri, 6 Dec 2019 16:58:53 +0000 (17:58 +0100)
committerTim Marx <t.marx@proxmox.com>
Fri, 6 Dec 2019 17:12:43 +0000 (18:12 +0100)
Signed-off-by: Tim Marx <t.marx@proxmox.com>
lib/bloc/pve_guest_os_selector_bloc.dart [new file with mode: 0644]
lib/widgets/pve_guest_os_selector_widget.dart [new file with mode: 0644]

diff --git a/lib/bloc/pve_guest_os_selector_bloc.dart b/lib/bloc/pve_guest_os_selector_bloc.dart
new file mode 100644 (file)
index 0000000..c95cfd2
--- /dev/null
@@ -0,0 +1,59 @@
+import 'package:pve_flutter_frontend/bloc/proxmox_base_bloc.dart';
+import 'package:pve_flutter_frontend/models/pve_nodes_qemu_create_model.dart';
+import 'package:pve_flutter_frontend/states/proxmox_form_field_state.dart';
+
+class PveGuestOsSelectorBloc
+    extends ProxmoxBaseBloc<PveGuestOsSelectorEvent, PveGuestOsSelectorState> {
+  bool fieldRequired;
+
+  Map<OSType, Map<String, String>> osChoices = {
+    OSType.l26: {'desc': '5.x - 2.6 Kernel', 'type': 'Linux'},
+    OSType.l24: {'desc': '2.4 Kernel', 'type': 'Linux'},
+    OSType.win10: {'desc': '10/2016/2019', 'type': 'Microsoft Windows'},
+    OSType.win8: {'desc': '8.x/2012/2012r2', 'type': 'Microsoft Windows'},
+    OSType.win7: {'desc': '7/2008r2', 'type': 'Microsoft Windows'},
+    OSType.w2k8: {'desc': 'Vista/2008', 'type': 'Microsoft Windows'},
+    OSType.wxp: {'desc': 'XP/2003', 'type': 'Microsoft Windows'},
+    OSType.w2k: {'desc': '2000', 'type': 'Microsoft Windows'},
+    OSType.solaris: {'desc': '-', 'type': 'Solaris Kernel'},
+    OSType.other: {'desc': '-', 'type': 'Other'},
+  };
+
+  @override
+  PveGuestOsSelectorState get initialState => PveGuestOsSelectorState();
+
+  PveGuestOsSelectorBloc({this.fieldRequired = false});
+
+  @override
+  Stream<PveGuestOsSelectorState> processEvents(
+      PveGuestOsSelectorEvent event) async* {
+
+    if (event is ChangeOsType) {
+      if (event.type == null && fieldRequired) {
+        yield latestState.copyWith(value: event.type, errorText: "Required");
+      } else {
+        yield latestState.copyWith(value: event.type, errorText: null);
+      }
+
+    }
+  }
+}
+
+class PveGuestOsSelectorState extends PveFormFieldState<OSType> {
+  PveGuestOsSelectorState({OSType value, String errorText})
+      : super(value: value, errorText: errorText);
+
+  PveGuestOsSelectorState copyWith(
+      {String os, OSType value, String errorText}) {
+    return PveGuestOsSelectorState(
+        value: value ?? this.value, errorText: errorText ?? this.errorText);
+  }
+}
+
+abstract class PveGuestOsSelectorEvent {}
+
+class ChangeOsType extends PveGuestOsSelectorEvent {
+  final OSType type;
+
+  ChangeOsType(this.type);
+}
diff --git a/lib/widgets/pve_guest_os_selector_widget.dart b/lib/widgets/pve_guest_os_selector_widget.dart
new file mode 100644 (file)
index 0000000..0d4f3ee
--- /dev/null
@@ -0,0 +1,48 @@
+import 'package:flutter/material.dart';
+import 'package:font_awesome_flutter/font_awesome_flutter.dart';
+import 'package:provider/provider.dart';
+import 'package:pve_flutter_frontend/bloc/pve_guest_os_selector_bloc.dart';
+import 'package:pve_flutter_frontend/models/pve_nodes_qemu_create_model.dart';
+
+class PveGuestOsSelector extends StatelessWidget {
+  @override
+  Widget build(BuildContext context) {
+    final gBloc = Provider.of<PveGuestOsSelectorBloc>(context);
+    return StreamBuilder<PveGuestOsSelectorState>(
+        stream: gBloc.state,
+        initialData: gBloc.state.value,
+        builder: (context, snapshot) {
+          return DropdownButtonFormField<OSType>(
+            decoration: InputDecoration(labelText: 'Guest OS'),
+            items: gBloc.osChoices.keys
+                .map((choice) => DropdownMenuItem(
+                    value: choice,
+                    child:
+                        Row(
+                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
+                          children: [
+                          getIcon(gBloc.osChoices[choice]['type']),
+                          Text(gBloc.osChoices[choice]['desc'])])))
+                .toList(),
+            onChanged: (choice) {
+              gBloc.events.add(ChangeOsType(choice));
+            },
+            value: snapshot.data?.value,
+            validator: (_) => snapshot.data?.errorText,
+            autovalidate: true,
+          );
+        });
+  }
+
+  Widget getIcon(String osGroup) {
+    if (osGroup == "Microsoft Windows"){
+      return Icon(FontAwesomeIcons.windows);
+    }
+
+    if (osGroup == "Linux"){
+      return Icon(FontAwesomeIcons.linux);
+    }
+
+    return Text(osGroup);
+  }
+}