import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; import 'discovered_server.dart'; /// One discovered machine. A machine with one route is used by tapping it. A /// machine with several routes (LAN, Tailscale IP, Tailscale name) expands to /// a checklist so the operator chooses which of them to save; the first /// checked route is dialled first and the rest are backups to the same machine. class DiscoveredServerRow extends StatefulWidget { const DiscoveredServerRow({ super.key, required this.server, required this.onSelect, this.enabled = true, }); final DiscoveredServer server; final ValueChanged onSelect; final bool enabled; @override State createState() => _DiscoveredServerRowState(); } class _DiscoveredServerRowState extends State { bool _expanded = false; late final Set _chosen = {widget.server.addresses.first}; bool get _multiple => widget.server.addresses.length > 1; void _tap() { if (!_multiple) { widget.onSelect(widget.server); return; } setState(() => _expanded = !_expanded); } void _use() { final ordered = [ for (final address in widget.server.addresses) if (_chosen.contains(address)) address, ]; widget.onSelect(widget.server.choosing(ordered)); } @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); final server = widget.server; final detail = _multiple ? '${server.addresses.length} addresses' : server.address; final subtitle = Text( server.passwordRequired ? '$detail ยท password' : detail, ); final onTap = widget.enabled ? _tap : null; final Widget head = cupertino ? CupertinoListTile( key: Key('discovered-${server.address}'), leading: const Icon(CupertinoIcons.desktopcomputer), title: Text(server.name), subtitle: subtitle, trailing: _multiple ? Icon( _expanded ? CupertinoIcons.chevron_up : CupertinoIcons.chevron_down, ) : const CupertinoListTileChevron(), onTap: onTap, ) : ListTile( key: Key('discovered-${server.address}'), leading: const Icon(Icons.dns_outlined), title: Text(server.name), subtitle: subtitle, trailing: Icon( _multiple ? (_expanded ? Icons.expand_less : Icons.expand_more) : Icons.chevron_right, ), enabled: widget.enabled, onTap: onTap, ); if (!_multiple || !_expanded) return head; final useLabel = 'Use ${server.name}'; final use = cupertino ? CupertinoButton.filled( key: Key('discovered-use-${server.address}'), onPressed: widget.enabled && _chosen.isNotEmpty ? _use : null, child: Text(useLabel), ) : FilledButton( key: Key('discovered-use-${server.address}'), onPressed: widget.enabled && _chosen.isNotEmpty ? _use : null, child: Text(useLabel), ); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ head, Padding( padding: const EdgeInsetsDirectional.only(start: 16, end: 16), child: Text( 'Choose how to reach ${server.name}. Checked addresses are saved ' 'as routes to this one machine; the first is tried first.', ), ), for (final address in server.addresses) _choice(context, cupertino, address), Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: Align(alignment: AlignmentDirectional.centerStart, child: use), ), ], ); } Widget _choice( BuildContext context, bool cupertino, DiscoveredAddress address, ) { final checked = _chosen.contains(address); void toggle(bool? value) => setState(() { if (value ?? false) { _chosen.add(address); } else { _chosen.remove(address); } }); final key = Key('discovered-route-${address.endpoint}'); final title = Text(address.label); final subtitle = Text(address.endpoint.toString()); if (cupertino) { return CupertinoListTile( key: key, title: title, subtitle: subtitle, trailing: CupertinoCheckbox( value: checked, onChanged: widget.enabled ? toggle : null, ), onTap: widget.enabled ? () => toggle(!checked) : null, ); } return CheckboxListTile( key: key, value: checked, onChanged: widget.enabled ? toggle : null, title: title, subtitle: subtitle, controlAffinity: ListTileControlAffinity.leading, ); } }