|
| 1 | +import 'package:bluez/bluez.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:settings/view/pages/bluetooth/bluetooth_device_types.dart'; |
| 4 | +import 'package:settings/view/pages/bluetooth/bluetooth_model.dart'; |
| 5 | +import 'package:yaru_icons/widgets/yaru_icons.dart'; |
| 6 | +import 'package:yaru_widgets/yaru_widgets.dart'; |
| 7 | + |
| 8 | +class BluetoothDeviceRow extends StatefulWidget { |
| 9 | + const BluetoothDeviceRow( |
| 10 | + {Key? key, required this.device, required this.model}) |
| 11 | + : super(key: key); |
| 12 | + |
| 13 | + final BlueZDevice device; |
| 14 | + final BluetoothModel model; |
| 15 | + |
| 16 | + @override |
| 17 | + State<BluetoothDeviceRow> createState() => _BluetoothDeviceRowState(); |
| 18 | +} |
| 19 | + |
| 20 | +class _BluetoothDeviceRowState extends State<BluetoothDeviceRow> { |
| 21 | + late String status; |
| 22 | + |
| 23 | + @override |
| 24 | + void initState() { |
| 25 | + status = widget.device.connected ? 'connected' : 'disconnected'; |
| 26 | + |
| 27 | + super.initState(); |
| 28 | + } |
| 29 | + |
| 30 | + @override |
| 31 | + Widget build(BuildContext context) { |
| 32 | + status = widget.device.connected ? 'connected' : 'disconnected'; |
| 33 | + return InkWell( |
| 34 | + borderRadius: BorderRadius.circular(4.0), |
| 35 | + onTap: () => setState(() { |
| 36 | + showSimpleDeviceDialog(context); |
| 37 | + }), |
| 38 | + child: Padding( |
| 39 | + padding: const EdgeInsets.all(8.0), |
| 40 | + child: YaruRow( |
| 41 | + trailingWidget: Text(widget.device.name), |
| 42 | + actionWidget: Text( |
| 43 | + widget.device.connected ? 'connected' : 'disconnected', |
| 44 | + style: TextStyle( |
| 45 | + color: |
| 46 | + Theme.of(context).colorScheme.onSurface.withOpacity(0.7)), |
| 47 | + )), |
| 48 | + ), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + void showSimpleDeviceDialog(BuildContext context) { |
| 53 | + showDialog( |
| 54 | + context: context, |
| 55 | + builder: (context) => StatefulBuilder(builder: (context, setState) { |
| 56 | + return AlertDialog( |
| 57 | + title: Padding( |
| 58 | + padding: const EdgeInsets.only(right: 8, left: 8, bottom: 8), |
| 59 | + child: Row( |
| 60 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 61 | + children: [ |
| 62 | + Flexible( |
| 63 | + child: RichText( |
| 64 | + text: TextSpan( |
| 65 | + text: widget.device.name, |
| 66 | + style: Theme.of(context).textTheme.headline6), |
| 67 | + maxLines: 10, |
| 68 | + overflow: TextOverflow.ellipsis, |
| 69 | + ), |
| 70 | + ), |
| 71 | + Padding( |
| 72 | + padding: const EdgeInsets.only(left: 10), |
| 73 | + child: Icon( |
| 74 | + BluetoothDeviceTypes.getIconForAppearanceCode( |
| 75 | + widget.device.appearance)), |
| 76 | + ) |
| 77 | + ], |
| 78 | + ), |
| 79 | + ), |
| 80 | + content: SizedBox( |
| 81 | + height: 270, |
| 82 | + width: 300, |
| 83 | + child: SingleChildScrollView( |
| 84 | + child: Column( |
| 85 | + children: [ |
| 86 | + YaruRow( |
| 87 | + trailingWidget: widget.device.connected |
| 88 | + ? const Text('Connected') |
| 89 | + : const Text('Disconnected'), |
| 90 | + actionWidget: Switch( |
| 91 | + value: widget.device.connected, |
| 92 | + onChanged: (newValue) async { |
| 93 | + widget.device.connected |
| 94 | + ? await widget.device.disconnect() |
| 95 | + : await widget.device |
| 96 | + .connect() |
| 97 | + .catchError((ioError) => {}); |
| 98 | + Navigator.of(context).pop(); |
| 99 | + setState(() {}); |
| 100 | + })), |
| 101 | + YaruRow( |
| 102 | + trailingWidget: widget.device.paired |
| 103 | + ? const Text('Paired') |
| 104 | + : const Text('Unpaired'), |
| 105 | + actionWidget: Padding( |
| 106 | + padding: const EdgeInsets.only(right: 8), |
| 107 | + child: Text(widget.device.paired ? 'Yes' : 'No'), |
| 108 | + )), |
| 109 | + YaruRow( |
| 110 | + trailingWidget: const Text('Address'), |
| 111 | + actionWidget: Padding( |
| 112 | + padding: const EdgeInsets.only(right: 8), |
| 113 | + child: Text(widget.device.address), |
| 114 | + )), |
| 115 | + YaruRow( |
| 116 | + trailingWidget: const Text('Type'), |
| 117 | + actionWidget: Padding( |
| 118 | + padding: const EdgeInsets.only(right: 8), |
| 119 | + child: Text(BluetoothDeviceTypes |
| 120 | + .map[widget.device.appearance] ?? |
| 121 | + 'Unkown'), |
| 122 | + )), |
| 123 | + Padding( |
| 124 | + padding: const EdgeInsets.only( |
| 125 | + top: 16, bottom: 8, right: 8, left: 8), |
| 126 | + child: SizedBox( |
| 127 | + width: 300, |
| 128 | + child: OutlinedButton( |
| 129 | + onPressed: () { |
| 130 | + if (BluetoothDeviceTypes.isMouse( |
| 131 | + widget.device.appearance)) { |
| 132 | + // TODO: get route name from model |
| 133 | + Navigator.of(context) |
| 134 | + .pushNamed('routeName'); |
| 135 | + } |
| 136 | + }, |
| 137 | + child: const Text('Open device settings')), |
| 138 | + ), |
| 139 | + ), |
| 140 | + Padding( |
| 141 | + padding: const EdgeInsets.all(8), |
| 142 | + child: SizedBox( |
| 143 | + width: 300, |
| 144 | + child: TextButton( |
| 145 | + onPressed: () async { |
| 146 | + await widget.device.disconnect().then( |
| 147 | + (value) => widget.model |
| 148 | + .removeDevice(widget.device)); |
| 149 | + Navigator.of(context).pop(); |
| 150 | + }, |
| 151 | + child: const Text('Remove device')), |
| 152 | + ), |
| 153 | + ) |
| 154 | + ], |
| 155 | + ), |
| 156 | + ), |
| 157 | + ), |
| 158 | + ); |
| 159 | + })).then((value) => setState(() {})); |
| 160 | + } |
| 161 | +} |
0 commit comments