Add and use showMessage for SnackBars.

This commit is contained in:
Dain Nilsson 2022-03-25 15:43:32 +01:00
parent 53699612f4
commit 4b5ea2fdd7
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
16 changed files with 63 additions and 160 deletions

View File

@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'app/logging.dart';
import 'app/message.dart';
import 'app/views/responsive_dialog.dart';
import 'core/state.dart';
import 'desktop/state.dart';
@ -42,12 +43,7 @@ class AboutPage extends ConsumerWidget {
final data = response['diagnostics'];
final text = const JsonEncoder.withIndent(' ').convert(data);
await Clipboard.setData(ClipboardData(text: text));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Diagnostic data copied to clipboard'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Diagnostic data copied to clipboard');
},
child: const Text('Run diagnostics...'),
),
@ -79,12 +75,7 @@ class LoggingPanel extends ConsumerWidget {
onChanged: (level) {
ref.read(logLevelProvider.notifier).setLogLevel(level!);
_log.config('Log level set to $level');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Log level set to $level'),
duration: const Duration(seconds: 2),
),
);
showMessage(context, 'Log level set to $level');
},
),
const SizedBox(width: 8.0),
@ -94,12 +85,7 @@ class LoggingPanel extends ConsumerWidget {
_log.info('Copying log to clipboard...');
final logs = LogBuffer.of(context).getLogs().join('\n');
await Clipboard.setData(ClipboardData(text: logs));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Log copied to clipboard'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Log copied to clipboard');
},
),
],

15
lib/app/message.dart Executable file
View File

