One Flutter client for network-computer: drive a remote Linux/Mac desktop from Android, iPhone, iPad, or desktop over WebRTC.

0 downloads

network-computer-flutter/lib/connect_screen.dart
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/network-computer-flutter.git
HTTPS
https://gitstable.com/git/askrobots/network-computer-flutter.git
SSH
ssh://git@gitstable.com:2222/askrobots/network-computer-flutter.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Quellhorst No colour (compare) in ⋯: the desk's picture in grey on this device c74471a 1 month, 3 weeks ago History
connect_screen.dart · 8.2 KB · 225 lines
1import 'package:flutter/material.dart';
2import 'package:provider/provider.dart';
3import 'package:shared_preferences/shared_preferences.dart';
4import 'signaling.dart';
5import 'session_store.dart';
6import 'secrets.dart';
7
8class ConnectScreen extends StatefulWidget {
9 const ConnectScreen({super.key});
10 @override
11 State<ConnectScreen> createState() => _ConnectScreenState();
12}
13
14class _ConnectScreenState extends State<ConnectScreen> {
15 final url = TextEditingController();
16 final user = TextEditingController(text: 'nc');
17 final pass = TextEditingController();
18 final pin = TextEditingController();
19 final fp = TextEditingController();
20 String host = '';
21 bool relayOnly = false;
22 bool paired = false;
23
24 @override
25 void initState() {
26 super.initState();
27 _restore();
28 }
29
30 Future<void> _restore() async {
31 final p = await SharedPreferences.getInstance();
32 await p.remove('pin'); // PIN is no longer stored; pairing replaces it
33 setState(() {
34 url.text = p.getString('url') ?? 'https://';
35 user.text = p.getString('user') ?? 'nc';
36 pass.text = '';
37 fp.text = p.getString('fp') ?? '';
38 host = p.getString('host') ?? '';
39 relayOnly = p.getBool('relay') ?? false;
40 });
41 final saved = await Secrets.read('pass'); // the keychain (moved there from preferences)
42 if (mounted && saved != null) setState(() => pass.text = saved);
43 // Developer test hook: --dart-define=NC_AUTO_URL=... (and NC_AUTO_PASS,
44 // NC_AUTO_HOST, NC_AUTO_PIN) fills the form and connects, so a build can
45 // be checked end to end without typing. Values come from the command line
46 // at build time; nothing is stored in the source.
47 const autoUrl = String.fromEnvironment('NC_AUTO_URL');
48 if (autoUrl.isNotEmpty) {
49 setState(() {
50 url.text = autoUrl;
51 pass.text = const String.fromEnvironment('NC_AUTO_PASS');
52 pin.text = const String.fromEnvironment('NC_AUTO_PIN');
53 const h = String.fromEnvironment('NC_AUTO_HOST');
54 if (h.isNotEmpty) host = h;
55 });
56 await _refresh();
57 if (mounted) await _connect();
58 return;
59 }
60 _refresh();
61 }
62
63 Future<void> _save() async {
64 final p = await SharedPreferences.getInstance();
65 await p.setString('url', url.text);
66 await p.setString('user', user.text);
67 await Secrets.write('pass', pass.text);
68 await p.setString('fp', fp.text);
69 await p.setString('host', host);
70 await p.setBool('relay', relayOnly);
71 }
72
73 Endpoint? _endpoint() {
74 final u = Uri.tryParse(url.text.trim());
75 if (u == null || u.host.isEmpty) return null;
76 return Endpoint(u, user.text.trim(), pass.text, fingerprint: fp.text);
77 }
78
79 Future<void> _refresh() async {
80 final ep = _endpoint();
81 if (ep == null) return;
82 final store = context.read<SessionStore>();
83 await store.refreshHosts(ep);
84 if (host.isEmpty && store.hosts.isNotEmpty) setState(() => host = store.hosts.first);
85 await _checkPaired();
86 }
87
88 Future<void> _checkPaired() async {
89 final ep = _endpoint();
90 final p = ep != null && host.isNotEmpty && await SessionStore.isPaired(ep, host);
91 if (mounted) setState(() => paired = p);
92 }
93
94 Future<void> _connect() async {
95 final ep = _endpoint();
96 if (ep == null) { _snack('Enter a valid rendezvous URL'); return; }
97 if (host.isEmpty) { _snack('Pick a host'); return; }
98 final store = context.read<SessionStore>();
99 await _save();
100 await store.connect(ep, host, pin.text.trim(), relayOnly);
101 }
102
103 void _snack(String s) =>
104 ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(s)));
105
106 @override
107 Widget build(BuildContext context) {
108 final store = context.watch<SessionStore>();
109 final connecting = store.state == ConnState.connecting;
110 final size = MediaQuery.sizeOf(context);
111 // sideways phone or tablet: two columns, so the form fits without scrolling
112 final wide = size.width > size.height && size.width >= 560;
113
114 final header = Row(children: [
115 Container(
116 width: wide ? 32 : 40, height: wide ? 32 : 40,
117 decoration: BoxDecoration(
118 borderRadius: BorderRadius.circular(10),
119 gradient: const LinearGradient(colors: [Color(0xFF4C8DFF), Color(0xFF3AD29F)]),
120 ),
121 child: Icon(Icons.desktop_windows, size: wide ? 18 : 22),
122 ),
123 const SizedBox(width: 12),
124 Expanded(child: Text('Network Computer',
125 style: TextStyle(fontSize: wide ? 17 : 20, fontWeight: FontWeight.w600))),
126 ]);
127 final login = <Widget>[
128 _field(url, 'Rendezvous', hint: 'https://host:8765', dense: wide),
129 Row(children: [
130 Expanded(child: _field(user, 'User', dense: wide)),
131 const SizedBox(width: 10),
132 Expanded(child: _field(pass, 'Password', obscure: true, dense: wide)),
133 ]),
134 ];
135 final hostRow = Row(children: [
136 Expanded(
137 child: DropdownButtonFormField<String>(
138 initialValue: store.hosts.contains(host) ? host : null,
139 decoration: InputDecoration(labelText: 'Host', isDense: wide),
140 items: store.hosts.map((h) => DropdownMenuItem(value: h, child: Text(h))).toList(),
141 onChanged: (v) { setState(() => host = v ?? ''); _checkPaired(); },
142 ),
143 ),
144 IconButton(onPressed: _refresh, icon: const Icon(Icons.refresh)),
145 ]);
146 final pinField = _field(pin, paired ? 'Host PIN (paired — not needed)' : 'Host PIN',
147 keyboard: TextInputType.number, dense: wide);
148 final fpField = _field(fp, 'TLS fingerprint (optional)',
149 hint: 'only for a self-signed secure-mode rendezvous', dense: wide);
150 final relay = SwitchListTile(
151 contentPadding: EdgeInsets.zero,
152 dense: wide,
153 title: const Text('Force TURN relay'),
154 value: relayOnly,
155 onChanged: (v) => setState(() => relayOnly = v),
156 );
157 final connectButton = FilledButton(
158 onPressed: connecting ? null : _connect,
159 child: Padding(
160 padding: const EdgeInsets.all(4),
161 child: connecting ? Text(store.status) : const Text('Connect'),
162 ),
163 );
164 final failure = store.state == ConnState.failed
165 ? Padding(
166 padding: const EdgeInsets.only(top: 8),
167 child: Text(store.status, style: const TextStyle(color: Color(0xFFFF5D5D))),
168 )
169 : const SizedBox.shrink();
170
171 final Widget form = wide
172 ? Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
173 Expanded(child: Column(mainAxisSize: MainAxisSize.min,
174 crossAxisAlignment: CrossAxisAlignment.stretch,
175 children: [header, const SizedBox(height: 10), ...login, fpField])),
176 const SizedBox(width: 24),
177 Expanded(child: Column(mainAxisSize: MainAxisSize.min,
178 crossAxisAlignment: CrossAxisAlignment.stretch,
179 children: [hostRow, pinField, relay, connectButton, failure])),
180 ])
181 : Column(
182 mainAxisSize: MainAxisSize.min,
183 crossAxisAlignment: CrossAxisAlignment.stretch,
184 children: [
185 header, const SizedBox(height: 20), ...login,
186 const SizedBox(height: 4), hostRow, pinField, fpField, relay,
187 const SizedBox(height: 8), connectButton, failure,
188 ],
189 );
190
191 return Scaffold(
192 body: SafeArea(
193 child: Center(
194 child: SingleChildScrollView( // only needed with the keyboard up
195 padding: EdgeInsets.all(wide ? 10 : 24),
196 child: ConstrainedBox(
197 constraints: BoxConstraints(maxWidth: wide ? 860 : 440),
198 child: Card(
199 child: Padding(
200 padding: EdgeInsets.fromLTRB(wide ? 18 : 24, wide ? 12 : 24, wide ? 18 : 24, wide ? 12 : 24),
201 child: form,
202 ),
203 ),
204 ),
205 ),
206 ),
207 ),
208 );
209 }
210
211 Widget _field(TextEditingController c, String label,
212 {String? hint, bool obscure = false, TextInputType? keyboard, bool dense = false}) {
213 return Padding(
214 padding: EdgeInsets.symmetric(vertical: dense ? 3 : 6),
215 child: TextField(
216 controller: c,
217 obscureText: obscure,
218 keyboardType: keyboard,
219 autocorrect: false,
220 enableSuggestions: false,
221 decoration: InputDecoration(labelText: label, hintText: hint, isDense: dense),
222 ),
223 );
224 }
225}