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

91 lines
2.5 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2022-06-05 14:49:16 +03:00
import '../../widgets/custom_icons.dart';
import '../models.dart';
import 'device_images.dart';
class DeviceAvatar extends StatelessWidget {
final bool selected;
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(
2022-06-02 15:49:38 +03:00
{super.key,
this.selected = false,
required this.child,
this.badge,
this.radius});
2022-06-02 15:49:38 +03:00
factory DeviceAvatar.yubiKeyData(YubiKeyData data,
{bool selected = false, double? radius}) =>
DeviceAvatar(
2022-06-05 14:49:16 +03:00
badge: data.node is NfcReaderNode ? nfcIcon : null,
2022-02-01 15:30:03 +03:00
selected: selected,
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-06-02 15:49:38 +03:00
factory DeviceAvatar.deviceNode(DeviceNode node,
{bool selected = false, 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),
selected: selected,
2022-06-02 15:49:38 +03:00
radius: radius,
);
}
return DeviceAvatar(
selected: selected,
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-02-01 15:30:03 +03:00
selected: selected,
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
),
);
@override
Widget build(BuildContext context) {
2022-06-02 15:49:38 +03:00
final radius = this.radius ?? 24;
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-02-01 15:30:03 +03:00
backgroundColor: selected
2022-05-20 15:10:17 +03:00
? Theme.of(context).colorScheme.primary
2022-02-01 15:30:03 +03:00
: Colors.transparent,
child: CircleAvatar(
2022-06-02 15:49:38 +03:00
radius: radius - 1,
2022-02-01 15:30:03 +03:00
backgroundColor: Theme.of(context).colorScheme.background,
2022-06-05 14:49:16 +03:00
child: IconTheme(
data: IconTheme.of(context).copyWith(
size: radius,
),
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: nfcIcon,
2022-02-01 15:30:03 +03:00
),
),
],
);
}
}