yubioath-flutter/lib/app/views/device_avatar.dart

97 lines
2.7 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2022-07-07 21:20:04 +03:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/state.dart';
2022-06-05 14:49:16 +03:00
import '../../widgets/custom_icons.dart';
import '../models.dart';
2022-07-07 21:20:04 +03:00
import '../state.dart';
import 'device_images.dart';
class DeviceAvatar extends StatelessWidget {
2022-02-01 15:30:03 +03:00
final Widget child;
2022-06-05 14:49:16 +03:00
final Widget? badge;
2022-06-02 15:49:38 +03:00
final double? radius;
const DeviceAvatar({super.key, required this.child, this.badge, this.radius});
factory DeviceAvatar.yubiKeyData(YubiKeyData data, {double? radius}) =>
DeviceAvatar(
badge: isDesktop && data.node is NfcReaderNode ? nfcIcon : null,
2022-06-02 15:49:38 +03:00
radius: radius,
2022-05-12 09:34:51 +03:00
child: getProductImage(data.info, data.name),
2022-02-01 15:30:03 +03:00
);
factory DeviceAvatar.deviceNode(DeviceNode node, {double? radius}) =>
2022-02-01 15:30:03 +03:00
node.map(
usbYubiKey: (node) {
final info = node.info;
if (info != null) {
return DeviceAvatar.yubiKeyData(
YubiKeyData(node, node.name, info),
2022-06-02 15:49:38 +03:00
radius: radius,
);
}
return DeviceAvatar(
2022-06-02 15:49:38 +03:00
radius: radius,
2022-05-12 09:34:51 +03:00
child: const Icon(Icons.device_unknown),
);
},
nfcReader: (_) => DeviceAvatar(
2022-06-02 15:49:38 +03:00
radius: radius,
2022-06-05 14:49:16 +03:00
child: nfcIcon,
2022-02-01 15:30:03 +03:00
),
);
2022-07-07 21:20:04 +03:00
factory DeviceAvatar.currentDevice(WidgetRef ref, {double? radius}) {
final deviceNode = ref.watch(currentDeviceProvider);
if (deviceNode != null) {
return ref.watch(currentDeviceDataProvider).maybeWhen(
data: (data) => DeviceAvatar.yubiKeyData(
data,
radius: radius,
),
orElse: () => DeviceAvatar.deviceNode(
deviceNode,
radius: radius,
),
);
} else {
return DeviceAvatar(
radius: radius,
child: const Icon(Icons.usb),
);
}
}
@override
Widget build(BuildContext context) {
final radius = this.radius ?? 20;
2022-02-01 15:30:03 +03:00
return Stack(
alignment: AlignmentDirectional.bottomEnd,
children: [
CircleAvatar(
2022-06-02 15:49:38 +03:00
radius: radius,
backgroundColor: Theme.of(context).colorScheme.background,
child: IconTheme(
data: IconTheme.of(context).copyWith(
size: radius,
2022-06-05 14:49:16 +03:00
),
child: child,
2022-02-01 15:30:03 +03:00
),
),
if (badge != null)
CircleAvatar(
2022-06-02 15:49:38 +03:00
radius: radius / 3,
backgroundColor: Theme.of(context).colorScheme.primary,
2022-06-05 14:49:16 +03:00
child: IconTheme(
data: IconTheme.of(context).copyWith(
color: Theme.of(context).colorScheme.onPrimary,
size: radius * 0.5,
),
child: badge!,
2022-02-01 15:30:03 +03:00
),
),
],
);
}
}