yubioath-flutter/lib/app/message.dart

61 lines
1.8 KiB
Dart
Raw Normal View History

2022-03-25 17:43:32 +03:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'models.dart';
import 'state.dart';
2022-03-25 17:43:32 +03:00
ScaffoldFeatureController showMessage(
BuildContext context,
String message, {
Duration duration = const Duration(seconds: 1),
}) {
final width = MediaQuery.of(context).size.width;
2022-03-31 12:41:28 +03:00
final narrow = width < 540;
2022-03-25 17:43:32 +03:00
return ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(message),
duration: duration,
2022-03-31 12:41:28 +03:00
behavior: narrow ? SnackBarBehavior.fixed : SnackBarBehavior.floating,
width: narrow ? null : 400,
2022-03-25 17:43:32 +03:00
));
}
Future<void> showBottomMenu(
BuildContext context, List<MenuAction> actions) async {
2022-04-27 14:09:55 +03:00
MediaQuery? mediaQuery = context.findAncestorWidgetOfExactType<MediaQuery>();
var width = mediaQuery?.data.size.width ?? 0;
await showModalBottomSheet(
2022-04-05 17:11:01 +03:00
context: context,
2022-04-27 11:36:00 +03:00
constraints: width > 540 ? const BoxConstraints(maxWidth: 380) : null,
2022-04-05 17:11:01 +03:00
builder: (context) => SafeArea(child: _BottomMenu(actions)));
}
class _BottomMenu extends ConsumerWidget {
final List<MenuAction> actions;
const _BottomMenu(this.actions);
@override
Widget build(BuildContext context, WidgetRef ref) {
// If current device changes, we need to pop back to the main Page.
ref.listen<DeviceNode?>(currentDeviceProvider, (previous, next) {
Navigator.of(context).pop();
});
2022-04-05 17:11:01 +03:00
return Column(
mainAxisSize: MainAxisSize.min,
children: actions
.map((a) => ListTile(
leading: a.icon,
title: Text(a.text),
enabled: a.action != null,
onTap: a.action == null
? null
: () {
Navigator.pop(context);
a.action?.call(context);
},
))
.toList(),
);
}
}