veilidchat/lib/veilid_processor/views/signal_strength_meter.dart

90 lines
3.3 KiB
Dart
Raw Normal View History

2024-02-11 00:29:58 -05:00
import 'package:async_tools/async_tools.dart';
2024-01-04 22:29:43 -05:00
import 'package:awesome_extensions/awesome_extensions.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:quickalert/quickalert.dart';
import 'package:signal_strength_indicator/signal_strength_indicator.dart';
import 'package:veilid_support/veilid_support.dart';
import '../../theme/theme.dart';
import '../cubit/connection_state_cubit.dart';
class SignalStrengthMeterWidget extends StatelessWidget {
const SignalStrengthMeterWidget({super.key});
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scale = theme.extension<ScaleScheme>()!;
const iconSize = 16.0;
return BlocBuilder<ConnectionStateCubit,
AsyncValue<ProcessorConnectionState>>(builder: (context, state) {
late final Widget iconWidget;
state.when(
data: (connectionState) {
late final double value;
late final Color color;
late final Color inactiveColor;
switch (connectionState.attachment.state) {
case AttachmentState.detached:
iconWidget = Icon(Icons.signal_cellular_nodata,
size: iconSize, color: scale.primaryScale.primaryText);
2024-01-04 22:29:43 -05:00
return;
case AttachmentState.detaching:
iconWidget = Icon(Icons.signal_cellular_off,
size: iconSize, color: scale.primaryScale.primaryText);
2024-01-04 22:29:43 -05:00
return;
case AttachmentState.attaching:
value = 0;
color = scale.primaryScale.primaryText;
2024-01-04 22:29:43 -05:00
case AttachmentState.attachedWeak:
value = 1;
color = scale.primaryScale.primaryText;
2024-01-04 22:29:43 -05:00
case AttachmentState.attachedStrong:
value = 2;
color = scale.primaryScale.primaryText;
2024-01-04 22:29:43 -05:00
case AttachmentState.attachedGood:
value = 3;
color = scale.primaryScale.primaryText;
2024-01-04 22:29:43 -05:00
case AttachmentState.fullyAttached:
value = 4;
color = scale.primaryScale.primaryText;
2024-01-04 22:29:43 -05:00
case AttachmentState.overAttached:
value = 4;
color = scale.primaryScale.primaryText;
2024-01-04 22:29:43 -05:00
}
inactiveColor = scale.primaryScale.primaryText;
2024-01-04 22:29:43 -05:00
iconWidget = SignalStrengthIndicator.bars(
value: value,
activeColor: color,
inactiveColor: inactiveColor,
size: iconSize,
barCount: 4,
spacing: 2);
2024-01-04 22:29:43 -05:00
},
loading: () => {iconWidget = const Icon(Icons.warning)},
error: (e, st) => {
iconWidget = const Icon(Icons.error).onTap(
() async => QuickAlert.show(
type: QuickAlertType.error,
context: context,
title: 'Error',
text: 'Error: {e}\n StackTrace: {st}'),
)
});
return GestureDetector(
onLongPress: () async {
await GoRouterHelper(context).push('/developer');
},
child: iconWidget);
});
}
}