main
 1import 'package:app_settings/app_settings.dart';
 2import 'package:blood_pressure_app/features/old_bluetooth/logic/bluetooth_cubit.dart';
 3import 'package:flutter/material.dart';
 4import 'package:flutter_bloc/flutter_bloc.dart';
 5import 'package:blood_pressure_app/l10n/app_localizations.dart';
 6
 7/// A closed ble input that shows the adapter state and allows to start the input.
 8class ClosedBluetoothInput extends StatelessWidget {
 9  /// Show adapter state and allow starting inputs
10  const ClosedBluetoothInput({super.key,
11    required this.bluetoothCubit,
12    required this.onStarted,
13    this.inputInfo,
14  });
15
16  /// State update provider and interaction with the device.
17  final BluetoothCubit bluetoothCubit;
18
19  /// Called when the user taps on an active start button.
20  final void Function() onStarted;
21
22  /// Callback called when the user wants to know more about this input.
23  ///
24  /// The info icon is not shown when this is null.
25  final void Function()? inputInfo;
26
27  Widget _buildTile({
28    required String text,
29    required IconData icon,
30    required void Function() onTap,
31  }) => ListTile(
32    title: Text(text),
33    leading: Icon(icon),
34    onTap: onTap,
35    trailing: inputInfo == null ? null : IconButton(
36      icon: const Icon(Icons.info_outline),
37      onPressed: inputInfo!,
38    ),
39  );
40  
41  @override
42  Widget build(BuildContext context) {
43    final localizations = AppLocalizations.of(context)!;
44    return BlocBuilder<BluetoothCubit, BluetoothState>(
45      bloc: bluetoothCubit,
46      builder: (context, BluetoothState state) => switch(state) {
47        BluetoothInitial() => const SizedBox.shrink(),
48        BluetoothUnfeasible() => const SizedBox.shrink(),
49        BluetoothUnauthorized() => _buildTile(
50          text: localizations.errBleNoPerms,
51          icon: Icons.bluetooth_disabled,
52          onTap: () async {
53            await AppSettings.openAppSettings();
54            await bluetoothCubit.forceRefresh();
55          },
56        ),
57        BluetoothDisabled() => _buildTile(
58          text: localizations.bluetoothDisabled,
59          icon: Icons.bluetooth_disabled,
60          onTap: () async {
61            final bluetoothOn = await bluetoothCubit.enableBluetooth();
62            if (!bluetoothOn) await AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
63            await bluetoothCubit.forceRefresh();
64          },
65        ),
66        BluetoothReady() => _buildTile(
67          text: localizations.bluetoothInput,
68          icon: Icons.bluetooth,
69          onTap: onStarted,
70        ),
71      },
72    );
73  }
74  
75}