2021-12-02 13:44:17 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-07-07 21:20:04 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2021-12-02 13:44:17 +03:00
|
|
|
|
2022-08-12 10:52:10 +03:00
|
|
|
import '../../core/state.dart';
|
2022-06-05 14:49:16 +03:00
|
|
|
import '../../widgets/custom_icons.dart';
|
2021-12-02 13:44:17 +03:00
|
|
|
import '../models.dart';
|
2022-07-07 21:20:04 +03:00
|
|
|
import '../state.dart';
|
2021-12-02 13:44:17 +03:00
|
|
|
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;
|
2022-07-06 14:24:01 +03:00
|
|
|
const DeviceAvatar({super.key, required this.child, this.badge, this.radius});
|
2021-12-02 13:44:17 +03:00
|
|
|
|
2022-07-06 14:24:01 +03:00
|
|
|
factory DeviceAvatar.yubiKeyData(YubiKeyData data, {double? radius}) =>
|
2022-02-09 15:05:32 +03:00
|
|
|
DeviceAvatar(
|
2022-08-12 10:52:10 +03:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2022-07-06 14:24:01 +03:00
|
|
|
factory DeviceAvatar.deviceNode(DeviceNode node, {double? radius}) =>
|
2022-02-01 15:30:03 +03:00
|
|
|
node.map(
|
2022-03-07 17:36:49 +03:00
|
|
|
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,
|
2022-03-07 17:36:49 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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),
|
2022-03-07 17:36:49 +03:00
|
|
|
);
|
|
|
|
},
|
2022-02-09 15:05:32 +03:00
|
|
|
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),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:44:17 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-07-06 14:24:01 +03:00
|
|
|
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,
|
2022-07-06 14:24:01 +03:00
|
|
|
backgroundColor: Theme.of(context).colorScheme.background,
|
|
|
|
child: IconTheme(
|
|
|
|
data: IconTheme.of(context).copyWith(
|
|
|
|
size: radius,
|
2022-06-05 14:49:16 +03:00
|
|
|
),
|
2022-07-06 14:24:01 +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,
|
|
|
|
),
|
2022-08-12 10:52:10 +03:00
|
|
|
child: badge!,
|
2022-02-01 15:30:03 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2021-12-02 13:44:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|