An open-source cross-platform alternative to AirDrop
fix: keep device name unchanged when cancelling the dialog (#3608)
Shlomocfa2838290f4
diff --git a/app/lib/pages/tabs/settings_tab.dart b/app/lib/pages/tabs/settings_tab.dartindex 28f5525e..dd16c516 100644--- a/app/lib/pages/tabs/settings_tab.dart+++ b/app/lib/pages/tabs/settings_tab.dart@@ -368,39 +368,42 @@ class SettingsTab extends StatelessWidget {child: TextFieldWithActions(name: t.settingsTab.network.alias,controller: vm.aliasController,- onChanged: (s) async {- await ref.notifier(settingsProvider).setAlias(s);+ allowEmpty: false,+ onSubmitted: (confirmedAlias) async {+ final alias = confirmedAlias.trim();+ vm.aliasController.text = alias;+ await ref.notifier(settingsProvider).setAlias(alias);},actions: [Tooltip(message: t.settingsTab.network.generateRandomAlias,child: IconButton(- onPressed: () async {- // Generates random alias- final newAlias = generateRandomAlias();-- // Update the TextField with the new alias- vm.aliasController.text = newAlias;-- // Persist the new alias using the settingsProvider- await ref.notifier(settingsProvider).setAlias(newAlias);+ onPressed: () {+ final randomAlias = generateRandomAlias();+ vm.aliasController.text = randomAlias;},icon: const Icon(Icons.casino),),),- Tooltip(- message: t.settingsTab.network.useSystemName,- child: IconButton(- onPressed: () async {- final newAlias = await getDeviceName();- if (newAlias == null) {- return;- }+ Builder(+ builder: (dialogContext) => Tooltip(+ message: t.settingsTab.network.useSystemName,+ child: IconButton(+ onPressed: () async {+ final systemName = await getDeviceName();+ if (systemName == null || !dialogContext.mounted) {+ return;+ }- vm.aliasController.text = newAlias;- await ref.notifier(settingsProvider).setAlias(newAlias);- },- icon: const Icon(Icons.desktop_windows_rounded),+ final isDialogOpen = ModalRoute.of(dialogContext)?.isCurrent ?? false;+ if (!isDialogOpen) {+ return;+ }++ vm.aliasController.text = systemName;+ },+ icon: const Icon(Icons.desktop_windows_rounded),+ ),),),],diff --git a/app/lib/widget/dialogs/text_field_with_actions.dart b/app/lib/widget/dialogs/text_field_with_actions.dartindex 7804e664..b0e56f51 100644--- a/app/lib/widget/dialogs/text_field_with_actions.dart+++ b/app/lib/widget/dialogs/text_field_with_actions.dart@@ -8,13 +8,21 @@ import 'package:routerino/routerino.dart';class TextFieldWithActions extends StatefulWidget {final String name;final TextEditingController controller;- final ValueChanged<String> onChanged;+ final bool allowEmpty;++ /// Called while typing. Cancellation does not undo this callback's effects.+ final ValueChanged<String>? onChanged;++ /// Called on confirmation. Cancellation always restores the original text in the controller.+ final ValueChanged<String>? onSubmitted;final List<Widget> actions;const TextFieldWithActions({required this.name,required this.controller,- required this.onChanged,+ this.allowEmpty = true,+ this.onChanged,+ this.onSubmitted,required this.actions,});@@ -32,9 +40,10 @@ class _TextFieldWithActionsState extends State<TextFieldWithActions> {foregroundColor: Theme.of(context).colorScheme.onSurface,),onPressed: () async {- await showDialog(+ final originalText = widget.controller.text;+ final confirmedText = await showDialog<String>(context: context,- builder: (context) {+ builder: (dialogContext) {return AlertDialog(title: Text(widget.name),content: Column(@@ -51,23 +60,43 @@ class _TextFieldWithActionsState extends State<TextFieldWithActions> {textAlign: TextAlign.center,onChanged: widget.onChanged,autofocus: true,- onFieldSubmitted: (_) => context.pop(),+ onFieldSubmitted: (text) {+ if (widget.allowEmpty || text.trim().isNotEmpty) {+ dialogContext.pop(text);+ }+ },),],),actions: [- ElevatedButton(- style: ElevatedButton.styleFrom(- backgroundColor: Theme.of(context).colorScheme.primary,- foregroundColor: Theme.of(context).colorScheme.onPrimary,+ ValueListenableBuilder<TextEditingValue>(+ valueListenable: widget.controller,+ builder: (context, value, child) => ElevatedButton(+ style: ElevatedButton.styleFrom(+ backgroundColor: Theme.of(dialogContext).colorScheme.primary,+ foregroundColor: Theme.of(dialogContext).colorScheme.onPrimary,+ ),+ onPressed: widget.allowEmpty || value.text.trim().isNotEmpty ? () => dialogContext.pop(widget.controller.text) : null,+ child: Text(t.general.confirm),),- onPressed: () => context.pop(),- child: Text(t.general.confirm),),],);},);+ if (!mounted) {+ return;+ }+ setState(() {+ if (confirmedText != null) {+ widget.controller.text = confirmedText;+ } else {+ widget.controller.text = originalText;+ }+ });+ if (confirmedText != null) {+ widget.onSubmitted?.call(confirmedText);+ }},child: Padding(padding: const EdgeInsets.symmetric(vertical: 5),