From 15f6b116ccdf66af1f00681b39660e54585c4a37 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Tue, 4 Mar 2025 19:42:13 -0600 Subject: [PATCH] Create management portal in Flutter #39 --- web/repertory/lib/main.dart | 40 +++++----- .../lib/widgets/add_mount_widget.dart | 76 +++++++++++++++---- 2 files changed, 80 insertions(+), 36 deletions(-) diff --git a/web/repertory/lib/main.dart b/web/repertory/lib/main.dart index 0a527367..97de5c55 100644 --- a/web/repertory/lib/main.dart +++ b/web/repertory/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:repertory/constants.dart' as constants; @@ -9,7 +10,9 @@ import 'package:repertory/widgets/mount_list_widget.dart'; import 'package:repertory/widgets/mount_settings.dart'; void main() { - runApp(const MyApp()); + runApp( + ChangeNotifierProvider(create: (_) => MountList(), child: const MyApp()), + ); } class MyApp extends StatelessWidget { @@ -85,10 +88,7 @@ class _MyHomePageState extends State { leading: const Icon(Icons.storage), title: Text(widget.title), ), - body: ChangeNotifierProvider( - create: (context) => MountList(), - child: MountListWidget(), - ), + body: MountListWidget(), floatingActionButton: FloatingActionButton( onPressed: _allowAdd @@ -102,19 +102,17 @@ class _MyHomePageState extends State { builder: (_, mountList, __) { return AddMountWidget( allowEncrypt: - !mountList.items.contains( + mountList.items.firstWhereOrNull( (item) => item.type == "encrypt", - ), - mountName: _mountName, + ) == + null, mountType: _mountType, - onNameChanged: - (mountName) => setState( - () => _mountName = mountName ?? "", - ), - onTypeChanged: - (mountType) => setState( - () => _mountType = mountType ?? "S3", - ), + onNameChanged: (mountName) { + _mountName = mountName ?? ""; + }, + onTypeChanged: (mountType) { + _mountType = mountType ?? "S3"; + }, ); }, ), @@ -122,10 +120,8 @@ class _MyHomePageState extends State { TextButton( child: const Text('Cancel'), onPressed: () { - setState(() { - _mountType = "S3"; - _mountName = ""; - }); + _mountType = "S3"; + _mountName = ""; Navigator.of(context).pop(); }, ), @@ -137,10 +133,10 @@ class _MyHomePageState extends State { Provider.of(context, listen: false) .add(_mountType, _mountName) .then((_) { + _mountType = "S3"; + _mountName = ""; setState(() { _allowAdd = true; - _mountType = "S3"; - _mountName = ""; }); }) .catchError((_) { diff --git a/web/repertory/lib/widgets/add_mount_widget.dart b/web/repertory/lib/widgets/add_mount_widget.dart index ab0ac512..5d0d9beb 100644 --- a/web/repertory/lib/widgets/add_mount_widget.dart +++ b/web/repertory/lib/widgets/add_mount_widget.dart @@ -1,8 +1,7 @@ import 'package:flutter/material.dart'; -class AddMountWidget extends StatelessWidget { +class AddMountWidget extends StatefulWidget { final bool allowEncrypt; - final String mountName; final String mountType; final void Function(String? newName) onNameChanged; final void Function(String? newType) onTypeChanged; @@ -12,7 +11,6 @@ class AddMountWidget extends StatelessWidget { AddMountWidget({ super.key, required this.allowEncrypt, - required this.mountName, required this.mountType, required this.onNameChanged, required this.onTypeChanged, @@ -22,23 +20,73 @@ class AddMountWidget extends StatelessWidget { } } + @override + State createState() => _AddMountWidgetState(); +} + +class _AddMountWidgetState extends State { + String? _mountType; + + @override + void initState() { + _mountType = widget.mountType; + super.initState(); + } + @override Widget build(BuildContext context) { return Column( mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, children: [ - // DropdownButton( - // value: mountType, - // onChanged: onTypeChanged, - // items: - // _items.map>((item) { - // return DropdownMenuItem(value: item, child: Text(item)); - // }).toList(), - // ), - TextField( - decoration: InputDecoration(labelText: 'Name'), - onChanged: onNameChanged, + Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + 'Provider Type', + textAlign: TextAlign.left, + style: TextStyle( + color: Theme.of(context).colorScheme.onSurface, + fontWeight: FontWeight.bold, + ), + ), + const SizedBox(width: 10), + DropdownButton( + value: _mountType, + onChanged: (value) { + setState(() { + _mountType = value; + }); + widget.onTypeChanged(value); + }, + items: + widget._items.map>((item) { + return DropdownMenuItem( + value: item, + child: Text(item), + ); + }).toList(), + ), + ], ), + const SizedBox(height: 10), + if (_mountType != "Encrypt") + Text( + 'Configuration Name', + textAlign: TextAlign.left, + style: TextStyle( + color: Theme.of(context).colorScheme.onSurface, + fontWeight: FontWeight.bold, + ), + ), + if (_mountType != "Encrypt") + TextField( + autofocus: true, + decoration: InputDecoration(), + onChanged: widget.onNameChanged, + ), ], ); }