utils/github_device_auth/lib/src/device_code_dialog.dart
Krzysztof kuhy Rudnicki 0ef5461805
Some checks failed
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
sync_settings_ui tests / test (push) Waiting to run
github_device_auth tests / test (push) Has been cancelled
Move sync UI packages to material_ui
Flutter 3.50 deprecates package:flutter/material.dart. An app on
material_ui gives flutter/material widgets no MaterialLocalizations,
so sync_settings_ui 0.8.0 and github_device_auth 0.5.0 move first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:03:03 +01:00

119 lines
3.7 KiB
Dart

/// The GitHub device-flow dialog: shows the user code, opens the page, and
/// polls until the grant is approved.
library;
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:github_device_auth/src/device_auth.dart';
import 'package:material_ui/material_ui.dart';
import 'package:url_launcher/url_launcher.dart';
/// Dialog shown during the device flow: displays the user code, opens the
/// verification page, and polls until authorized — popping the token (or
/// null if cancelled / failed).
class DeviceCodeDialog extends StatefulWidget {
/// Creates the dialog for a pending device-flow grant.
const new({required this.device, required this.auth, super.key});
/// The pending grant: user code, verification URL, poll interval.
final DeviceCodeResponse device;
/// Auth client used to poll for the approved token.
final GitHubDeviceAuth auth;
@override
State<DeviceCodeDialog> createState() => _DeviceCodeDialogState();
}
class _DeviceCodeDialogState extends State<DeviceCodeDialog> {
String? _error;
@override
void initState() {
super.initState();
unawaited(_poll());
}
Future<void> _poll() async {
try {
final token = await widget.auth.pollForToken(widget.device);
if (mounted) Navigator.of(context).pop(token);
}
// `on Object`, not `on Exception`: an `Error` (notably `ArgumentError`
// from URI parsing) is not an `Exception`, so `on Exception` lets it
// escape and the dialog spins on "Waiting for authorization…" forever
// with nothing shown to the user. The same trap silently killed the
// Firebase sync tick in these apps -- see the 2026-08-11 fix.
on Object catch (e) {
if (mounted) setState(() => _error = '$e');
}
}
Future<void> _openPage() async {
await Clipboard.setData(ClipboardData(text: widget.device.userCode));
await launchUrl(
Uri.parse(widget.device.verificationUri),
mode: LaunchMode.externalApplication,
);
}
@override
Widget build(BuildContext context) {
// Glyphs in place of app-written words (kuhy's textgate rule); each old
// sentence is the spoken label. The user code is GitHub's data, typed
// by the user, so it stays visible.
return AlertDialog(
title: const Icon(Icons.key, semanticLabel: 'Authorize on GitHub'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(
Icons.keyboard,
semanticLabel: 'Enter this code on GitHub:',
),
const SizedBox(width: 12),
SelectableText(
widget.device.userCode,
style: Theme.of(context).textTheme.headlineSmall,
),
],
),
const SizedBox(height: 16),
if (_error case final error?)
Icon(
Icons.error,
color: Theme.of(context).colorScheme.error,
semanticLabel: error,
)
else
Semantics(
label: 'Waiting for authorization…',
child: const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
),
),
],
),
actions: [
IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: const Icon(Icons.close, semanticLabel: 'Cancel'),
),
FilledButton(
onPressed: _openPage,
child: const Icon(
Icons.open_in_new,
semanticLabel: 'Open GitHub & copy code',
),
),
],
);
}
}