@ -0,0 +1,15 @@
import 'package:flutter/material.dart';
ScaffoldFeatureController showMessage(
BuildContext context,
String message, {
Duration duration = const Duration(seconds: 1),
}) {
final width = MediaQuery.of(context).size.width;
return ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(message),
duration: duration,
behavior: SnackBarBehavior.floating,
width: width < 540 ? null : 400,
));
}

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import '../../app/message.dart';
import '../state.dart';
import '../../app/views/responsive_dialog.dart';
import '../../fido/models.dart';
@ -89,12 +90,7 @@ class _AddFingerprintDialogState extends ConsumerState<AddFingerprintDialog>
}, onError: (error, stacktrace) {
_log.severe('Error adding fingerprint', error, stacktrace);
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Error adding fingerprint'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Error adding fingerprint');
});
}
@ -180,12 +176,7 @@ class _AddFingerprintDialogState extends ConsumerState<AddFingerprintDialog>
.read(fingerprintProvider(widget.node.path).notifier)
.renameFingerprint(_fingerprint!, _label);
Navigator.of(context).pop(true);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Fingerprint added'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Fingerprint added');
}
: null,
child: const Text('Save'),

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/views/responsive_dialog.dart';
import '../models.dart';
import '../state.dart';
@ -43,12 +44,7 @@ class DeleteCredentialDialog extends ConsumerWidget {
.read(credentialProvider(device.path).notifier)
.deleteCredential(credential);
Navigator.of(context).pop(true);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Credential deleted'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Credential deleted');
},
child: const Text('Delete'),
),

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/views/responsive_dialog.dart';
import '../models.dart';
import '../state.dart';
@ -43,12 +44,7 @@ class DeleteFingerprintDialog extends ConsumerWidget {
.read(fingerprintProvider(device.path).notifier)
.deleteFingerprint(fingerprint);
Navigator.of(context).pop(true);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Fingerprint deleted'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Fingerprint deleted');
},
child: const Text('Delete'),
),

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/state.dart';
import '../../app/views/responsive_dialog.dart';
@ -121,12 +122,7 @@ class _FidoPinDialogState extends ConsumerState<FidoPinDialog> {
.setPin(_newPin, oldPin: oldPin);
result.when(success: () {
Navigator.of(context).pop(true);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('PIN set'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'PIN set');
}, failed: (retries, authBlocked) {
setState(() {
if (authBlocked) {

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/views/responsive_dialog.dart';
import '../models.dart';
import '../state.dart';
@ -72,12 +73,7 @@ class _RenameAccountDialogState extends ConsumerState<RenameFingerprintDialog> {
.read(fingerprintProvider(widget.device.path).notifier)
.renameFingerprint(fingerprint, _label);
Navigator.of(context).pop(renamed);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Fingerprint renamed'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Fingerprint renamed');
}
: null,
child: const Text('Save'),

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import '../../app/message.dart';
import '../../core/models.dart';
import '../state.dart';
import '../../app/views/responsive_dialog.dart';
@ -90,21 +91,11 @@ class _ResetDialogState extends ConsumerState<ResetDialog> {
}, onDone: () {
_subscription = null;
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('FIDO application reset'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'FIDO application reset');
}, onError: (e) {
_log.severe('Error performing FIDO reset', e);
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Error performing reset'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Error performing reset');
});
}
: null,

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:collection/collection.dart';
import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/state.dart';
import '../../app/views/app_failure_screen.dart';
@ -193,12 +194,11 @@ class _ManagementScreenState extends ConsumerState<ManagementScreen> {
try {
if (reboot) {
// This will take longer, show a message
close = ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(
content: Text('Reconfiguring YubiKey...'),
duration: Duration(seconds: 8),
))
.close;
close = showMessage(
context,
'Reconfiguring YubiKey...',
duration: const Duration(seconds: 8),
).close;
}
await ref
.read(managementStateProvider(widget.deviceData.node.path).notifier)
@ -207,10 +207,7 @@ class _ManagementScreenState extends ConsumerState<ManagementScreen> {
.copyWith(enabledCapabilities: _enabled),
reboot: reboot,
);
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Configuration updated'),
duration: Duration(seconds: 2),
));
showMessage(context, 'Configuration updated');
} finally {
close?.call();
}
@ -221,10 +218,7 @@ class _ManagementScreenState extends ConsumerState<ManagementScreen> {
UsbInterfaces.forCapabilites(
info.config.enabledCapabilities[Transport.usb] ?? 0),
onSubmit: (enabledInterfaces) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Not yet implemented!'),
duration: Duration(seconds: 1),
));
showMessage(context, 'Not yet implemented!');
});
@override

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/state.dart';
import '../models.dart';
@ -92,23 +93,13 @@ mixin AccountMixin {
Future<OathCode> calculateCode(BuildContext context, WidgetRef ref) async {
Function? close;
if (credential.touchRequired) {
close = ScaffoldMessenger.of(context)
.showSnackBar(
const SnackBar(
content: Text('Touch your YubiKey'),
duration: Duration(seconds: 30),
),
)
close = showMessage(context, 'Touch your YubiKey',
duration: const Duration(seconds: 30))
.close;
} else if (credential.oathType == OathType.hotp) {
final showPrompt = Timer(const Duration(milliseconds: 500), () {
close = ScaffoldMessenger.of(context)
.showSnackBar(
const SnackBar(
content: Text('Touch your YubiKey'),
duration: Duration(seconds: 30),
),
)
close = showMessage(context, 'Touch your YubiKey',
duration: const Duration(seconds: 30))
.close;
});
close = showPrompt.cancel;
@ -129,12 +120,7 @@ mixin AccountMixin {
final code = getCode(ref);
if (code != null) {
Clipboard.setData(ClipboardData(text: code.value));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Code copied to clipboard'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Code copied to clipboard');
}
}
@ -189,12 +175,7 @@ mixin AccountMixin {
? null
: (context) {
Clipboard.setData(ClipboardData(text: code.value));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Code copied to clipboard'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Code copied to clipboard');
},
),
MenuAction(

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/state.dart';
import '../../app/models.dart';
import '../../app/views/responsive_dialog.dart';
@ -322,12 +323,7 @@ class _OathAddAccountPageState extends ConsumerState<OathAddAccountPage> {
credentialListProvider(widget.device.path).notifier)
.addAccount(cred.toUri(), requireTouch: _touch);
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Account added'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Account added');
} else {
setState(() {
_validateSecretLength = true;

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/views/responsive_dialog.dart';
import '../models.dart';
import '../state.dart';
@ -50,12 +51,7 @@ class DeleteAccountDialog extends ConsumerWidget {
.read(credentialListProvider(device.path).notifier)
.deleteAccount(credential);
Navigator.of(context).pop(true);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Account deleted'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Account deleted');
},
child: const Text('Delete'),
),

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/views/responsive_dialog.dart';
import '../../app/models.dart';
import '../../app/state.dart';
@ -66,12 +67,7 @@ class _ManagePasswordDialogState extends ConsumerState<ManagePasswordDialog> {
.unsetPassword(_currentPassword);
if (result) {
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Password removed'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Password removed');
} else {
setState(() {
_currentIsWrong = true;
@ -88,12 +84,7 @@ class _ManagePasswordDialogState extends ConsumerState<ManagePasswordDialog> {
.read(oathStateProvider(widget.path).notifier)
.forgetPassword();
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Password forgotten'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Password forgotten');
},
),
],
@ -149,12 +140,7 @@ class _ManagePasswordDialogState extends ConsumerState<ManagePasswordDialog> {
.setPassword(_currentPassword, _newPassword);
if (result) {
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Password set'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Password set');
} else {
setState(() {
_currentIsWrong = true;

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:yubico_authenticator/oath/models.dart';
import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/views/app_failure_screen.dart';
import '../../app/views/app_loading_screen.dart';
@ -29,19 +30,9 @@ class OathScreen extends ConsumerWidget {
oathStateProvider(deviceData.node.path).notifier)
.unlock(password, remember: remember);
if (!result.first) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Wrong password'),
duration: Duration(seconds: 1),
),
);
showMessage(context, 'Wrong password');
} else if (remember && !result.second) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Failed to remember password'),
duration: Duration(seconds: 1),
),
);
showMessage(context, 'Failed to remember password');
}
},
),

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/views/responsive_dialog.dart';
import '../models.dart';
import '../state.dart';
@ -107,12 +108,7 @@ class _RenameAccountDialogState extends ConsumerState<RenameAccountDialog> {
.renameAccount(credential,
_issuer.isNotEmpty ? _issuer : null, _account);
Navigator.of(context).pop(renamed);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Account renamed'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'Account renamed');
}
: null,
child: const Text('Save'),

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/message.dart';
import '../../app/views/responsive_dialog.dart';
import '../state.dart';
import '../../app/models.dart';
@ -39,12 +40,7 @@ class ResetDialog extends ConsumerWidget {
onPressed: () async {
await ref.read(oathStateProvider(device.path).notifier).reset();
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('OATH application reset'),
duration: Duration(seconds: 2),
),
);
showMessage(context, 'OATH application reset');
},
child: const Text('Reset'),
),